GoalWrite and read a Python for loop with range(n), and say which values the counter takes.
New words
range(n) · a Python tool that gives the numbers 0, 1, 2 and so on, stopping before nloop body · the indented lines that run again on every passcounter · the loop variable, such as tick or day, that holds the current number
Explanation
Python repeats a fixed number of times with the word for. The simplest form is for name in range(n): and the line ends with a colon. The lines of the body are indented below it. The counter starts at 0 and the body runs n times. The last value is n − 1, because range stops before n. You can choose any name for the counter, and you use that name inside the body. If you want to show 1, 2, 3 instead of 0, 1, 2, print counter + 1. When the number of repeats comes from the user, convert it with int(input(...)) first. In pseudocode this loop is FOR counter ← 0 TO n - 1.
Python
Counter takes the values
Body runs
range(3)
0, 1, 2
3
range(6)
0, 1, 2, 3, 4, 5
6
range(1)
0
1
range(0)
(none)
0
Worked examples
Python
1for tick inrange(4):
2print("Tick", tick)
Output
Tick 0
Tick 1
Tick 2
Tick 3
A clock that ticks four times. The counter is called tick and it starts at 0. The program prints the word and the counter. The last value is 3 because range(4) stops before 4. The loop has four passes.
Python
1days = int(input("How many days? "))
2for day inrange(days):
3print("Day", day + 1)
Output
Day 1
Day 2
Day 3
The number of repeats comes from the user, who types 3. The program converts the text to a number with int. The counter still starts at 0, so day + 1 is printed to give Day 1, Day 2 and Day 3. (The prompt appears on the screen first; the output box shows only what print writes.)
Python
1for step inrange(5):
2print(step * 3)
Output
0
3
6
9
12
The counter can be used in a calculation. Here every value is multiplied by 3. The counter takes 0, 1, 2, 3 and 4, so the output is five numbers, starting with 0 and not with 3. The pseudocode twin is FOR step ← 0 TO 4.
Class activity
Number cards (no computer). Write the numbers 0 to 9 on ten small cards and lay them in a row. Write loop cards such as for n in range(4):, for n in range(7): and for n in range(0):. For each loop card, pick up the cards the counter takes, in order, and read the numbers aloud. Count the cards in your hand and compare with the number in the brackets. Then make a card that says print(n * 2) and read out what the program would print for range(5). If you have a computer or tablet, type one of the loops into IDLE and compare.
Practice
1
Write down what this program prints.
Python
1for x inrange(4):
2print(x + 10)
10, 11, 12 and 13, each on its own line. The counter x is 0, 1, 2 and 3, and 10 is added each time.
2
Write this pseudocode as a Python for loop.
Pseudocode
1FOR i ← 0 TO 72 OUTPUT "Row ", i
3NEXT i
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): for i in range(8): / [indent] print("Row", i). Check: The counter starts at 0 and ends at 7, so range(8) is needed.
3
This program prints 0 to 4. Change it so that it prints 1 to 12.
Python
1for c inrange(5):
2print(c)
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): for c in range(12): / [indent] print(c + 1). Check: range(12) gives 0 to 11, and adding 1 gives 1 to 12.
4
Write a Python program that prints the first six multiples of 4: 4, 8, 12, 16, 20 and 24, using range(6).
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): for i in range(6): / [indent] print((i + 1) * 4). Check: The counter is 0 to 5, so (i + 1) * 4 gives 4, 8, 12, 16, 20 and 24.