← IGCSE Computer Science Paper 2

FOR ... TO as range() in Python

Python for FOR i ← 1 TO n. range() stops before its end value, so use n + 1 to include n. A third argument is the STEP; a negative step counts down.

Understanding FOR ... TO as range() in Python in IGCSE Computer Science Paper 2

What it does: The end value 5 must be included, so the range() end value is 6, one more than 5. STEP 5 becomes the third argument. The end value is 21 so that 20 is included, giving 0, 5, 10, 15, 20. range() stops before its end value, so you need 11 to include 10. range(1, 10) would miss the last pass. The end value 6 is not included, so i stops at 5. This is the classic off-by-one trap. range(0, 20, 5) gives 0, 5, 10, 15 and misses 20. Use 21 so that 20 is included.

Common mistakes: range() stops before its end value, so range(1, n) only goes up to 4 and misses 5. To match FOR i <- 1 TO n it must be range(1, n + 1). When counting down, range() stops before its end value, so an end of 1 stops at 2 and never prints 1. To count down to 1 the end value must be 0: range(n, 0, -1).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 216–231) and word search (Modules 188–231).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which Python loop matches FOR i ← 1 TO 10?
for i in range(1, 10):for i in range(1, 11):
Matches
for i in range(1, 10): # FOR i ← 1 TO 10
↓
Off-by-one error: range(1, 10) stops at 9, so the loop misses the value 10.
Applying
Meanings
Term
for i in range(1, n + 1):
Meaning
Python for FOR i ← 1 TO n. range() stops before its end value, so use n + 1 to include n. A third argument is the STEP; a negative step counts down.
Practice
n = 4
Testing
Gaps
for i in range(1, ):
Check Answer
Spots
2total = 0
3for i in range(1, n):

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Chameleon' badge — Type Shifter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which Python loop matches FOR i ← 1 TO 10?
A. for i in range(1, 10):
B. for i in range(1, 11): ✓
range() stops before its end value, so you need 11 to include 10. range(1, 10) would miss the last pass.
2. How many times does for i in range(1, 6): run the loop body?
A. 5 times (i is 1, 2, 3, 4, 5) ✓
B. 6 times (i is 1, 2, 3, 4, 5, 6)
The end value 6 is not included, so i stops at 5. This is the classic off-by-one trap.
3. Which statement about range(1, n) is correct?
A. i stops at n - 1 ✓
B. i stops at n
The end value is excluded. To include n, write range(1, n + 1).
4. Which Python loop matches FOR Count ← 0 TO 20 STEP 5?
A. for count in range(0, 20, 5):
B. for count in range(0, 21, 5): ✓
range(0, 20, 5) gives 0, 5, 10, 15 and misses 20. Use 21 so that 20 is included.
5. In range(2, 11, 3), what is the third argument 3?
A. The STEP: how much i changes each time ✓
B. The end value: where the loop stops
The order is range(start, stop, step). The third argument is the STEP, just like STEP in pseudocode.

Match the Pairs

for i in range(1, n + 1): → Python version of FOR i ← 1 TO n; the + 1 makes the loop include n
for i in range(10, 0, -2): → Counts down 10, 8, 6, 4, 2 in steps of 2 and stops before reaching 0
The pseudocode FOR Count ← 1 TO 5 runs for the values 1, 2, 3, 4 and 5 → for count in range(1, 6):
The pseudocode FOR Num ← 0 TO 20 STEP 5 becomes range() with a third argument for the STEP → for num in range(0, 21, 5):
for i in range(1, 10): # FOR i ← 1 TO 10 → Off-by-one error: range(1, 10) stops at 9, so the loop misses the value 10.
for i in range(5, 1, -1): # FOR i ← 5 TO 1 STEP -1 → The end value 1 is not included when counting down, so the loop stops at 2 and misses 1.

Key Term

for i in range(1, n + 1):
Python for FOR i ← 1 TO n. range() stops before its end value, so use n + 1 to include n. A third argument is the STEP; a negative step counts down.

Fill in the Blanks

1. # FOR i <- 1 TO n for i in range(1, n + 1): print(i)
Hint: Type the end expression that makes range() include n, since it stops before the end value.
2. # FOR i <- 1 TO 5 for i in range(1, 6): print(i)
Hint: Type the end value for range(): one more than the last number the loop should reach.
3. # FOR i <- 0 TO 20 STEP 5 for i in range(0, 21, 5): print(i)
Hint: Type the step value, which goes in the third argument of range().
4. # FOR i <- 5 TO 1 STEP -1 for i in range(5, 0, -1): print(i)
Hint: Type the end value when counting down. range() stops before it, so it is one past the last number wanted.
5. # FOR i <- 10 TO 2 STEP -2 for i in range(10, 1, -2): print(i)
Hint: Type the third argument that makes the loop count down in twos.

Spot the Error

Question 1
A n = 5
B total = 0
C for i in range(1, n):
D total = total + i
E print(total)
Line C — range() stops before its end value, so range(1, n) only goes up to 4 and misses 5. To match FOR i <- 1 TO n it must be range(1, n + 1).
Question 2
A n = 5
B print("Countdown")
C print("Start")
D for i in range(n, 1, -1):
E print(i)
Line D — When counting down, range() stops before its end value, so an end of 1 stops at 2 and never prints 1. To count down to 1 the end value must be 0: range(n, 0, -1).
Question 3
A total = 0
B for i in range(2, 10, 2):
C total = total + i
D print("Sum 2 to 10:", total)
Line B — FOR i <- 2 TO 10 STEP 2 includes 10, but range() stops before its end value, so range(2, 10, 2) misses 10. The end value must be 11: range(2, 11, 2).
Question 4
A for i in range(1, 12):
B print(3 * i)
C print("3 times table to 12")
Line A — The table should run from 1 to 12, but range(1, 12) stops at 11. Add 1 to the end value: range(1, 13).
Question 5
A start = 10
B print("Countdown")
C for i in range(start, 0, 1):
D print(i)
E print("Go!")
Line C — To count down, the step (third argument) must be negative. A step of 1 counts upwards from 10, so the loop never runs. It should be range(start, 0, -1).

Student with a class code? Join your class →