Paul's Python CoursePPC

How does range() work, and why does it stop one early?

, 3 minutes to read

range() gives a for loop a run of whole numbers, starting at 0 unless you tell it otherwise and stopping just before the number you give it, so range(5) gives 0, 1, 2, 3 and 4. It stops one early so that range(5) hands you exactly five numbers, which also lines up with the way Python numbers the items in a list from 0.

What the numbers in the brackets mean

With one number, range() treats it as the stop and starts from 0. With two, the first is where to start and the second is where to stop. Wrapping a range in list() is a quick way to see what's inside it.

print(list(range(5)))
print(list(range(1, 5)))
print(len(range(3, 10)))
[0, 1, 2, 3, 4]
[1, 2, 3, 4]
7

The stop number never turns up. If you wanted 5 in that second list, you'd have to ask for range(1, 6). That catches nearly everyone the first few times, and the habit worth building is to add one to the last number you actually want. To count from 1 to 12, write range(1, 13).

Why it's built that way

It feels backwards at first, but stopping early makes the sums easy. A range from a up to b holds b - a numbers, so you can tell range(3, 10) has seven numbers without counting them, which is the 7 above. It also means two ranges that share a number fit together end to end, with nothing missed and nothing repeated.

for n in range(0, 3):
    print("first", n)
for n in range(3, 6):
    print("second", n)
first 0
first 1
first 2
second 3
second 4
second 5

The first loop stops before 3 and the second one starts on it, so 3 is printed once. And since list positions start at 0 as well, range(len(names)) gives you exactly the positions that exist in a list called names and nothing past the end.

Try it

This prints the seven times table, from 1 x 7 up to 12 x 7. The 13 is there because the loop stops before it.

Run it

main.py
table = 7
for n in range(1, 13):
    print(n, "x", table, "=", n * table)

Output

Press Run and what your program prints shows up here.

Change the 13 to 12 and run it again. The last line, 12 x 7 = 84, disappears. Then put it back and change table to another number.

You can also give range() a third number, the step, which is how far it moves each time. Try range(1, 13, 2) and you'll get the odd rows only, 1, 3, 5 and on up to 11. A negative step counts down, and it still stops one early, so range(10, 0, -1) goes from 10 down to 1 and never prints 0.

A small exercise

Write a loop that prints the even numbers from 2 to 20, one per line, with 20 included. Use a step rather than an if.

Show an answer
for n in range(2, 21, 2):
    print(n)

It starts at 2 and goes up in twos. The stop has to be 21, or anything just above 20, because the stop number itself is never printed. With range(2, 20, 2) the last number would be 18.

You'll use range() to print a whole times table in neat columns in the Times Table Trainer project. The Handbook page on range has more examples, including counting down and what an empty range does, and for loops covers the loop it usually feeds.

The project that goes with it

Times Table Trainer is a free project in Act 1, for Aaesha. It runs in your browser with nothing to install, and you don't need an account for it.

More guides