AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Python loops

Week 6 · 1.4 Loops in text-based programming · 3 lessons
2

Start, stop, include

Computing 9: Topic 1.4, pp. 80–88
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.

PythonCounter takes the valuesBody runs
range(2, 6)2, 3, 4, 54
range(5, 6)51
range(1, 11)1, 2, 3, 4, 5, 6, 7, 8, 9, 1010
range(4, 4)(none)0
range(7, 3)(none)0
Worked examples
Python
1first = int(input("First stop? "))
2last = int(input("Last stop? "))
3for stop in range(first, last + 1):
4    print("Stop", stop)
Output
Stop 4
Stop 5
Stop 6
Stop 7
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 in range(2):
2    print("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 = 0
2for day in range(1, 8):
3    total = total + day
4    print("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’.

Practice
1
Complete the table.
PythonValues of the counterBody runs
range(3, 8)??
range(0, 5)??
range(6, 6)??
range(2, 12)??
2
Write a program that prints every whole number from 20 to 30, including 30.
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 in range(1, 10):
2    print(number)
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 in range(3):
2    print("Ring")
3    print("Pause")