Skip to content
Laviyx Learn

Try

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.

Beginner5 min readUpdated
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:

Python
def book_ticket(name, seat, meal="veg"):
    return f"{name}: seat {seat}, {meal} meal"

print(book_ticket("Asha", "12A"))
Output
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:

Python
print(book_ticket(seat="3C", name="John", meal="non-veg"))
print(book_ticket("Meera", meal="vegan", seat="7B"))
Output
John: seat 3C, non-veg meal
Meera: seat 7B, vegan meal

Keyword 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:

Python
print("a", "b", sep="-")
print(sorted([3, 1, 2], reverse=True))
print(round(3.14159, 2))
Output
a-b
[3, 2, 1]
3.14

The ordering rule#

Positional arguments must come before keyword arguments:

Python
print(book_ticket(name="Asha", "12A"))
Output
  File "example.py", line 3
    print(book_ticket(name="Asha", "12A"))
                                        ^
SyntaxError: positional argument follows keyword argument

Passing more arguments than there are parameters is also an error:

Python
print(book_ticket("Asha", "12A", "veg", "extra"))
Output
TypeError: book_ticket() takes from 2 to 3 positional arguments but 4 were given

Unpacking a list or dictionary into arguments#

* spreads a list (or tuple) into positional arguments, and ** spreads a dictionary into keyword arguments:

Python
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)
Output
6
60

This 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:

Python
def change(items, value):
    items.append(99)
    value += 1

nums = [1, 2]
n = 10
change(nums, n)
print(nums, n)
Output
[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 += 1 creates a new integer inside the function and does not affect n.

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
Python
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."))
Output
Asha Rao
Ravi S. Kumar

Key takeaway#

Finished this lesson?