GoalUse range(start, stop) to begin at any number, and work out which values run and how many times.
New words
start · the first value of the counter in range(start, stop)stop · the value that range does not reach; the counter stays below itindentation · the spaces at the start of a line; they show which lines are inside the loop
Explanation
A second form of range gives you the start as well: range(start, stop). The counter begins at start and goes up by 1. It stops before it reaches stop, so stop itself never runs. The body runs stop − start times. To include the last number you want, write that number plus 1 as the stop value, for example range(1, 11) for 1 to 10. If stop is not larger than start, the loop does not run. Use this form when the counter must start at a particular number. Indentation matters too. Lines indented below the for line are repeated. The first line that is not indented runs once, after the loop has finished.
A bus route. The user types 4 for the first stop and 7 for the last stop, and the last stop must be included. So the stop value is last + 1. The counter takes 4, 5, 6 and 7.
Python
1for n inrange(2):
2print("Go")
3print("Done")
Output
Go
Go
Done
Indentation. Only the print("Go") line is indented, so it is the only line inside the loop. The loop runs twice, and then print("Done") runs once.
Python
1total = 02for day inrange(1, 8):
3 total = total + day
4print("Day", day, "pot", total)
5print("Final pot", total)
Output
Day 1 pot 1
Day 2 pot 3
Day 3 pot 6
Day 4 pot 10
Day 5 pot 15
Day 6 pot 21
Day 7 pot 28
Final pot 28
A money pot. Seven days, and on day 1 one coin is added, on day 2 two coins, and so on. The loop starts at 1 and the stop value is 8 to include day 7. The pot grows by the day number each time. The pot is 1, 3, 6, 10, 15, 21 and 28.
Class activity
Stop before the line (no computer). Draw a long line of 15 boxes numbered 0 to 14. Write range cards such as range(3, 8), range(10, 13) and range(6, 6). For each card, put a coin on the start box, move it along one box at a time, and say each number. Stop when the next move would land on the stop box, and place a red counter on that box to show that it does not run. Write down the values, count them, and check with stop − start. Then make a card for ‘2 to 9 including 9’.
Write a program that prints every whole number from 20 to 30, including 30.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): for n in range(20, 31): / [indent] print(n). Check: The stop value is 31, so 30 is included.
3
This program should print 1 to 10 but it stops at 9. Find the mistake and correct it. What kind of error is this?
Python
1for number inrange(1, 10):
2print(number)
The stop value must be 11, because range stops before the stop value: range(1, 11). It is a logic error: the program runs but gives the wrong result.
4
Predict what this program prints. How does the output change if print("Pause") is moved to the left so that it is not indented?
Python
1for t inrange(3):
2print("Ring")
3print("Pause")
It prints Ring, Pause, Ring, Pause, Ring, Pause (six lines), because both lines are inside the loop. Without the indent, Pause is printed once at the end: Ring, Ring, Ring, Pause.