Function Arguments (Positional, Keyword, Default & More) in Python
Python offers several distinct ways to pass arguments into a function, each suited to different situations: positional arguments matched by order, keyword arguments matched by name, default arguments that make a parameter optional, and two more specialized forms — positional-only and keyword-only — that control exactly how a caller is allowed to pass a value.
Every argument gets matched to a parameter
Either by position (order) or by name.
Positional arguments match left to right
In the exact order they’re written in the call.
Keyword arguments match by name
Regardless of the order they’re written in.
A default value makes an argument optional
param=value in the definition lets the caller skip it entirely.
Positional and keyword arguments can mix
As long as all positional arguments come before any keyword ones.
def describe_pet(name, animal="dog"): print(f"{name} is a {animal}") describe_pet("Rex")describe_pet("Rex", "cat")describe_pet(name="Rex", animal="cat")describe_pet(animal="cat", name="Rex")Positional Arguments
Matched to parameters purely by their order in the call — the first argument fills the first parameter, and so on.
def add(a, b): return a + b print(add(3, 5))Keyword Arguments
Matched to parameters by name rather than position, so the call can list them in any order.
def add(a, b): return a + b print(add(b=5, a=3))Default Arguments
A parameter with param=value in the definition becomes optional — the caller can skip it, and it falls back to that default.
def add(a, b=10): return a + b print(add(3))Positional-Only Arguments
Parameters before a / in the definition can only be passed positionally, never by keyword — this locks the parameter name down as an implementation detail.
def add(a, b, /): return a + b print(add(3, 5))Keyword-Only Arguments
Parameters after a * in the definition must be passed by keyword — this forces the caller to be explicit about what each value means.
def add(*, a, b): return a + b print(add(a=3, b=5))Arbitrary Arguments (*args, **kwargs)
A parameter prefixed with * collects any number of extra positional arguments into a tuple; ** does the same for keyword arguments into a dictionary — covered in full in the next topic.
def add_all(*numbers): return sum(numbers) print(add_all(1, 2, 3, 4))Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.