Variable-length arguments (*args) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using variable-length arguments, the function can take many inputs. We want to know how the time to run changes as the number of inputs grows.
How does the function's work increase when we add more arguments?
Analyze the time complexity of the following code snippet.
def sum_all(*args):
total = 0
for num in args:
total += num
return total
This function adds up all the numbers given as arguments.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each argument in
argsto add it. - How many times: Once for each argument passed to the function.
As we add more numbers to add, the function does more work, going through each number once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of inputs.
Time Complexity: O(n)
This means the time to add all numbers grows in a straight line as you add more numbers.
[X] Wrong: "Using *args makes the function run instantly no matter how many inputs."
[OK] Correct: Even with *args, the function still needs to look at each input once, so more inputs mean more work.
Understanding how variable inputs affect time helps you explain your code clearly and shows you think about efficiency, a skill valued in many coding situations.
"What if we changed the function to multiply all arguments instead of adding? How would the time complexity change?"
Practice
*args do in a Python function?Solution
Step 1: Understand the role of
*args*argscollects extra positional arguments passed to a function into a tuple.Step 2: Compare options with this behavior
Only Allows the function to accept any number of extra positional arguments correctly describes this behavior; others describe different concepts.Final Answer:
Allows the function to accept any number of extra positional arguments -> Option AQuick Check:
*args= flexible positional inputs [OK]
- Confusing *args with **kwargs
- Thinking *args limits arguments
- Assuming *args returns multiple values
Solution
Step 1: Recall the syntax for variable-length positional arguments
The correct syntax uses an asterisk before the parameter name:*args.Step 2: Match options with correct syntax
def func(*args): matches the correct syntax; others are invalid or for different purposes.Final Answer:
def func(*args): -> Option BQuick Check:
Star before name = variable positional args [OK]
- Using **args instead of *args
- Placing * after the parameter name
- Confusing syntax with keyword arguments
def add_numbers(*args):
return sum(args)
print(add_numbers(1, 2, 3, 4))Solution
Step 1: Understand how
The function collects all arguments into a tuple and sums them using*argsworks in the functionsum().Step 2: Calculate the sum of the arguments
1 + 2 + 3 + 4 = 10, so the function returns 10.Final Answer:
10 -> Option AQuick Check:
Sum of (1,2,3,4) = 10 [OK]
- Concatenating numbers as strings
- Expecting a TypeError for multiple args
- Forgetting that *args is a tuple
def greet(*names):
for name in names
print(f"Hello, {name}!")Solution
Step 1: Check the for loop syntax
The for loop line is missing a colon at the end, which is required in Python.Step 2: Verify other parts of the function
The*namessyntax and print statement are correct; the function can have *args.Final Answer:
Missing colon after for loop -> Option CQuick Check:
For loops need a colon [:] [OK]
- Forgetting colon after for loop
- Confusing *args with **kwargs
- Incorrect indentation
Solution
Step 1: Use
The function parameter must be*argsto accept variable positional arguments*argsto accept any number of positional arguments.Step 2: Return a list of squares using list comprehension
Use[x**2 for x in args]to square each argument and collect results in a list.Final Answer:
def squares(*args): return [x**2 for x in args] -> Option DQuick Check:
Use *args and list comprehension for squares [OK]
- Using **args instead of *args
- Forgetting to return a list
- Incorrect comprehension syntax
