May-30-2017, 05:10 AM
xrange()
is built-in function that returns xrange object
, i.e. it will produce the same sequence as range()
, but without storing all of the elements in the memory. for short one it's the same, difference comes with large sequence. you can iterate over it yielding one number at a time or pass it as argument 'everywhere where iterator is expectedAs Larz60+ said in python3 it become range.
>>> for n in xrange(5): ... print n ... 0 1 2 3 4 >>> list(xrange(3)) [0, 1, 2] >>> [n**2 for n in xrange(4)] [0, 1, 4, 9]the docs are more or less clear