Informatics Practices · Ch 3 — Brief Overview of Python
The range() Function
The range() Function
A for loop is only as useful as the sequence it walks through, and Python supplies a built-in function whose whole job is to manufacture sequences of integers on demand: range(). Instead of typing out a list like [0, 1, 2, 3, 4] by hand, you describe the sequence you want and range() generates it.
Its syntax is:
range([start], stop[, step])
The function creates a sequence of integers that begins at the start value and runs up to the stop value — excluding the stop value itself — moving by the given step on each jump. The square brackets in the syntax mark start and step as optional, and sensible defaults apply when they are omitted:
- If
startis not specified, the sequence begins from 0 by default. - If
stepis not specified, the value is incremented by 1 in each iteration by default. - All parameters of
range()must be integers. - The
stepcan be a positive or a negative integer, but it can never be zero.
The detail students trip over most often is that the stop value is never included: range(10) ends at 9, not at 10. Think of stop as a boundary the sequence walks up to but never touches.
Example 3.13 demonstrates every behaviour of the function. Wrapping the call in list() converts the generated sequence into a list so it can be displayed:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# start value is given as 2
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
# step value is 5 and start value is 0
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
# step value is -1, so a decreasing sequence is generated
>>> list(range(0, -9, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8]
Read these four calls as a tour of the parameters:
range(10)supplies onlystop. The defaults kick in, so the sequence starts at 0, climbs by 1, and stops just before 10 — ten integers, 0 through 9.range(2, 10)suppliesstartandstop. The sequence now begins at 2 instead of 0, still climbing by 1, and again 10 itself is left out.range(0, 30, 5)supplies all three values. Starting from 0 and jumping by 5, the sequence reaches 25 and stops, because the next jump would land on 30 — the excluded boundary.range(0, -9, -1)shows a negativestep. Withstepset to −1 the sequence decreases: it starts at 0 and walks down to −8, stopping short of −9. A decreasing sequence is only possible with a negative step.
The most common home for range() is inside a for loop, where it generates the sequence of numbers the loop variable runs through. Program 3-5 prints the multiples of 10 for the numbers in a given range:
#Program 3-5
#Print multiples of 10 for numbers in a range
for num in range(5):
if num > 0:
print(num * 10)
Output:
10
20
30
40
Here range(5) hands the loop the sequence 0, 1, 2, 3, 4, so num takes each of these values in turn. The if num > 0: test filters out the very first value: when num is 0 nothing is printed, and for the remaining values the program prints num * 10, giving 10, 20, 30 and 40. Notice how the loop, the condition and the arithmetic combine — range() drives the repetition while the if decides which iterations actually produce output.
A range() call can also drive a loop that sits inside another loop, as Program 3-6 demonstrates:
#Program 3-6
#Demonstrate working of nested for loops
for var1 in range(3):
print("Iteration " + str(var1 + 1) + " of outer loop")
for var2 in range(2): #nested loop
print(var2 + 1)
print("Out of inner loop")
print("Out of outer loop")
Output:
Iteration 1 of outer loop
1
2
Out of inner loop
Iteration 2 of outer loop
1
2 …