Python range
Python hosting: PythonAnywhere — host, run and code Python in the cloud. Free tier available.
The range() function returns of generates a sequence of numbers, starting from the lower bound to the upper bound.
range(lower_bound, upper_bound, step_size)
- lower_bound: The starting value of the list.
- upper_bound: The max value of the list, excluding this number.
- step_bound: The step size, the difference between each number in the list.
The python range function in the interpreterRelated Course:
Practice Python with interactive exercises
range implementation difference This distinction won't usually be an issue. The range() is implemented slightly different in the Python versions:
- Python 2.x: The range() function returns a list.
- Python 3.x: The range() function generates a sequence.
>>> range(5)
A call to range(1,10) returns: 1,2,3,4,5,6,7,8,9
>>> range(1,10)
Calling range(0,10,2) returns: 0,2,4,6,8
>>> range(0,10,2)
range in python 3 To generate a list using range, add the list function
>>> list(range(5))
We can use all parameters (lower bound, upper bound, step)
>>> list(range(0,10,2))
[0, 2, 4, 6, 8]
python 2 implementation This version of range() allocates computer memory and also populates the computer memory behind the scenes. For large ranges, this is implementation is not very efficient.
Usually you won't have any issues with the Python2 implementation of range() but if you use large numbers (millions of items) you could run into issues.