Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python xrange() Function

Python xrange() Function

Python built-in function Python built-in 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:

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 Python built-in function