note.nkmk.me

How to use range() in Python

Posted: 2020-07-22 / Tags: Python

In Python, you can use the built-in function range() to generate a series of numbers.

Here, the following contents will be described.

  • Difference between range() in Python2 and Python3
    • range() and xrange() in Python2
    • range() in Python3
  • range(stop): 0 <= x < stop
  • range(start, stop): start <= x < stop
  • range(start, stop, step): start <= x < stop (increasing by step)
  • Reversed range()
  • range() with float

See the following post for details of the for statement.

Sponsored Link

Difference between range() in Python2 and Python3

There is a difference between range() in Python2 and Python3. Note that an error will occur if the old code for Pyhton2 is executed as it is in Python3.

range() and xrange() in Python2

Python2 has two functions, range() and xrange().

range() returns 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'>

Since xrange() does not store all numbers but generates them when necessary, numbers are not displayed with print().

You can use xrange() as it is with for statement.

for i in xrange(3):
    print(i)
# 0
# 1
# 2

range() in Python3

Python3 doesn't have xrange() but only range().

# print(xrange(3))
# NameError: name 'xrange' is not defined

range() in Python3 is equivalent to xrange() in Python2 and returns an object of type range.

print(range(3))
# range(0, 3)

print(type(range(3)))
# <class 'range'>

for i in range(3):
    print(i)
# 0
# 1
# 2

If you want to generate a list like range() in Python2, use list().

print(list(range(3)))
# [0, 1, 2]

In the sample code below, the result of range() is converted into a list with list(). This is just for explanation, when you use it in for statement, list() is unnecessary.

range(stop): 0 <= x < stop

If you specify one integer as an argument like range(stop), a series of numbers 0 <= i <stop will be generated. 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 makes empty.

print(list(range(-3)))
# []

range(start, stop): start <= x < stop

If you specify two integers as an argument like range(start, stop), a series of numbers start <= i <stop will be generated. 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
Sponsored Link

range(start, stop, step): start <= x < stop (increasing by step)

If you specify three integers as arguments, such as range(start, stop, step), a series of numbers start <= i < stop increasing by step is generated.

If you specify a negative value for the third argument step, it will decrease. In this case, it will be empty unless 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 numbers.

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 function reversed() 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 list() if you use it in for statement.

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 the integer int as the argument 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, use list comprehensions.

print([i / 10 for i in range(3, 10, 2)])
# [0.3, 0.5, 0.7, 0.9]

A slight error may occur when multiplying floating point numbers. It can be rounded using round().

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, it is easier to use np.arange(). The specification of the argument of np.arange() is the same as that of range(), and float can be used.

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 posts for np.arange() and conversion between numpy.ndarray and list.

Sponsored Link
Share

Related Categories

Related Posts