What is the difference between range and xrange functions in Python 2.X?
In Python 2.X, range
and xrange
are used to generate a sequence of numbers. The main difference between the two is the way they generate and store the sequence of numbers.
range
generates the entire sequence of numbers in memory at once and returns a list, whereas xrange
generates the numbers one at a time, as they are needed, and returns an xrange object.
Here is an example of how to use range
and xrange
in Python 2.X:
# Using range
for i in range(10):
print(i)
# Using xrange
for i in xrange(10):
print(i)
In python 3 range()
behaves like xrange()
in python 2.