Skip to content

Module 9 · Functions — topic 5 of 19

Arguments

The values passed in a call: how positional, keyword and unpacked arguments are matched to parameters, and the errors Python raises when they don't match.

Beginner 10 min read·Lesson 130 of 306 in Python Programming

Learning objectives

  • Match arguments to parameters in a call
  • Read and fix TypeError messages about arguments
  • Unpack sequences and dictionaries into arguments

Definition

An argument is a value supplied in a function call. Python matches arguments to parameters by position or by name, and raises a TypeError if the call provides too many, too few or duplicate values.

Introduction

When you call send_email("asha@example.com", subject="Hi"), the call supplies two arguments: one by position and one by name. Python's matching rules decide which parameter receives which value.

Why it matters

Argument errors are among the most common TypeErrors. Reading them quickly saves time, and unpacking with * and ** makes calls with data from lists and dictionaries short and clear.

Detailed explanation

Positional arguments fill parameters from left to right. Keyword arguments name the parameter explicitly: f(b=2, a=1). Positional arguments must come before keyword arguments in a call.

Every parameter without a default must receive exactly one value. Missing one gives 'missing 1 required positional argument'; giving one twice gives 'got multiple values for argument'.

Extra positional arguments give 'takes 2 positional arguments but 3 were given'; unknown keywords give 'got an unexpected keyword argument'.

*sequence in a call unpacks the items as separate positional arguments; **mapping unpacks key-value pairs as keyword arguments.

Arguments can be any expressions, including other calls; all are evaluated before the function runs.

Syntax

Python
def book(title, author, year):
...
book("Clean Code", "Martin", 2008) # positional
book(title="Clean Code", year=2008, author="Martin") # keyword
row = ["Clean Code", "Martin", 2008]
book(*row) # unpack a list
info = {"title": "Clean Code", "author": "Martin", "year": 2008}
book(**info) # unpack a dict
  • Positional before keyword.
  • Each parameter gets exactly one value.
  • * unpacks sequences, ** unpacks dicts.

Code examples

Positional and keyword arguments

Python
def describe(name, age, city):
return f"{name}, {age}, {city}"
print(describe("Asha", 20, "Pune"))
print(describe(city="Delhi", name="Ravi", age=22))
print(describe("Meera", city="Goa", age=19))
Output
Asha, 20, Pune
Ravi, 22, Delhi
Meera, 19, Goa

Unpacking into a call

Python
def volume(l, w, h):
return l * w * h
dims = (2, 3, 4)
print(volume(*dims))
sizes = {"h": 10, "l": 1, "w": 2}
print(volume(**sizes))
Output
24
20

More examples

Reading argument errors

Python
def rect(width, height):
return width * height
calls = [lambda: rect(2), lambda: rect(2, 3, 4), lambda: rect(2, width=3), lambda: rect(2, depth=3)]
for call in calls:
try:
call()
except TypeError as e:
print(e)
Output
rect() missing 1 required positional argument: 'height'
rect() takes 2 positional arguments but 3 were given
rect() got multiple values for argument 'width'
rect() got an unexpected keyword argument 'depth'

Step-by-step explanation

  • Each lambda wraps one faulty call so the loop can try them one at a time.
  • rect(2) leaves height without a value.
  • rect(2, 3, 4) has one positional argument too many.
  • rect(2, width=3) gives width a value by position and again by name.
  • rect(2, depth=3) uses a keyword that isn't a parameter.

Expected output

Running “Reading argument errors” prints exactly:

TextOutput
rect() missing 1 required positional argument: 'height'
rect() takes 2 positional arguments but 3 were given
rect() got multiple values for argument 'width'
rect() got an unexpected keyword argument 'depth'

Practical applications

  • Functions are often called with **config dictionaries loaded from JSON files.
  • CSV rows can be passed with *row into constructor functions.

Best practices

  • Use keyword arguments for clarity when a call has several values of the same type.
  • Unpack with * and ** instead of indexing manually.
  • Read the whole TypeError message: it names the parameter involved.

Common mistakes

Keyword before positional

Avoid

describe(name="Asha", 20, "Pune")

Prefer

describe("Asha", 20, city="Pune")

Once you use a keyword argument, every following argument must also be a keyword: SyntaxError otherwise.

Unpacking the wrong length

Avoid

volume(*[2, 3])

Prefer

volume(*[2, 3, 4])

The unpacked sequence must supply exactly the missing positional arguments.

Important notes

  • Python 3.8+ also allows positional-only parameters (marked with /), which can't be passed by keyword.

Key takeaways

  • Arguments are matched by position, then by name.
  • Each required parameter needs exactly one value.
  • * and ** unpack sequences and dictionaries into a call.

Try it yourself

  1. 1
    Call print with sep and end keyword arguments to print 1-2-3 followed by '!'.
  2. 2
    Store the arguments of a function call in a dictionary and call it with **.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • ModerateWhich call to def f(a, /, b, *, c) is invalid?
Practise this topic