Python xrange() Function
xrange() is a function in Python 2 used to generate a sequence of numbers.
xrange()The function usage is the same asrangeexactly; the difference is that it does not generate an array, but a generator.
Note:In Python 3,xrange()it was removed,range()the function was improved and hasxrange()all the functions and features. That is, in Python 3,range()the function lazily generates sequences, so there is no longer a need forxrange()。
Syntax
xrange syntax:
xrange(stop) xrange(start, stop[, step])
Parameter description:
- start: Counting starts from start. Default is starting from 0. For example, xrange(5) is equivalent to xrange(0, 5)
- stop: Counting ends at stop, but does not include stop. For example: xrange(0, 5) is [0, 1, 2, 3, 4] without 5
- step: step size, default is 1. For example: xrange(0, 5) is equivalent to xrange(0, 5, 1)
Return Value
Returns a generator.
Examples
The following examples demonstrate how to use xrange:
>>>xrange(8)
xrange(8)
>>> list(xrange(8))
[0, 1, 2, 3, 4, 5, 6, 7]
>>> range(8) # Using range
[0, 1, 2, 3, 4, 5, 6, 7]
>>> xrange(3, 5)
xrange(3, 5)
>>> list(xrange(3,5))
[3, 4]
>>> range(3,5) # Using range
[3, 4]
>>> xrange(0,6,2)
xrange(0, 6, 2) # Step size is 2
>>> list(xrange(0,6,2))
[0, 2, 4]
>>>
Python Built-in Functions