Module 9 · Functions — topic 6 of 19
Positional arguments
Arguments matched to parameters by their order: when positional calls are clear, when they are error-prone, and positional-only parameters with the / marker.
Learning objectives
- Call functions with positional arguments correctly
- Recognise calls where the order is easy to confuse
- Define positional-only parameters with /
Definition
A positional argument is an argument without a name in a call; Python assigns positional arguments to the function's parameters in order from left to right.
Introduction
Most everyday calls are positional: len(text), max(a, b), pow(2, 10). The meaning is clear because the order is obvious or conventional.
Why it matters
Positional arguments are concise, but swapping two of them is a silent bug: transfer(from_acc, to_acc, amount) with the accounts reversed still runs — it just moves money the wrong way.
Detailed explanation
Positional arguments are matched strictly by position: the first argument to the first parameter, and so on.
They are ideal when there are one or two obvious inputs, or a conventional order (x, y), (start, stop).
When several parameters have the same type and no natural order, prefer keyword arguments or keyword-only parameters so callers can't mix them up.
Placing / in a parameter list makes the parameters before it positional-only: def power(base, exp, /) can't be called as power(base=2, exp=3). Built-ins like len and pow work this way, which leaves the parameter names free to change.
When you read someone else's function, check its signature before calling it positionally. In VS Code, hovering over the name or typing the opening parenthesis shows the parameters in order; in the interactive shell, help(function) prints them. A few seconds of checking prevents the hardest kind of bug to find — one that produces a plausible but wrong answer instead of an error message.
Syntax
- Order decides which parameter gets which value.
- / marks earlier parameters positional-only.
Code examples
Order matters
2.5 0.4
Positional-only parameters
1024 TypeError: power() got some positional-only arguments passed as keyword arguments: 'base, exponent'
More examples
A silent swapping bug
{'asha': 550, 'ravi': 50}
{'asha': 450, 'ravi': 150}Step-by-step explanation
- The first call passes the account names in the wrong order; Python can't know, so it moves money the wrong way.
- The second call names each argument, which makes the intent visible and the mistake obvious when reading.
Expected output
Running “A silent swapping bug” prints exactly:
Practical applications
- Mathematical functions (math.hypot(x, y)) use positional arguments.
- Banking and messaging APIs often require keywords for clarity.
Best practices
- Use positional arguments for one or two obvious inputs.
- Use keywords when the meaning isn't clear from the values.
- Hover over a function in VS Code to see its parameter order.
Common mistakes
Swapping same-typed arguments
Avoid
Prefer
Positional calls depend on remembering the order; check the signature.
Important notes
- The / marker was added in Python 3.8.
Key takeaways
- Positional arguments fill parameters in order.
- Swapping them causes silent bugs.
- / makes parameters positional-only.
Try it yourself
- 1Write bmi(weight_kg, height_m) and call it positionally; then explain what happens if you swap the arguments.
- 2Make the parameters of a clamp(value, lo, hi) function positional-only and test a keyword call.
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.
- ModerateWhat is printed?