4.7. The range FunctionΒΆ
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()
It turns out that generating lists with a specific number of integers is a very common thing to do, especially when you
want to write simple for loop
controlled iteration. Even though you can use any four items, or any four integers for that matter, the conventional thing to do is to use a list of integers starting with 0.
In fact, these lists are so popular that Python gives us special built-in
range
objects
that can deliver a sequence of values to
the for
loop. 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.
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 function is actually a very powerful function
when it comes to
creating sequences of integers. It can take one, two, or three parameters. We have seen
the simplest case of one parameter such as range(4)
which creates [0, 1, 2, 3]
.
But what if we really want to have the sequence [1, 2, 3, 4]
?
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. What happened to the 5?
In this case we interpret the parameters of the range function to mean
range(start,stop+1).
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.
Here are a two examples for you to run. Try them and then add another line below to create a sequence starting at 10 and going up to 20 (including 20).
Codelens will help us to further understand the way range works. In this case, the variable i
will take on values
produced by the range
function.
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. 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)
. The most general form of the 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.
Try it in codelens.
Check your understanding
-
turtle-8-1: In the command range(3, 10, 2), what does the second argument (10) specify?
- (A) Range should generate a list that stops at 9 (including 9).
- Range will generate the list [3, 5, 7, 9].
- (B) Range should generate a list that starts at 10 (including 10).
- The first argument (3) tells range what number to start at.
- (C) Range should generate a list starting at 3 that stops at 10 (including 10).
- Range will always stop at the number before (not including) the specified ending point for the list.
- (D) Range should generate a list using every 10th number between the start and the stopping number.
- The third argument (2) tells range how many numbers to skip between each element in the list.
-
turtle-8-2: What command correctly generates the list [2, 5, 8]?
- (A) range(2, 5, 8)
- This command generates the list [2] because the first number (2) tells range where to start, the second number tells range where to end (5, not inclusive) and the third number tells range how many numbers to skip between elements (8). Since 10>= 8, there is only one number in this list.
- (B) range(2, 8, 3)
- This command generates the list [2, 5] because 8 is not less than 8 (the specified ending number).
- (C) range(2, 10, 3)
- The first number is the starting point, the second is the maximum allowed, and the third is the amount to increment by.
- (D) range(8, 1, -3)
- This command generates the list [8, 5, 3] because it starts at 8, ends at (or above 1), and skips every third number going down.
-
turtle-8-3: What happens if you give range only one argument? For example: range(4)
- (A) It will generate a list starting at 0, with every number included up to but not including the argument it was passed.
- Yes, if you only give one number to range it starts with 0 and ends before the number specified incrementing by 1.
- (B) It will generate a list starting at 1, with every number up to but not including the argument it was passed.
- Range starts at 0 unless otherwise specified.
- (C) It will generate a list starting at 1, with every number including the argument it was passed.
- Range starts at 0 unless otherwise specified, and never includes its ending element (which is the argument it was passed).
- (D) It will cause an error: range always takes exactly 3 arguments.
- If range is passed only one argument, it interprets that argument as the end of the list (not inclusive).