Skip to content
PythonFunctionsbeginner18 min read

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

Using and overriding a default
python
def greet(name, greeting='Hello'):
print(f'{greeting}, {name}!')
 
greet('Ari')
greet('Ari', 'Welcome')
Output
Hello, Ari! Welcome, Ari!

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

A default value does not mean that a parameter can never receive another value. It is the fallback value used when the caller omits that argument.

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

Passing arguments by position and by name
python
def describe(name, city='Delhi'):
print(f'{name} lives in {city}.')
 
describe('Mira')
describe('Mira', 'Jaipur')
describe(name='Mira', city='Pune')
describe(city='Agra', name='Mira')
Output
Mira lives in Delhi. Mira lives in Jaipur. Mira lives in Pune. Mira lives in Agra.

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

A keyword argument describes how a value is passed in a function call. A keyword-only parameter is a different concept: it is a parameter that must be supplied by keyword.

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

Enforcing keyword-only arguments
python
def create_user(name, *, active=True):
print(f'{name}: {active}')
 
create_user('Asha')
create_user('Asha', active=False)
create_user(name='Dev', active=True)
Output
Asha: True Asha: False Dev: True

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

The examples show invalid calls as comments so that the complete example remains runnable. Uncomment an invalid call individually to inspect the error.

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

Worked dry run
python
def order_summary(item, quantity=1, status='pending'):
print(f'{item}: {quantity}, {status}')
 
order_summary('Notebook', status='shipped')
order_summary('Pen', 3)
Output
Notebook: 1, shipped Pen: 3, pending

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

A default is used separately for each call that omits its argument. Supplying an explicit value overrides the default for that call.

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

Avoid accidentally sharing a list default
python
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
 
print(add_item('pen'))
print(add_item('book'))
Output
['pen'] ['book']

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

Do not write def add_item(item, items=[]): if you expect a fresh list on every call. That list is created once when the function is defined and can retain changes between calls.

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.

Incorrect Approach
script.py
python
Thinking a default value is used even when the caller supplies an argument.
Correct Solution
script.py
python
A default is used only when the corresponding argument is omitted. An explicitly supplied value takes precedence.

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.

Incorrect Approach
script.py
python
Confusing keyword arguments with keyword-only parameters.
Correct Solution
script.py
python
Keyword arguments are used in calls, such as greet(name='Ari'). Keyword-only parameters are defined after * and must be supplied by keyword.

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.

Incorrect Approach
script.py
python
Putting a positional argument after a keyword argument in a function call.
Correct Solution
script.py
python
In a regular function call, write positional arguments first and keyword arguments afterward.

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.

Incorrect Approach
script.py
python
Giving the same parameter a value more than once.
Correct Solution
script.py
python
Match each parameter to only one argument. Do not provide it both positionally and by keyword.

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.

Incorrect Approach
script.py
python
Placing a required parameter after a defaulted parameter in the same parameter group.
Correct Solution
script.py
python
Put required positional-or-keyword parameters before parameters with defaults.

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.

Incorrect Approach
script.py
python
Using a mutable list or dictionary as a default without understanding its lifetime.
Correct Solution
script.py
python
Use None as the default and create a new list or dictionary inside the function when a fresh object is needed.

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.

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?
Answer:
It is a value specified in a function definition that Python uses when the caller omits the corresponding argument. For example, in def greet(name, greeting='Hello'), the greeting defaults to 'Hello', but the caller can provide another value.
Q2:What is the difference between positional and keyword arguments?
Answer:
Positional arguments are matched to parameters by order. Keyword arguments are matched by parameter name. For example, greet('Ari', 'Hi') passes values positionally, while greet(name='Ari', greeting='Hi') passes them by keyword.
Q3:Can keyword arguments be passed in any order?
Answer:
Keyword arguments can be supplied in different orders because Python matches them by parameter name. Each name must be valid and cannot assign a parameter more than once. In a regular call, positional arguments must come before keyword arguments.
Q4:What is a keyword-only parameter?
Answer:
A keyword-only parameter must be supplied by name when an argument is provided. A standalone * in the function definition marks the following parameters as keyword-only. For example, def save(path, *, overwrite=False) allows save('file.txt', overwrite=True), but not save('file.txt', True).
Q5:Why must required parameters come before defaulted parameters?
Answer:
For regular positional-or-keyword parameters, Python requires parameters without defaults to appear before parameters with defaults. A definition such as def example(a=1, b) is invalid syntax because the parameter ordering is not allowed.
Q6:Why should mutable default values such as lists be used carefully?
Answer:
A default list is created when the function is defined and may be reused across calls. Changes made to it can persist. If each call needs a fresh list, use None as the default and create a new list inside the function.

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.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: