How to use range() in Python
In Python, you can generate a series of integers with the built-in range()
function.
See the following article for details of for
loops in Python.
range()
and the range
type in Python3
In Python3, range()
creates an object of range
type.
An object of range
type does not store values, but creates them when needed. Therefore, its values are not displayed with print()
. However, since it is an iterable object, its values can be printed in a for
loop.
print(range(3))
# range(0, 3)
print(type(range(3)))
# <class 'range'>
for i in range(3):
print(i)
# 0
# 1
# 2
Note that Python2 has range()
and xrange()
, and the behavior of range()
is different between Python2 and Python3. This will be explained at the end of this article.
Convert a range
object to a list
If you want to convert a range
object to a list, use the list()
function.
print(list(range(3)))
# [0, 1, 2]
In the following sample code, the result of range()
is converted into a list using list()
. Note that this conversion is for explanatory purposes only, and using list()
is not required when working with a for
loop.
range(stop)
: 0 <= x < stop
range(stop)
generates a series of integers 0 <= i < stop
. Note that stop
is not included in the result.
print(list(range(3)))
# [0, 1, 2]
print(list(range(10)))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Specifying a negative value returns an empty range
.
print(list(range(-3)))
# []
range(start, stop)
: start <= x < stop
range(start, stop)
generates a series of integers start <= i < stop
. Note that start
is included, but stop
is not included in the result.
The result will be empty when stop <= start
.
print(list(range(3, 10)))
# [3, 4, 5, 6, 7, 8, 9]
print(list(range(10, 3)))
# []
print(list(range(-3, 3)))
# [-3, -2, -1, 0, 1, 2]
print(list(range(3, -3)))
# []
range(0, stop)
is equivalent to range(stop)
.
print(range(0, 3) == range(3))
# True
range(start, stop, step)
: start <= x < stop (increasing by step)
range(start, stop, step)
generates a series of integers start <= i < stop
, increasing by step
.
If you provide a negative value for the step
argument, the sequence will decrease. In such cases, the sequence will be empty if start <= stop
.
print(list(range(3, 10, 2)))
# [3, 5, 7, 9]
print(list(range(10, 3, 2)))
# []
print(list(range(10, 3, -2)))
# [10, 8, 6, 4]
print(list(range(3, 10, -2)))
# []
range(start, stop, 1)
is equivalent to range(start, stop)
.
print(range(3, 10, 1) == range(3, 10))
# True
range(0, stop, 1)
is equivalent to range(0, stop)
and range(stop)
.
print(range(0, 10, 1) == range(0, 10) == range(10))
# True
Reversed range()
Specifying a negative value for the third argument step
can generate decreasing integers.
print(list(range(3, 10, 2)))
# [3, 5, 7, 9]
print(list(range(9, 2, -2)))
# [9, 7, 5, 3]
It is also possible to use the built-in reversed()
function to reverse the result of range()
.
print(list(reversed(range(3, 10, 2))))
# [9, 7, 5, 3]
Also, in this case, you don't need to convert the result to a list if you use the reversed range in a for
loop.
for i in reversed(range(3, 10, 2)):
print(i)
# 9
# 7
# 5
# 3
range()
with float
Like the previous examples, you can only specify integers (int
) as the arguments to range()
.
An error occurs when the floating point number (float
) is specified.
# print(list(range(0.3, 1.0, 0.2)))
# TypeError: 'float' object cannot be interpreted as an integer
If you want to generate a series of float
values, use list comprehensions.
print([i / 10 for i in range(3, 10, 2)])
# [0.3, 0.5, 0.7, 0.9]
A small error may occur when multiplying floating point numbers. You can round the result using the round()
function.
print([i * 0.1 for i in range(3, 10, 2)])
# [0.30000000000000004, 0.5, 0.7000000000000001, 0.9]
print([round(i * 0.1, 1) for i in range(3, 10, 2)])
# [0.3, 0.5, 0.7, 0.9]
If you can use NumPy, using np.arange()
is more convenient. The arguments for np.arange()
are the same as for range()
, with the added support for float
values.
import numpy as np
print(np.arange(3))
# [0 1 2]
print(np.arange(3, 10))
# [3 4 5 6 7 8 9]
print(np.arange(3, 10, 2))
# [3 5 7 9]
print(np.arange(0.3, 1.0, 0.2))
# [0.3 0.5 0.7 0.9]
See the following articles for np.arange()
and conversion between numpy.ndarray
and list
.
- numpy.arange(), linspace(): Generate ndarray with evenly spaced values
- Convert numpy.ndarray and list to each other
range()
and xrange()
in Python2
Python2 includes two functions: range()
and xrange()
. In Python3, xrange()
has been removed, leaving only range()
.
There is a difference between range()
in Python2 and Python3. Note that an error may occur if code written for Python2 is executed without modification in Python3.
range()
returns a list, and xrange()
returns an object of type xrange
.
print(range(3))
# [0, 1, 2]
print(type(range(3)))
# <type 'list'>
print(xrange(3))
# xrange(3)
print(type(xrange(3)))
# <type 'xrange'>
The xrange()
and xrange
types in Python2 are equivalent to the range()
and range
types in Python3.
If you want to run old Python2 code in Python3, you need to change xrange()
to range()
.
In Python2, range()
returns a list, which is equivalent to using list(range())
in Python3. When using range()
in a for
loop, you don't need to use list()
. However, if you want to treat the result of range()
as a list, you will need to convert it using list()
.