Positional Parameters
The most basic type. Arguments match parameters by position β first argument goes to first parameter, second to second, etc.
def greet(first_name, last_name):
print(f"Hello, {first_name} {last_name}!")
greet("Alice", "Smith") # Alice β first_name, Smith β last_name
greet("Bob", "Jones")
Default Parameter Values
Parameters can have default values β used when the caller doesn't provide that argument.
def power(base, exponent=2): # exponent defaults to 2
return base ** exponent
print(power(5)) # 25 - uses default exponent=2
print(power(5, 3)) # 125 - overrides default
print(power(2, 10)) # 1024
Always put parameters with default values AFTER parameters without defaults, or you get a SyntaxError.
*args β Variable Positional Arguments
*args collects any number of positional arguments into a tuple.
def sum_all(*numbers): # numbers is a tuple
print(f"Received: {numbers}")
return sum(numbers)
print(sum_all(1, 2, 3)) # 6
print(sum_all(10, 20, 30, 40)) # 100
print(sum_all()) # 0 - empty tuple
**kwargs β Variable Keyword Arguments
**kwargs collects any number of keyword arguments into a dictionary.
def print_info(**details): # details is a dict
for key, value in details.items():
print(f" {key}: {value}")
print_info(name="Alice", age=25, city="London")
Parameter Order β The Golden Rule
When combining parameter types, order matters: regular β *args β keyword-only β **kwargs.
# Full parameter order example
def full_function(a, b, c=10, *args, keyword_only=99, **kwargs):
print(f"a={a}, b={b}, c={c}")
print(f"args={args}")
print(f"keyword_only={keyword_only}")
print(f"kwargs={kwargs}")
full_function(1, 2, 3, 4, 5, keyword_only="hello", x=100)