Sequences are everywhere. Our alphabet and our numbers are sequences. And in computers, sequences of bits take meanings—they form numbers and values.
For looping, we often use an immutable sequence called range. This built-in method returns a sequence based on the numbers we pass it. Range receives a start, end and step.
This program shows how to use range()
with a for
-loop. This style of code may be considered "Pythonic" meaning it is simple and avoids just doing things like other languages.
range()
immutable sequence. It returns the values 0, 1 and 2, which we print in the loop body.# This sets "i" to 0, 1 and 2. # ... The step, not specified, is 1. for i in range(0, 3): print(i)0 1 2
When we use just one argument in range()
the start is considered zero. So a range 5 goes from 0 through 4 inclusive.
# Use a single argument in range to start at zero. for i in range(5): print(f"Range(5): {i}")Range(5): 0 Range(5): 1 Range(5): 2 Range(5): 3 Range(5): 4
Often we need to access adjacent list elements. One way to do this is with range()
. In this example, we loop through all the indexes in the list.
names = ["mark", "michelle", "cecil"] # Loop over range starting at index 1. for i in range(1, len(names)): # Adjacent names. print(names[i - 1], names[i])mark michelle michelle cecil
We can use range()
to decrement a variable, to move downwards. We specify the "step" value as the optional third parameter of range. We use -1 to decrement by one.
# Loop over range with negative step. for i in range(5, 0, -1): print(i)5 4 3 2 1
We can use range()
to create a list based on a sequence of values. This is much simpler than looping and adding numbers—it is shorter, less prone to bugs.
List
comprehension can also be used to create lists based on expressions. It is a preferred technique.# Create a list from a range. values = list(range(0, 4)) print(values)[0, 1, 2, 3]
What performance impact does range()
have? With range, we provide two or three numbers and loop over them. I found that range loops are faster than while
-loops.
while
-loop to sum numbers 10 million times.for-range
loop.for
-loop, with or without a range()
call, is faster than while in Python 3.3.import time print(time.time()) # Version 1: while-loop. c = 0 i = 0 while i < 10000000: c += i i += 1 print(time.time()) # Version 2: for-range loop. c2 = 0 for y in range(0, 10000000): c2 += y print(time.time())1406236639.696959 1406236643.343169 while-loop: 3.65 s 1406236645.533295 for-loop, range: 2.19 s
With range()
, we keep our code simple. And in the benchmark, we found that range()
does not cause any performance problems. It is faster than many alternatives.
In the Python documentation, range()
is considered an "immutable sequence." This means it cannot be changed after specified.
Because range()
is immutable, it can be heavily optimized—no allocations in memory are needed for a new range. With simpler, faster code, range is a good choice.