FOR ... TO as range() in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

FOR ... TO as range() in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which Python loop matches FOR i ← 1 TO 10?
A for i in range(1, 10):
B for i in range(1, 11):
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)
3. Which statement about range(1, n) is correct?
A i stops at n - 1
B i stops at n
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):
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

for i in range(1, n + 1):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
# FOR i <- 1 TO n for i in range(1, _____): print(i)
2.
# FOR i <- 1 TO 5 for i in range(1, _): print(i)
3.
# FOR i <- 0 TO 20 STEP 5 for i in range(0, 21, _): print(i)
4.
# FOR i <- 5 TO 1 STEP -1 for i in range(5, _, -1): print(i)
5.
# FOR i <- 10 TO 2 STEP -2 for i in range(10, 1, __): print(i)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A n = 5
B total = 0
C for i in range(1, n):
D total = total + i
E print(total)
Question 2
A n = 5
B print("Countdown")
C print("Start")
D for i in range(n, 1, -1):
E print(i)
Question 3
A total = 0
B for i in range(2, 10, 2):
C total = total + i
D print("Sum 2 to 10:", total)
Question 4
A for i in range(1, 12):
B print(3 * i)
C print("3 times table to 12")
Question 5
A start = 10
B print("Countdown")
C for i in range(start, 0, 1):
D print(i)
E print("Go!")

Answer Key

FOR ... TO as range() in Python
Section A: Quick Questions
Q1
B — for i in range(1, 11):
Q2
A — 5 times (i is 1, 2, 3, 4, 5)
Q3
A — i stops at n - 1
Q4
B — for count in range(0, 21, 5):
Q5
A — The STEP: how much i changes each time
Section B: Match the Pairs
Q1
for i in range(1, n + 1): → Python version of FOR i ← 1 TO n; the + 1 makes the loop include n
Q2
for i in range(10, 0, -2): → Counts down 10, 8, 6, 4, 2 in steps of 2 and stops before reaching 0
Q3
The pseudocode FOR Count ← 1 TO 5 runs for the values 1, 2, 3, 4 and 5 → for count in range(1, 6):
Q4
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):
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
# FOR i <- 1 TO n for i in range(1, n + 1): print(i)
Q2
# FOR i <- 1 TO 5 for i in range(1, 6): print(i)
Q3
# FOR i <- 0 TO 20 STEP 5 for i in range(0, 21, 5): print(i)
Q4
# FOR i <- 5 TO 1 STEP -1 for i in range(5, 0, -1): print(i)
Q5
# FOR i <- 10 TO 2 STEP -2 for i in range(10, 1, -2): print(i)
Section E: Spot the Error
Q1
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).
Q2
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).
Q3
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).
Q4
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).
Q5
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).