Home
Python
range Examples
This page was last reviewed on Mar 13, 2024.
Dot Net Perls
Range. 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.
So We access the range() immutable sequence. It returns the values 0, 1 and 2, which we print in the loop body.
Note The program iterates over numbers, but contains no iteration statement like i++. This expression is handled by range.
# 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
One argument. When we use just one argument in range() the start is considered zero. So a range 5 goes from 0 through 4 inclusive.
String Literal
# 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
Adjacent. 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.
Then We access the previous and current element in the list. These are adjacent elements. Please note we start the range at 1, not 0.
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
Range, negative. 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.
Note The second value (0) is never reached in the loop body—it is an "exclusive" boundary.
# Loop over range with negative step. for i in range(5, 0, -1): print(i)
5 4 3 2 1
Create list. 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.
Note 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]
A benchmark. 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.
Version 1 This version of the code uses a while-loop to sum numbers 10 million times.
Version 2 Here we do the same operation as version 1, but we use a for-range loop.
Result For performance, my tests show that a 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.
An immutable sequence. In the Python documentation, range() is considered an "immutable sequence." This means it cannot be changed after specified.
An optimization. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 13, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.