Methods with parameters in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use methods with parameters, we want to know how the time it takes to run changes as the input changes.
We ask: How does the method's work grow when the input values get bigger?
Analyze the time complexity of the following code snippet.
class Calculator:
def multiply(self, numbers):
result = 1
for num in numbers:
result *= num
return result
calc = Calculator()
print(calc.multiply([2, 3, 4, 5]))
This method multiplies all numbers in a list and returns the product.
- Primary operation: Looping through each number in the input list.
- How many times: Once for every number in the list.
As the list gets longer, the method does more multiplications, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The work grows directly with the size of the input list.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input list gets bigger.
[X] Wrong: "The method takes the same time no matter how many numbers are in the list."
[OK] Correct: Because the method must multiply each number, more numbers mean more work and more time.
Understanding how methods with parameters scale helps you explain your code clearly and shows you know how input size affects performance.
"What if the method also called another method inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand what parameters do
Parameters allow a method to receive inputs when it is called.Step 2: Identify the correct purpose
Parameters are not for storing data permanently or printing output; they are for input.Final Answer:
To accept inputs that the method can use -> Option AQuick Check:
Parameters = inputs [OK]
- Thinking parameters store data permanently
- Confusing parameters with output
- Believing parameters create variables outside method
a and b in Python?Solution
Step 1: Recall Python method syntax
Methods use parentheses () to list parameters separated by commas.Step 2: Identify correct syntax
Only def my_method(a, b): uses parentheses and commas correctly.Final Answer:
def my_method(a, b): -> Option CQuick Check:
Method parameters use ( ) and commas [OK]
- Using square brackets instead of parentheses
- Using curly braces instead of parentheses
- Omitting parentheses around parameters
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))Solution
Step 1: Understand the method call
The method greet is called with argument "Alice" passed to parameter name.Step 2: Evaluate the return statement
The method returns the string "Hello, Alice!" using f-string formatting.Final Answer:
Hello, Alice! -> Option DQuick Check:
Method returns greeting with input name [OK]
- Printing the parameter name instead of its value
- Confusing method name with output
- Expecting an error due to missing quotes
def add_numbers(x, y)
return x + ySolution
Step 1: Check method syntax
Python method definitions require a colon ':' after the parameter list.Step 2: Identify missing colon
The code misses the colon after (x, y), causing syntax error.Final Answer:
Missing colon after parameter list -> Option BQuick Check:
Method header ends with ':' [OK]
- Forgetting colon after parameters
- Using wrong brackets for parameters
- Misplacing return statement
calculate_area that takes two parameters width and height and returns their product. Which code correctly implements this?Solution
Step 1: Understand the method goal
The method should return the product (multiplication) of width and height.Step 2: Check each option's return value
def calculate_area(width, height): return width * height returns width * height, which is correct. Others return sum, print output, or division.Final Answer:
def calculate_area(width, height):\n return width * height -> Option AQuick Check:
Area = width x height [OK]
- Using + instead of * for multiplication
- Printing instead of returning value
- Dividing instead of multiplying
