Learn Python
>>> functions.function_arguments

Function Arguments (Positional, Keyword, Default & More) in Python

#concept

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.

#how it works
1

Every argument gets matched to a parameter

Either by position (order) or by name.

2

Positional arguments match left to right

In the exact order they’re written in the call.

3

Keyword arguments match by name

Regardless of the order they’re written in.

4

A default value makes an argument optional

param=value in the definition lets the caller skip it entirely.

5

Positional and keyword arguments can mix

As long as all positional arguments come before any keyword ones.

#example
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")
Output
Rex is a dog
Rex is a cat
Rex is a cat
Rex is a cat
1describe_pet("Rex") passes only one positional argument — animal falls back to its default, "dog".
2describe_pet("Rex", "cat") passes both positionally, in the order they’re defined.
3describe_pet(name="Rex", animal="cat") passes both by keyword — the order no longer matters.
4describe_pet(animal="cat", name="Rex") proves keyword arguments can be given in any order, since Python matches them by name, not position.
#variants

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))
Output
8

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))
Output
8

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))
Output
13

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))
Output
8

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))
Output
8

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))
Output
10
#use cases
Making a function flexible with sensible defaults, so most callers can skip arguments they rarely need to change
Using keyword arguments to make a call self-documenting, like create_user(name='Ada', is_admin=True)
Locking down a parameter's name with positional-only syntax, so it can be renamed later without breaking callers
Forcing keyword-only arguments to prevent confusing calls where argument order is easy to get wrong

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.