Ask coding questions

← Back to all posts
Python: whtas the differnce betwen xrange and range?
georgegerdes (4)

I sa people talking bout xrange or range, and I wanted to know, what is th differnce? Can you use both?

Answered by qualladoom (320) [earned 5 cycles]
View Answer
qualladoom (320)

Hey georg, didn't we talk 2 minutes ago? anyway, here is what i found out for you:
range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. In Python 3, there is no xrange , but the range function behaves like xrange in Python 2.If you want to write code that will run on both Python 2 and Python 3, you should use range().

range() – This returns a list of numbers created using range() function.
xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called “lazy evaluation“.

i hope that helps you! Here is a link with more info about it:

georgegerdes (4)

@enigma_dev ok, that sems understandable. Thx!

murphybeck (0)

Both range() and xrange() are built-in functions in Python that are used to generate integers or whole numbers in a given range . The range() and xrange() comparison is relevant only if you are using both Python 2.x and Python 3 . It is because the range() function in python 3.x is just a re-implementation of the xrange() of python 2.x. It actually works the same way as the xrange does.