Multiple parameters in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a function takes more than one input, we want to know how its running time changes as these inputs grow.
We ask: How does the work increase when each input gets bigger?
Analyze the time complexity of the following code snippet.
def combine_lists(list1, list2):
result = []
for item1 in list1:
for item2 in list2:
result.append((item1, item2))
return result
This function pairs every item from the first list with every item from the second list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over both lists.
- How many times: Outer loop runs once per item in list1; inner loop runs once per item in list2 for each outer loop.
Each item in the first list pairs with every item in the second list, so work grows with both sizes multiplied.
| Input Size (list1, list2) | Approx. Operations |
|---|---|
| (10, 10) | 100 |
| (100, 100) | 10,000 |
| (1000, 1000) | 1,000,000 |
Pattern observation: Doubling both lists multiplies work by four; work grows fast as both inputs grow.
Time Complexity: O(n * m)
This means the time grows by multiplying the sizes of both inputs together.
[X] Wrong: "The time depends only on the bigger list size."
[OK] Correct: Because the function pairs every item from one list with every item from the other, both sizes matter equally.
Understanding how multiple inputs affect time helps you explain your code clearly and shows you can think about real problems with several factors.
"What if we changed the inner loop to only run half the size of list2? How would the time complexity change?"
Practice
def greet(name, age):Solution
Step 1: Understand function parameters
Parameters inside parentheses let a function accept inputs to use inside its code.Step 2: Multiple parameters mean multiple inputs
When separated by commas, each parameter is a separate input the function expects.Final Answer:
The function can receive more than one piece of information to work with. -> Option DQuick Check:
Multiple parameters = multiple inputs [OK]
- Thinking parameters are outputs
- Believing functions can't have more than one parameter
- Confusing parameters with function return values
x and y?Solution
Step 1: Check function definition syntax
Python functions use parentheses to list parameters separated by commas.Step 2: Identify correct separator and brackets
Parameters must be separated by commas inside parentheses, not spaces, semicolons, or brackets.Final Answer:
def add(x, y): -> Option CQuick Check:
Parameters separated by commas inside () [OK]
- Using spaces instead of commas
- Using semicolons or brackets instead of commas and parentheses
- Missing parentheses around parameters
def multiply(a, b):
return a * b
result = multiply(3, 4)
print(result)Solution
Step 1: Understand function call with parameters
The function multiply receives 3 and 4 as inputs for a and b.Step 2: Calculate the return value
It returns the product 3 * 4 = 12, which is stored in result and printed.Final Answer:
12 -> Option AQuick Check:
3 times 4 equals 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting an error due to parameters
def greet(name, age)
print(f"Hello {name}, you are {age} years old.")Solution
Step 1: Check function header syntax
Python requires a colon ':' at the end of the function header line.Step 2: Identify missing colon
The given function header lacks the colon after the closing parenthesis.Final Answer:
Missing colon at the end of the function header. -> Option AQuick Check:
Function header must end with ':' [OK]
- Forgetting the colon ':'
- Using wrong brackets for parameters
- Misplacing print statement
describe_person that takes name, age, and city as parameters and returns a sentence like:"Alice is 30 years old and lives in Paris."Which function definition and return statement is correct?
Solution
Step 1: Check function parameters and string formatting
The function must accept three parameters and return a formatted string with all parts included.Step 2: Compare return statements
def describe_person(name, age, city): return f"{name} is {age} years old and lives in {city}." uses an f-string correctly with all parts and punctuation. def describe_person(name, age, city): return name + " is " + age + " years old and lives in " + city + "." tries string addition but age is an int, causing error. def describe_person(name, age, city): print(f"{name} is {age} years old and lives in {city}.") prints instead of returning. def describe_person(name, age, city): return f"{name} is {age} years old lives in {city}." misses 'and' in the sentence.Final Answer:
def describe_person(name, age, city): return f"{name} is {age} years old and lives in {city}." -> Option BQuick Check:
Use f-string with all parameters and return [OK]
- Concatenating strings with int without conversion
- Using print instead of return
- Missing words or punctuation in the string
