Parameters & Arguments
Learn how to pass data into functions. Master positional arguments, keyword arguments, and default values.
Learn how to pass data into functions. Master positional arguments, keyword arguments, and default values. This hands-on tutorial focuses on practical implementation of parameters & arguments concepts.
Parameters & Arguments
Functions become powerful when they can accept data. This is done using Parameters and Arguments.
- Parameter: The variable listed inside the parentheses in the function definition.
- Argument: The value that is sent to the function when it is called.
Positional Arguments
By default, arguments must be passed in the correct order.
Keyword Arguments
You can pass arguments by key=value syntax. In this case, the order doesn't matter.
Default Values
You can define a default value for a parameter. If the caller doesn't provide an argument, the default is used.
Advanced: Positional-Only and Keyword-Only Parameters ⚡
Python 3.8+ introduced special syntax to enforce how arguments can be passed.
Positional-Only (/)
Parameters before / MUST be passed positionally.
Keyword-Only (*)
Parameters after * MUST be passed as keywords.
Mutable Default Arguments Pitfall ⚠️
NEVER use mutable objects (like lists or dicts) as default values! They persist across function calls.
Unpacking Arguments with * and **
You can unpack lists and dictionaries when calling functions.
Coding Challenge: Flexible Greeting System 🎭
Create a function greet_user that:
- Takes a name (required)
- Takes a greeting (keyword-only, default "Hello")
- Takes any number of additional info items (*args)
- Prints a formatted greeting
AI Mentor
Confused about "Python function parameters arguments and default values"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 5What is the difference between a parameter and an argument?
Key Takeaways
✅ Parameters define what data a function accepts.
✅ Arguments are the actual data passed.
✅ Use / for positional-only and * for keyword-only parameters.
✅ Never use mutable defaults ([], {}).
✅ Use * and ** to unpack arguments.
What's Next?
Now that we can send data in, let's learn how to get data out using Return Values.
Keep coding! 🚀