Lesson 22: Positional, keyword, default and variable-length parameters
A function can be called by position or by name, can have optional arguments, and can accept any number of them. Each style exists to make the call site readable.
In short
- Positional arguments are matched by order; keyword arguments are matched by name and can be in any order.
- A default value makes a parameter optional: `def f(a, b=0)` can be called as `f(5)`.
- `*args` collects extra positional arguments into a tuple; `**kwargs` collects extra keywords into a dictionary.
- Never use a list or dictionary as a default value — it is created once and shared between calls.
2
ways to pass a value: position and keyword
Python docs
1
default list shared by every call, which is why to avoid it
Python docs
Four ways to pass a value
Python gives you more than one way to call a function, and each one exists to make the calling line clearer.
| Style | Example | Meaning |
|---|---|---|
| Positional | student("Asha", "MCA") | First argument to first parameter |
| Keyword | student(name="Asha") | Matched by name, order free |
| Default | def f(a, b=0) | b may be left out |
| Variable length | def f(*args, **kwargs) | Any number of arguments |
The program
# params.py — positional, keyword and default arguments
def student(name, course="MCA", marks=0):
return f"{name} | {course} | {marks}"
print(student("Asha")) # defaults used
print(student("Ravi", "MCA")) # two positional
print(student("Neha", marks=91)) # keyword, course defaulted
print(student(marks=74, name="Amit")) # keyword, any order
def total(*numbers):
return sum(numbers)
print(total(1, 2, 3, 4)) # 10
def show(**info):
for key, value in info.items():
print(key, "=", value)
show(name="Asha", roll=21)
def add_item(item, bucket=[]): # wrong: shared default
bucket.append(item)
return bucket
print(add_item("a"))
print(add_item("b")) # the list from the earlier call is still there
How it works, step by step
student("Asha")fillsname;courseandmarkstake their defaults.student("Neha", marks=91)skipscourseby naming the parameter it wants. Combined calls like this are common in real code.*numberscollects every positional argument into a tuple, sosum()can add them all.**infocollects keywords into a dictionary, which is then looped withitems().- The last function shows the trap: a default list is created once, when the function is defined, so both calls append to the same list.
What it prints
Asha | MCA | 0 Ravi | MCA | 0 Neha | MCA | 91 Amit | MCA | 74 10 name = Asha roll = 21 ['a'] ['a', 'b']
The fix for the mutable default
def add_item(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
print(add_item("a")) # ['a']
print(add_item("b")) # ['b'], a fresh list each time
None is safe because it is never modified. The list is then created fresh inside each call.
Ordering rules, in one line each
- Parameters without defaults come before parameters with defaults.
- Positional arguments come before keyword arguments in a call.
*argscomes before**kwargs.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Write `student(name, course="MCA", marks=0)` and call it three ways: one argument, two, and one by keyword.
- Write a function `total(*numbers)` that returns the sum of any number of arguments.
- Write a function `describe(**info)` that prints every key and value it receives.
- Fix this bug: `def add(item, bucket=[])` — rewrite it so each call starts with an empty list.
Sources
Frequently asked questions
What is a keyword argument?
An argument passed by parameter name, such as student(name="Asha"). It can appear in any order and makes the call readable when there are several parameters.
What is a default parameter value?
A value written in the definition, used when the caller leaves that argument out. def greet(name="friend") can be called as greet() and still works.
What do *args and **kwargs do?
args collects extra positional arguments into a tuple; kwargs collects extra keyword arguments into a dictionary. They let a function accept any number of arguments.
Why is using [] as a default parameter a bug?
Because the default is created once, at definition time, and reused. Every call appends to the same list, so results leak between calls. Use None and create the list inside the function.