Positional arguments in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time it takes to run a function changes when we use positional arguments.
We want to know how the number of steps grows as we give more inputs.
Analyze the time complexity of the following code snippet.
def sum_numbers(a, b, c):
return a + b + c
result = sum_numbers(1, 2, 3)
print(result)
This code adds three numbers given as positional arguments and prints the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding three numbers once.
- How many times: Exactly one time, no loops or repeats.
Since the function always adds exactly three numbers, the work stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 2 additions |
| 10 | Still 2 additions |
| 100 | Still 2 additions |
Pattern observation: The number of operations does not grow with input size here.
Time Complexity: O(1)
This means the time to run the function stays the same no matter how many inputs you give, because it only handles a fixed number.
[X] Wrong: "Adding more positional arguments makes the function slower in a big way."
[OK] Correct: The function only does a fixed number of steps, so adding more arguments to the call doesn't change how many steps the function runs.
Understanding how fixed inputs affect time helps you explain simple function behavior clearly and confidently.
"What if the function used a loop to add a list of numbers passed as positional arguments? How would the time complexity change?"
Practice
What are positional arguments in Python functions?
Solution
Step 1: Understand argument passing
Positional arguments are passed to a function in the order the function defines its parameters.Step 2: Differentiate from other argument types
Unlike keyword arguments, positional arguments do not specify parameter names explicitly.Final Answer:
Arguments passed in the exact order the function expects -> Option AQuick Check:
Positional arguments = ordered values [OK]
- Confusing positional with keyword arguments
- Thinking default values are positional arguments
- Assuming order doesn't matter
Which of the following function calls uses positional arguments correctly?
def greet(name, age):
return f"Hello {name}, you are {age} years old."Solution
Step 1: Identify positional argument usage
Positional arguments are passed without naming, in the order parameters are defined.Step 2: Check each option
greet('Alice', 30) passes 'Alice' then 30 matching (name, age) order correctly.Final Answer:
greet('Alice', 30) -> Option DQuick Check:
Positional call = values in order [OK]
- Mixing keyword and positional arguments incorrectly
- Passing arguments in wrong order
- Using parameter names with positional arguments
What is the output of this code?
def multiply(a, b):
return a * b
result = multiply(3, 4)
print(result)Solution
Step 1: Understand function call with positional arguments
Arguments 3 and 4 are passed as a and b respectively.Step 2: Calculate the return value
3 multiplied by 4 equals 12, so the function returns 12.Final Answer:
12 -> Option CQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Confusing argument order
Find the error in this function call:
def divide(x, y):
return x / y
print(divide(10))Solution
Step 1: Check function parameters and call
Function divide expects two positional arguments: x and y.Step 2: Analyze the call with one argument
Calling divide(10) provides only one argument, missing the second required positional argument y.Final Answer:
Missing one positional argument in the call -> Option AQuick Check:
Function needs 2 args, got 1 [OK]
- Assuming missing arguments default to zero
- Thinking one argument is enough
- Ignoring error messages
Given this function:
def create_greeting(greeting, name, punctuation):
return f"{greeting}, {name}{punctuation}"Which call correctly uses positional arguments to produce Hello, Bob!?
Solution
Step 1: Identify parameter order
The function parameters are greeting, name, punctuation in that order.Step 2: Match arguments to parameters positionally
create_greeting('Hello', 'Bob', '!') passes 'Hello' to greeting, 'Bob' to name, and '!' to punctuation, producing the desired output.Final Answer:
create_greeting('Hello', 'Bob', '!') -> Option BQuick Check:
Order matches parameters for correct output [OK]
- Swapping argument order
- Mixing positional and keyword incorrectly
- Assuming keyword arguments are positional
