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).
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
for i in range(1, n + 1):Fill in the Blanks
Spot the Error
Student with a class code? Join your class →