Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: What is the difference between range and xrange?
Answer:

Both methods are mainly used in Python to iterate the for loop for a fixed number of times. They differ only when we talk about Python versions.

The difference between range and xrange is as follows:

Range() methodXrange() method
The xrange() method is not supported in Python3 so that the range() method is used for iteration in for loops.The xrange() method is used only in Python version 2 for the iteration in loops.
The list is returned by this range() methodIt only returns the generator object because it doesn’t produce a static list during run time.
It occupies a huge amount of memory as it stores the complete list of iterating numbers in memory. It occupies less memory because it only stores one number at a time in memory.
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook