What is the difference between range and xrange functions in Python 2.X?

+1 vote

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about

for i in range(0, 20):
for i in xrange(0, 20):

Aug 22, 2018 in Python by bug_seeker
• 15,350 points
242 views

1 answer to this question.

0 votes

xrange only stores the range params and generates the numbers on demand. However the C implementation of Python currently restricts its args to C longs:

xrange(2**32-1, 2**32+1) # When long is 32 bits, OverflowError: Python int too large to convert to C long
range(2**32-1, 2**32+1) # OK --> [4294967295L, 4294967296L]

Note that in Python 3.0 there is only range and it behaves like the 2.x xrange but without the limitations on minimum and maximum end points.

answered Aug 22, 2018 by Priyaj
• 56,900 points

Related Questions In Python

0 votes
1 answer

What is the difference between str() and repr() functions in Python?

str() is mostly used to create output ...READ MORE

answered Jul 8 in Python by Arvind
• 2,960 points
79 views
0 votes
0 answers

What is the difference between 2*2 and 2**2 in python?

Is there a provision in python to ...READ MORE

Aug 23 in Python by Waseem
• 4,480 points
55 views
+1 vote
1 answer

What is the difference between print and return in python?

Return statements end the execution of a ...READ MORE

answered Aug 25 in Python by anonymous
125 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 283 views
+4 votes
6 answers
0 votes
1 answer

What is the difference between Python and IPython?

There are few differences between Python and ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,900 points
406 views
0 votes
1 answer

What is the difference between re.search and re.match?

The theoritical approach can be this way, re.match is ...READ MORE

answered Aug 10, 2018 in Python by Priyaj
• 56,900 points
922 views