Home >Backend Development >Python Tutorial >Detailed explanation on range and xrange in python
Overview
xrange and range are basically used when looping.
The usage of xrange is exactly the same as range. The difference is that the generated object is not a list object, but a generator.
When generating a large number sequence, using xrange will have much better performance than range, because there is no need to open up a large memory space right away. So try to use xrange .
range ( [start,] stop [, step] )
>>> a = range(10) >>> type(a) <type 'list'> >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a[5]5
xrange ( [start,] stop [, step] )
>>> b = xrange(10) >>> type(b) <type 'xrange'> >>> b xrange(10) >>> b[5]5
The above is the detailed content of Detailed explanation on range and xrange in python. For more information, please follow other related articles on the PHP Chinese website!