For loop execution model in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a for loop, we want to know how the time it takes grows as we repeat actions.
We ask: How does running the loop more times affect the total work done?
Analyze the time complexity of the following code snippet.
def print_numbers(n):
for i in range(n):
print(i)
This code prints numbers from 0 up to n-1 using a for loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print statement inside the for loop.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly in step with n; double n means double work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of loop steps.
[X] Wrong: "The loop runs instantly no matter how big n is."
[OK] Correct: Each loop step takes some time, so more steps mean more total time.
Understanding how loops grow helps you explain your code clearly and shows you know how programs scale.
"What if we added a nested for loop inside the first one? How would the time complexity change?"
Practice
for loop do in Python?Solution
Step 1: Understand the purpose of a for loop
A for loop runs code repeatedly for each item in a list, string, or range. 'Repeats a block of code for each item in a sequence' matches this; others do not.Final Answer:
Repeats a block of code for each item in a sequence -> Option AQuick Check:
For loop = repeat for each item [OK]
- Thinking for loops run only once
- Confusing for loops with if statements
- Believing for loops stop the program
Solution
Step 1: Recall Python for loop syntax
Python uses 'for variable in sequence:' followed by indented code. 'for i in range(5): print(i)' matches; others use wrong keywords or styles.Final Answer:
for i in range(5): print(i) -> Option CQuick Check:
Python for loop = for variable in sequence: [OK]
- Using 'to' instead of 'in'
- Using C-style for loop syntax
- Using 'foreach' which is not Python
for i in range(3):
print(i * 2)Solution
Step 1: Trace the loop range and output
range(3) gives i=0,1,2. Prints i*2: 0, 2, 4.Final Answer:
0 2 4 -> Option DQuick Check:
Multiply each i by 2 = 0 2 4 [OK]
- Printing i instead of i*2
- Starting count from 1 instead of 0
- Confusing range(3) with range(1,3)
for i in range(5)
print(i)Solution
Step 1: Check for syntax errors in for loop
Python requires ':' after the for statement. The code misses ':' after range(5).Final Answer:
Missing colon ':' after range(5) -> Option BQuick Check:
For loop needs ':' after header [OK]
- Forgetting colon ':' after for statement
- Incorrect indentation
- Assuming range(5) is invalid
Solution
Step 1: Identify correct code for list of squares using for loop
Initialize squares = [], then for i in range(5): append i*i, then print. squares = [] for i in range(5): squares.append(i*i) print(squares) works; others miss init, use comp, or overwrite.Final Answer:
squares = []\nfor i in range(5):\n squares.append(i*i)\nprint(squares) -> Option AQuick Check:
Initialize list, append squares in loop [OK]
- Not initializing list before appending
- Overwriting list variable inside loop
- Using list comprehension but question asks for for loop
