YouTip LogoYouTip

Python Func Xrange

Python xrange() Function |

-- Learning not just technology, but dreams!

Python Basic Tutorial

Python Basic Tutorial Python Introduction Python Environment Setup Python Chinese Encoding Python VS Code Python Basic Syntax Python Variable Types Python Operators Python Conditional Statements Python Loop Statements Python While Loop Statement Python for Loop Statement Python Nested Loops Python break Statement Python continue Statement Python pass Statement Python Number Python Strings Python Lists Python Tuples Python Dictionary Python Date and Time Python Functions Python Modules Python File I/O Python File Methods Python Exceptions Python OS File/Directory Methods Python Built-in Functions

Python Advanced Tutorial

Python Object-Oriented Python Regular Expressions Python CGI Programming Python MySQL Python Network Programming Python SMTP Python Multithreading Python XML Parsing Python GUI Programming (Tkinter) Python2.x vs 3.x Differences Python IDE Python JSON Python AI Drawing Python 100 Examples Python Quiz

Python OS File/Directory Methods

Python Object-Oriented

Python2.x Python xrange() Function

Python Built-in Functions Python Built-in Functions


xrange() is a function in Python 2 used to generate a sequence of numbers.

The usage of the xrange() function is identical to range, except that it generates a generator instead of an array.

Note: In Python 3, xrange() has been removed, the range() function has been improved, and it possesses all the functionality and features of xrange(). In other words, the range() function in Python 3 lazily generates sequences, so xrange() is no longer needed.

Syntax

xrange syntax:

xrange(stop)
xrange(start, stop[, step])

Parameter explanation:

  • start: The count starts from start. By default, it starts from 0. For example, xrange(5) is equivalent to xrange(0, 5)
  • stop: The count ends at stop, but does not include stop. For example: xrange(0, 5) is [0, 1, 2, 3, 4], without 5
  • step: The step value, default is 1. For example: xrange(0, 5) is equivalent to xrange(0, 5, 1)

Return Value

Returns a generator.

Example

The following examples demonstrate the usage of xrange:

>>>xrange(8)
xrange(8)
>>>list(xrange(8))
[0, 1, 2, 3, 4, 5, 6, 7]
>>>range(8) # range usage
[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 is 2
>>>list(xrange(0,6,2))
[0, 2, 4]
>>>

Python Built-in Functions Python Built-in Functions

← Python Func ComplexPython Func Getattr β†’