Argument order rules in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we write functions, the order of arguments can affect how the function runs, especially when some arguments are optional or have default values.
We want to see how the order of arguments impacts the number of steps the program takes.
Analyze the time complexity of the following code snippet.
def greet(name, greeting="Hello", punctuation="!"):
print(f"{greeting}, {name}{punctuation}")
greet("Alice")
greet("Bob", "Hi")
greet("Carol", punctuation=".")
This function prints a greeting message using required and optional arguments in a specific order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function call and string formatting happen once per call.
- How many times: Each call runs a fixed number of steps regardless of input size.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 call | Constant steps (format and print) |
| 10 calls | 10 times the constant steps |
| 100 calls | 100 times the constant steps |
Pattern observation: Each call takes the same amount of work, so total work grows linearly with the number of calls.
Time Complexity: O(n)
This means the total time grows directly with how many times the function is called, not with the argument order itself.
[X] Wrong: "Changing the order of arguments changes how fast the function runs."
[OK] Correct: The order of arguments affects how you call the function but does not change the number of steps inside the function.
Understanding argument order helps you write clear code and avoid mistakes, which is a valuable skill in any coding task or interview.
"What if we added a loop inside the function that repeats based on an argument? How would the time complexity change?"
Practice
Which of the following correctly describes the order of arguments in a Python function definition?
Solution
Step 1: Recall argument order rules
Python functions must define arguments in the order: positional-only, positional-or-keyword, then keyword-only.Step 2: Match the correct order
Positional-only, positional-or-keyword, keyword-only matches this exact order, others do not.Final Answer:
Positional-only, positional-or-keyword, keyword-only -> Option AQuick Check:
Argument order = Positional-only, positional-or-keyword, keyword-only [OK]
- Confusing keyword-only and positional-only order
- Thinking positional-or-keyword comes first
- Ignoring positional-only arguments
Which of the following function definitions is syntactically correct in Python?
def example(a, /, b, *, c):
passSolution
Step 1: Understand syntax for positional-only and keyword-only
The slash (/) marks positional-only arguments before it, and the asterisk (*) marks keyword-only arguments after it.Step 2: Check each option's order
def example(a, /, b, *, c): pass correctly placesabefore / (positional-only),bas positional-or-keyword, andcafter * (keyword-only). Others have invalid order or misplaced / and *.Final Answer:
def example(a, /, b, *, c): pass -> Option AQuick Check:
Syntax with / and * correct = def example(a, /, b, *, c): pass [OK]
- Placing / after positional-or-keyword arguments
- Putting * before /
- Using / as a standalone argument
What is the output of this code?
def func(a, /, b, *, c):
print(a, b, c)
func(1, 2, c=3)Solution
Step 1: Understand argument passing
ais positional-only, so must be passed by position (1).bis positional-or-keyword, passed by position (2).cis keyword-only, passed asc=3.Step 2: Check function call validity and output
All arguments are passed correctly. The print outputs the values: 1 2 3.Final Answer:
1 2 3 -> Option BQuick Check:
func(1, 2, c=3) prints 1 2 3 [OK]
- Passing positional-only argument by keyword
- Passing keyword-only argument positionally
- Confusing argument order in call
Identify the error in this function call:
def f(x, /, y, *, z):
return x + y + z
f(x=1, y=2, z=3)Solution
Step 1: Check argument types in definition
xis positional-only (before /),yis positional-or-keyword,zis keyword-only (after *).Step 2: Analyze function call
Callingf(x=1, y=2, z=3)tries to passxby keyword, which is not allowed for positional-only arguments, causing a TypeError.Final Answer:
TypeError: x is positional-only and cannot be passed as keyword -> Option DQuick Check:
Positional-only args cannot be keyword = TypeError [OK]
- Passing positional-only argument by keyword
- Thinking all arguments can be named
- Ignoring the / marker meaning
You want to define a function that takes exactly two positional-only arguments, one positional-or-keyword argument, and two keyword-only arguments. Which of these definitions is correct?
1. def func(a, b, /, c, *, d, e): pass 2. def func(a, /, b, c, *, d, e): pass 3. def func(a, b, c, /, *, d, e): pass 4. def func(a, b, /, *, c, d, e): pass
Solution
Step 1: Identify argument categories
Two positional-only args: before / (a, b). One positional-or-keyword: after / and before * (c). Two keyword-only: after * (d, e).Step 2: Check each definition
Definition 1 matches this order exactly:a, bbefore /,cafter /,d, eafter *. Others have wrong placement of / or * or argument counts.Final Answer:
Definition 1 -> Option CQuick Check:
Two positional-only, one normal, two keyword-only = Definition 1 [OK]
- Placing too many args before or after / or *
- Miscounting positional-only arguments
- Confusing keyword-only with positional-or-keyword
