Python xrange() Function
xrange() is a function in Python 2 used to generate a sequence of numbers.
xrange() The function is used in the same way as range , exactly the same, except that what is generated is not an array but a generator.
**Note:** in Python 3,xrange() has been removed,range() the function has been improved and now has xrange() all the features and capabilities of . In other words, in Python 3 the range() function generates sequences lazily, so there is no longer any need for xrange().
Syntax
xrange syntax:
xrange(stop)
xrange(start, stop[, step])
Parameter Description:
- start: counting begins at start. The default is 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] with no 5
- step: the step size, defaulting to 1. For example: xrange(0, 5) is equivalent to xrange(0, 5, 1)
Return Value
Returns a generator.
Example
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
)
Use range
[
3
,
4
]
xrange
(
0
,
6
,
2
)
xrange
(
0
,
6
,
2
)
Step size of 2
list
(
xrange
(
0
,
6
,
2
)
)
[
0
,
2
,
4
]
Python built-in function