Skip to content

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.

Beginner 8 min read·Lesson 131 of 306 in Python Programming

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

Python
def power(base, exponent, /):
return base ** exponent
power(2, 10) # OK
power(base=2, exponent=10) # TypeError: positional-only
  • Order decides which parameter gets which value.
  • / marks earlier parameters positional-only.

Code examples

Order matters

Python
def divide(a, b):
return a / b
print(divide(10, 4))
print(divide(4, 10))
Output
2.5
0.4

Positional-only parameters

Python
def power(base, exponent, /):
return base ** exponent
print(power(2, 10))
try:
power(base=2, exponent=10)
except TypeError as e:
print("TypeError:", e)
Output
1024
TypeError: power() got some positional-only arguments passed as keyword arguments: 'base, exponent'

More examples

A silent swapping bug

Python
def transfer(balances, source, target, amount):
balances[source] -= amount
balances[target] += amount
accounts = {"asha": 500, "ravi": 100}
transfer(accounts, "ravi", "asha", 50) # meant asha -> ravi
print(accounts)
accounts = {"asha": 500, "ravi": 100}
transfer(accounts, source="asha", target="ravi", amount=50)
print(accounts)
Output
{'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:

TextOutput
{'asha': 550, 'ravi': 50}
{'asha': 450, 'ravi': 150}

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

range(10, 0) # empty: start=10, stop=0

Prefer

range(0, 10)

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

  1. 1
    Write bmi(weight_kg, height_m) and call it positionally; then explain what happens if you swap the arguments.
  2. 2
    Make 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?
Practise this topic