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.
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
- Positional before keyword.
- Each parameter gets exactly one value.
- * unpacks sequences, ** unpacks dicts.
Code examples
Positional and keyword arguments
Asha, 20, Pune Ravi, 22, Delhi Meera, 19, Goa
Unpacking into a call
24 20
More examples
Reading argument errors
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:
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
Prefer
Once you use a keyword argument, every following argument must also be a keyword: SyntaxError otherwise.
Unpacking the wrong length
Avoid
Prefer
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
- 1Call print with sep and end keyword arguments to print 1-2-3 followed by '!'.
- 2Store 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?