Iteration using range() in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use range() to repeat actions, it is important to know how the time needed grows as the number of repeats increases.
We want to understand how the work changes when the range gets bigger.
Analyze the time complexity of the following code snippet.
for i in range(n):
print(i)
This code prints numbers from 0 up to n-1, repeating the print action n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
print(i)inside the loop. - How many times: Exactly once for each number from 0 to n-1, so n times.
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: If you double n, the work doubles too. The growth is steady and direct.
Time Complexity: O(n)
This means the time needed grows in a straight line with the size of n.
[X] Wrong: "The loop runs instantly because it just counts numbers."
[OK] Correct: Even counting and printing each number takes time, so the total time grows with n.
Understanding how simple loops grow with input size is a key skill. It helps you explain how your code behaves when handling more data.
"What if we changed range(n) to range(0, n, 2)? How would the time complexity change?"
Practice
range(5) do in a Python for loop?Solution
Step 1: Understand the range function
range(5)starts at 0 by default and stops before 5.Step 2: Identify the numbers generated
The numbers generated are 0, 1, 2, 3, 4.Final Answer:
Generates numbers from 0 to 4 -> Option AQuick Check:
range(5) = 0,1,2,3,4 [OK]
- Thinking range(5) includes 5
- Starting count at 1 by default
- Confusing stop value as inclusive
range()?Solution
Step 1: Check each range syntax
for i in range(1, 3): loops from 1 to 2 (2 times), for i in range(3): loops 0 to 2 (3 times), for i in range(0, 3, 2): loops 0 and 2 (2 times), for i in range(3, 0): runs zero times because start > stop without negative step.Step 2: Identify the correct loop count
Only for i in range(3): loops exactly 3 times: 0,1,2.Final Answer:
for i in range(3): -> Option BQuick Check:
range(3) loops 3 times [OK]
- Using range(1, 3) which loops 2 times
- Using invalid range with start > stop
- Confusing step parameter
for i in range(2, 7, 2):
print(i, end=' ')Solution
Step 1: Understand the range parameters
range(2, 7, 2) starts at 2, stops before 7, stepping by 2.Step 2: List the numbers generated
Numbers are 2, 4, 6.Final Answer:
2 4 6 -> Option DQuick Check:
range(2,7,2) = 2,4,6 [OK]
- Including 7 in output
- Using wrong step size
- Starting from 0 instead of 2
for i in range(5, 1):
print(i)Solution
Step 1: Analyze the range parameters
range(5, 1) means start at 5, stop before 1, with default step 1.Step 2: Understand loop behavior
Since start is greater than stop and step is positive, the loop runs zero times.Final Answer:
Loop does not run because start > stop without step -> Option CQuick Check:
range(5,1) empty range [OK]
- Expecting loop to run backwards
- Thinking syntax error occurs
- Assuming infinite loop
range(). Which code does this correctly?Solution
Step 1: Identify odd numbers range
Odd numbers from 1 to 15 are 1,3,5,...,15.Step 2: Check each option
for i in range(1, 16, 2): print(i) uses range(1,16,2) which generates 1,3,5,...,15 directly. for i in range(0, 15, 2): print(i) starts at 0 (even). for i in range(1, 15): if i % 2 == 1: print(i) misses 15 (range(1,15) goes to 14) and uses extra if check. for i in range(1, 16): print(i % 2) prints 1 or 0, not numbers.Final Answer:
for i in range(1, 16, 2): print(i) -> Option AQuick Check:
range(1,16,2) = odd numbers [OK]
- Starting at 0 for odd numbers
- Forgetting to include last number
- Printing modulo instead of number
