Default and Keyword Arguments
Use default values, pass arguments by name, and understand keyword-only parameters.
In this lesson, you will learn
- ✓Differentiate parameters, arguments, default values, and keyword arguments.
- ✓Explain when Python uses a default parameter value and when a supplied argument overrides it.
- ✓Call functions using positional and keyword arguments and identify invalid argument combinations.
- ✓Explain the difference between keyword arguments and keyword-only parameters.
- ✓Trace function calls and predict their output, including relevant errors.
- ✓Write and test functions with appropriate default values.
1. Default parameter values
A parameter is a name in a function definition. An argument is a value supplied when the function is called. A default parameter value is used when the caller omits the corresponding argument.
Write a default using an equals sign in the function definition: def greet(name, greeting='Hello'). Here, name is required, while greeting has a default value of 'Hello'. The caller can accept that default or provide another greeting.
Python uses the default only when the corresponding argument is omitted. If the caller supplies a value for that parameter, the supplied value is used instead.
Using and overriding a default
In the function definition, name has no default and greeting defaults to 'Hello'. In greet('Ari'), the argument 'Ari' fills name. Because greeting is omitted, Python uses 'Hello'. In greet('Ari', 'Welcome'), the first argument fills name and the second fills greeting. The supplied greeting replaces the default for this call. Each print call displays one line, so the output contains two lines.
Note
2. Positional and keyword arguments
A positional argument is matched to a parameter by its position in the function call. A keyword argument is matched by writing the parameter name followed by an equals sign and its value.
For example, greet('Ari', 'Welcome') passes both arguments positionally. The call greet(name='Ari', greeting='Welcome') passes both by keyword. These calls provide the same values to the same parameters.
Keyword arguments are useful when a function has several parameters because the names make the intended values clearer. They also let you supply parameters in a different order, provided each argument is valid and no parameter receives a value twice.
In a regular function call, positional arguments must come before keyword arguments. You cannot give a parameter one value positionally and then give it another value by keyword.
Passing arguments by position and by name
The first call supplies name and omits city, so city defaults to 'Delhi'. The second call supplies both values positionally, so city becomes 'Jaipur'. The third call supplies both values by keyword. The parameter names identify where each value belongs. The fourth call reverses the keyword order. It still works because Python matches each value by its parameter name.
Note
3. Keyword-only parameters and parameter ordering
Python also supports keyword-only parameters. In a function definition, a standalone * marks the point after which parameters must be passed by keyword. This can make calls clearer and prevent arguments from being confused by position.
In def create_user(name, *, active=True), name can be passed positionally or by keyword, but active must be passed by keyword if the caller wants to provide it. The default still applies when active is omitted.
A parameter with a default cannot appear before a regular parameter without a default in the same positional-or-keyword parameter group. For example, def example(a=1, b) is invalid syntax. Put required parameters before defaulted parameters.
In a function call, positional arguments must precede keyword arguments. Also, do not assign a value to the same parameter more than once. These rules prevent ambiguous or invalid calls.
Enforcing keyword-only arguments
The * in the definition makes active keyword-only. The first call omits active, so its default value True is used. The second call supplies active=False by keyword. The third call supplies both parameters by keyword. All three calls are valid.
Note
4. Trace a function call step by step
To trace a function call, first identify the parameters and their defaults. Next, match each supplied argument to its parameter. Fill in defaults for omitted optional parameters, then execute the function body with those values.
Do not confuse the function definition with a function call. Defining a function creates the function; its body runs when the function is called.
When a function returns a value, track that value back to the caller. When it uses print(), the printed text appears in the output.
Worked dry run
First call: item receives 'Notebook'. The caller omits quantity, so quantity is 1. The keyword status='shipped' overrides the default status='pending'. The first function call prints Notebook: 1, shipped. Second call: item receives 'Pen' and quantity receives 3 by position. The caller omits status, so status is 'pending'. The second function call prints Pen: 3, pending. The two print calls produce two output lines.
Note
5. Practical use and common edge cases
Defaults are useful when a function has a common, sensible value, such as a quantity of 1 or a status of 'pending'. Keyword arguments are especially helpful when a call would otherwise be difficult to understand from argument positions alone.
Choose defaults that make sense for the function. A required piece of information should generally remain required rather than being given a misleading default.
Avoid mutable objects such as lists or dictionaries as defaults unless you deliberately want the same object to be reused across calls. Python evaluates a default expression when the function is defined, not afresh every time the function is called.
A common alternative is to use None as a default and create a new list inside the function when no list is supplied. This avoids unintentionally sharing one list between calls.
Avoid accidentally sharing a list default
The default value is None, which indicates that the caller did not provide a list. Each call that receives None creates a new empty list inside the function. The item is appended to that call's list, and the list is returned. The second call creates a separate list, so it does not contain the 'pen' from the first call.
Caution
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Thinking a default value is used even when the caller supplies an argument.
Why it happens: Python uses the supplied argument when one is provided; the default is used only when that parameter is omitted.
Key takeaway: A default is used only when the corresponding argument is omitted. An explicitly supplied value takes precedence.
#2 Confusing keyword arguments with keyword-only parameters.
Why it happens: A keyword argument is a way to pass a value by parameter name at call time; a keyword-only parameter is one that must be passed by name.
Key takeaway: Keyword arguments are used in calls, such as greet(name='Ari'). Keyword-only parameters are defined after * and must be supplied by keyword.
#3 Putting a positional argument after a keyword argument in a function call.
Why it happens: In a regular function call, positional arguments must come before keyword arguments so parameter matching is unambiguous.
Key takeaway: In a regular function call, write positional arguments first and keyword arguments afterward.
#4 Giving the same parameter a value more than once.
Why it happens: Supplying a parameter positionally and again by keyword gives it two values and raises TypeError.
Key takeaway: Match each parameter to only one argument. Do not provide it both positionally and by keyword.
#5 Placing a required parameter after a defaulted parameter in the same parameter group.
Why it happens: Within a parameter group, required parameters must appear before parameters with defaults so calls remain unambiguous.
Key takeaway: Put required positional-or-keyword parameters before parameters with defaults.
#6 Using a mutable list or dictionary as a default without understanding its lifetime.
Why it happens: A mutable default object is created once when the function is defined and reused across calls, so mutations can unexpectedly persist between calls.
Key takeaway: Use None as the default and create a new list or dictionary inside the function when a fresh object is needed.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is a default parameter value in Python?
Q2:What is the difference between positional and keyword arguments?
Q3:Can keyword arguments be passed in any order?
Q4:What is a keyword-only parameter?
Q5:Why must required parameters come before defaulted parameters?
Q6:Why should mutable default values such as lists be used carefully?
Key takeaways
- A default parameter value is used when its argument is omitted; an explicitly supplied value overrides it.
- Positional arguments match by order, while keyword arguments match by parameter name.
- In a regular function call, positional arguments must come before keyword arguments.
- A standalone * in a function definition marks subsequent parameters as keyword-only.
- Do not assign the same parameter more than once in a call.
- Place required positional-or-keyword parameters before parameters with defaults.
- Use None and create a fresh object inside the function when a mutable default would otherwise be shared across calls.
- Trace each call by matching arguments, applying defaults, and then following the function body.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: