Home  >  Article  >  Backend Development  >  Detailed explanation on range and xrange in python

Detailed explanation on range and xrange in python

黄舟
黄舟Original
2017-10-07 11:40:141320browse

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 .

Experiment

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

>>> a = range(10)
>>> type(a)
<type &#39;list&#39;>
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[5]5

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

>>> b = xrange(10)
>>> type(b)
<type &#39;xrange&#39;>
>>> 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn