Positional vs.
Keyword Arguments
Master the two fundamental ways to pass data into Python functions: by position and by name.
What Is a Function Argument?
When you call a function, you supply values inside the parentheses. Those values are called arguments. The function's definition receives them through parameters: the variable names listed in its signature.
Two Ways to Pass Arguments
Why Does It Matter?
Choosing the right argument style affects readability, flexibility, and correctness. A wrong order in positional arguments silently produces bugs. Keyword arguments make intent explicit and let you skip optional parameters.
The Core Rule
Positional Arguments in Detail
Positional arguments are concise and work well when a function has few parameters whose purpose is obvious from context.
Keyword Arguments in Detail
Positional vs. Keyword: Side by Side
| Positional | Keyword | |
|---|---|---|
| Matching | By order | By name |
| Order sensitive? | Yes | No |
| Readability | Shorter calls | Self-documenting |
| Best for | 1-2 obvious params | Many or confusable params |
| Can skip defaults? | No | Yes |
Default Values & Optional Args
Mixing Positional and Keyword
How to Apply: Step by Step
Example 1: Simple Function
Example 2: Skipping Defaults
Example 3: Avoiding Silent Bugs
Common Mistakes
Summary Table
| Feature | Positional | Keyword | Mixed |
|---|---|---|---|
| Syntax | func(val1, val2) | func(p=val) | func(val, p=val) |
| Order | Strict | Flexible | Pos first, kw after |
| Can skip defaults? | No | Yes | Yes |
| Readability | Low (many args) | High | Balanced |
| Error risk | Silent swap | Typo in name | Both possible |
Question 1 of 5
Given def area(length, width), which call correctly uses keyword arguments to compute area(5, 3)?
Question 2 of 5
What happens when you run greet(greeting="Hi", "Sam")?
Question 3 of 5
Given def log(msg, level="INFO", timestamp=True), which call sets timestamp to False without changing level?
Question 4 of 5
What error does power(2, base=3) produce if def power(base, exponent)?
Question 5 of 5
Which statement about positional arguments is FALSE?