Parameters and Arguments
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Python offers tremendous flexibility when passing inputs to functions. You can pass arguments by position or explicitly by name (keyword arguments).
Positional arguments
Positional argument is a value passed to a function by its place in the call. The first value goes to the first parameter, the second to the second, and so on.
By default, arguments match parameters based on their order in the function call.
def format_name(first, last): return f"{last}, {first}"
print(format_name("Sunita", "Sharma"))Example 1 · Position decides the meaning
def order_line(product, quantity, price): print(f"{quantity} x {product} at {price} = {quantity * price}")
order_line("Keyboard", 2, 1500)order_line("Keyboard", 1500, 2)2 x Keyboard at 1500 = 3000 1500 x Keyboard at 2 = 3000
What this code does
- The first call matches by position: quantity is 2 and price is 1500.
- The second call swaps the numbers. Python does not complain, but the line now says 1500 x Keyboard at 2.
Because 'Sunita' was passed first, it binds to first. 'Sharma' binds to last.
Keyword arguments
Keyword argument is a value passed with the parameter's name, as in role="Admin". Keyword arguments can come in any order.
You can explicitly specify the parameter name during the call: param=value. Order does not matter when using keyword arguments.
def create_user(username, email, role): return f"{username} ({role}) <{email}>"
print(create_user(role="Admin", username="arun", email="arun@work.com"))Example 2 · Names make the call safe
def order_line(product, quantity, price): print(f"{quantity} x {product} at {price} = {quantity * price}")
order_line(price=1500, product="Keyboard", quantity=2)order_line("Mouse", quantity=3, price=400)2 x Keyboard at 1500 = 3000 3 x Mouse at 400 = 1200
What this code does
- Keyword arguments can come in any order because each one names its parameter.
- A call may start with positional values and then continue with keywords. Never put a positional value after a keyword.
Keyword arguments make function calls self-documenting and prevent bugs caused by swapped arguments.
Returning multiple values
Tuple unpacking assigns the items of a returned tuple to separate variables in one line, such as low, high = get_min_max(values).
In Python, a function can return several values separated by commas. Python bundles them into a tuple (see Tuples (opens in a new tab)), which you can unpack straight into variables.
def get_min_max(a, b, c): return min(a, b, c), max(a, b, c)
low, high = get_min_max(12, 45, 8)print(f"Lowest: {low}, Highest: {high}")Example 3 · Quotient and remainder
def divide_with_remainder(dividend, divisor): quotient = dividend // divisor remainder = dividend % divisor return quotient, remainder
q, r = divide_with_remainder(17, 5)print(f"Quotient: {q}, Remainder: {r}")Quotient: 3, Remainder: 2
What this code does
- Calculates quotient ($17 // 5 = 3$) and remainder ($17 % 5 = 2$).
- Returns both values as a tuple and unpacks them into
qandr.
Example 4 · One tuple, then three names
def summary(a, b, c): total = a + b + c return total, total / 3, max(a, b, c)
result = summary(70, 80, 90)print(result)total, average, best = resultprint(total, average, best)(240, 80.0, 90) 240 80.0 90
What this code does
- Printing
resultshows it is a single tuple:(240, 80.0, 90). - Unpacking needs exactly as many names as the tuple has items.
low receives 8 and high receives 45 via automatic tuple unpacking.
Next step
Check your understanding
9 quick questions