7. The range Type

In our simple example from the last section (shown again below), we used a list of four integers to cause the iteration to happen four times. We said that we could have used any four values. In fact, we even used four colors.

import turtle            # set up alex
wn = turtle.Screen()
alex = turtle.Turtle()

for i in [0, 1, 2, 3]:   # repeat four times
    alex.forward(50)
    alex.left(90)

wn.exitonclick()

Iterating a certain number of times is a very common thing to do, especially when you want to write simple for loop controlled iteration. Because this is such a common task, Python gives us a special built-in range type that can provide a sequence of values for the for loop to iterate through. The sequence provided by range always starts with 0. If you ask for range(4), then you will get 4 values starting with 0. In other words, 0, 1, 2, and finally 3. Notice that 4 is not included since we started with 0. Likewise, range(10) provides 10 values, 0 through 9. This way of starting a count at 0, instead of at 1, is called zero-based indexing and is very common in computer programming.

for i in range(4):
    # Executes the body with i = 0, then 1, then 2, then 3
for x in range(10):
    # sets x to each of ... [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Note

Computer scientists like to count from 0!

So to repeat something four times, a good Python programmer would do this:

for i in range(4):
    alex.forward(50)
    alex.left(90)

The range constructor (a constructor is a special class method that creates an object; we’ll learn more about this in the chapter on classes) can take one, two, or three parameters. We have seen the simplest case of one parameter such as range(4) which creates the sequence [0, 1, 2, 3]. But what if we really want to have the sequence [1, 2, 3, 4] instead? We can do this by using a two parameter version of range where the first parameter is the starting point and the second parameter is the ending point. The evaluation of range(1,5) produces the desired sequence. Why don’t we see a 5 in the sequence? It’s because we interpret the parameters to mean range(start,stop+1). So you must subtract 1 from the second parameter to know what the final integer in the sequence will be.

Note

Why in the world would range not just work like range(start, stop)? Think about it like this. Because computer scientists like to start counting at 0 instead of 1, range(N) produces a sequence of things that is N long, but the consequence of this is that the final number of the sequence is N-1. In the case of (start, stop) it helps to simply think that the sequence begins with start and continues as long as the number is less than stop.

Codelens will help us to further understand the way range works. In this case, the variable i will take on values produced by range(10).

(rangeme)

Finally, suppose we want to have a sequence of even numbers. How would we do that? Easy, we add another parameter, a step, that tells range what to count by. Say that for even numbers we want to start at 0 and count by 2’s. So if we wanted the first 10 even numbers we would use range(0,19,2).

Try it in codelens.

(rangeme2)

The most general form of range is range(start, stop, step). You can also create a sequence of numbers that starts big and gets smaller by using a negative value for the step parameter.

Note that we are casting the range object as a list — a data type we will discuss further later in the book.

Check your understanding