Python course menu
Python Function Arguments: Positional, Keyword and Unpacking
Learn how to pass arguments to Python functions: positional vs keyword arguments, the ordering rules, unpacking lists and dictionaries with * and **, and how mutable arguments can be changed by a function.
On this page
When you call a function, the values you pass are its arguments. Python gives you two ways to pass them, by position and by name, plus shortcuts for passing a whole list or dictionary at once.
Positional arguments#
Values are matched to parameters in order:
def book_ticket(name, seat, meal="veg"):
return f"{name}: seat {seat}, {meal} meal"
print(book_ticket("Asha", "12A"))Asha: seat 12A, veg meal"Asha" goes to name and "12A" to seat because of their positions.
Keyword arguments#
Name the parameter, and order no longer matters:
print(book_ticket(seat="3C", name="John", meal="non-veg"))
print(book_ticket("Meera", meal="vegan", seat="7B"))John: seat 3C, non-veg meal
Meera: seat 7B, vegan mealKeyword arguments make calls easier to read, especially for booleans and numbers: send_email(to, retry=True, timeout=30) says far more than send_email(to, True, 30).
You already use them with built-in functions:
print("a", "b", sep="-")
print(sorted([3, 1, 2], reverse=True))
print(round(3.14159, 2))a-b
[3, 2, 1]
3.14The ordering rule#
Positional arguments must come before keyword arguments:
print(book_ticket(name="Asha", "12A")) File "example.py", line 3
print(book_ticket(name="Asha", "12A"))
^
SyntaxError: positional argument follows keyword argumentPassing more arguments than there are parameters is also an error:
print(book_ticket("Asha", "12A", "veg", "extra"))TypeError: book_ticket() takes from 2 to 3 positional arguments but 4 were givenUnpacking a list or dictionary into arguments#
* spreads a list (or tuple) into positional arguments, and ** spreads a dictionary into keyword arguments:
def add(a, b, c):
return a + b + c
nums = [1, 2, 3]
print(add(*nums)) # same as add(1, 2, 3)
info = {"a": 10, "b": 20, "c": 30}
print(add(**info)) # same as add(a=10, b=20, c=30)6
60This is handy when the values already live in a list or come from a configuration dictionary. The dictionary's keys must match the parameter names.
Can a function change its arguments?#
It depends on whether the argument is mutable:
def change(items, value):
items.append(99)
value += 1
nums = [1, 2]
n = 10
change(nums, n)
print(nums, n)[1, 2, 99] 10- The list was changed, because the function received the same list object and modified it.
- The number was not, because
value += 1creates a new integer inside the function and does not affectn.
Common mistakes#
Practice#
Practice exercise
Write format_name(first, last, middle="") that returns the full name, skipping the middle name when it is empty. Call it once with positional arguments, and once with all three as keyword arguments in a different order.
Show hint
Put the parts in a list and " ".join() only the non-empty ones.
Show solution
def format_name(first, last, middle=""):
parts = [first, middle, last]
return " ".join(p for p in parts if p)
print(format_name("Asha", "Rao"))
print(format_name(last="Kumar", first="Ravi", middle="S."))Asha Rao
Ravi S. KumarKey takeaway#
Finished this lesson?