Section 5.7 The range
Function
If we want to print our table of squares and cubes of the numbers 1 to 5, we can also do it with a for
loop:
for n in [1, 2, 3, 4, 5]:
print(n, n * n, n * n * n)
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 a special built-in range
function that can deliver a sequence of values to the for
loop. When called with one argument, 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.
Here’s how we can do our table from 1 to 5 using range
. We have to add one to transform the numbers 0 to 4 into 1 to 5. To save some typing, we will use the exponentiation operator **
:
The number in the parentheses can also be a variable, so we can let the user choose the end number for the table:
The
range 1 function is actually a very powerful function when it comes to creating sequences of integers. It can take one, two, or three arguments. We have seen the simplest case of one argument 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 argument version of
range
where the first argument is the starting point and the second argument is the ending point. The evaluation of
range(1, 5)
produces the desired sequence
[1, 2, 3, 4]
.
The two-argument version of range(start, stop)
gives you the sequence starting with the start value up to but not including the stop value. That’s why we have to put in 5 for our stop value to get the sequence [1, 2,3, 4]
.
Here are 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? We add another argument, 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 ten even numbers we would use range(0, 19, 2)
.
The most general form of the range is range(start, stop, step)
. This will produce a sequence starting at the start value, adding the step to get the next number in the sequence, up to but not including the stop value. 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. Do you see why the first two statements produce the same result?
Checkpoint 5.7.3.
In the command range(3, 10, 2)
, what does the second argument (10) specify?
Range should generate a sequence that stops before 10 (including 9).
- Range will generate the sequence 3, 5, 7, 9.
Range should generate a sequence that starts at 10 (including 10).
- The first argument (3) tells range what number to start at.
Range should generate a sequence starting at 3 that stops at 10 (including 10).
- Range will always go up to but not including the specified limit for the sequence.
Range should generate a sequence 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 sequence.
Checkpoint 5.7.4.
Which command correctly generates the sequence 2, 5, 8?
range(2, 5, 8)
- This command generates the sequence with just the number 2 because the first parameter (2) tells range where to start, the second number tells range where to end (before 5) and the third number tells range how many numbers to skip between elements (8). Since 10 >= 5, there is only one number in this sequence.
range(2, 8, 3)
- This command generates the sequence 2, 5 because 8 is not less than 8 (the end value is never included).
range(2, 10, 3)
- The first number is the starting point, the second is past the last allowed, and the third is the amount to increment by.
range(8, 1, -3)
- This command generates the sequence 8, 5, 2 because it starts at 8, ends before 1, and skips to every third number going down.
Checkpoint 5.7.5.
What happens if you give range
only one argument? For example: range(4)
It will generate a sequence 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.
It will generate a sequence starting at 1, with every number up to but not including the argument it was passed.
-
range
with one parameter starts at 0.
It will generate a sequence starting at 1, with every number including the argument it was passed.
-
range
with one parameter starts at 0 and never includes the argument it was passed.
It will cause an error: range
always takes exactly 3 arguments.
- If
range
is passed only one argument, it interprets that argument as one past the end of the list.
Checkpoint 5.7.6.
Which range
function call will produce the sequence 20, 15, 10, 5?
range(5, 25, 5)
- The step 5 is positive, while the given sequence is decreasing. This answer creates the reversed, increasing sequence.
range(20, 3, -5)
- Yes: If we take steps of -5, down to but not including 3, we get 20, 15, 10, 5
range(20, 5, 4)
- The step 5 is positive, so the sequence would need to increase from 20 toward 4. That does not make sense, and the sequence would be empty.
range(20, 5, -5)
- The sequence goes down to, but never includes, the second parameter (5).
Checkpoint 5.7.7.
What could the second parameter (12) in range(2, 12, 4) be replaced with and generate exactly the same sequence?
No other value would give the same sequence.
- The sequence produced has steps of 4: 2, 6, 10. The next would be 14, but it is not before the limit 12. There are other limit choices past 10, but not past 14.
The only other choice is 14.
- 14 would work: It is also past 10, and not past 14, but there are other integers with the same properties.
11, 13, or 14
- Yes, any integer past 10, and not past the next step at 14 would work.
http://docs.python.org/py3k/library/functions.html?highlight=range#range