DEV Community

xrange vs. range vs. range

Guy Goldberg on January 19, 2018

A simple question While I was teaching a class about iterators and generators, a student asked me a question that didn't seem so interes...
Collapse
 
msoedov profile image
Alex Miasoiedov

range vs range and only range since python2 will be not maintained really soon

Collapse
 
coolgoose profile image
Alexandru Bucur

Still more than 2 years away + legacy projects that benefit from the info :)

Collapse
 
msoedov profile image
Alex Miasoiedov

Legacy py2 projects are very unlikely will benefit from a nano performance optimization

Collapse
 
guy_gold profile image
Guy Goldberg

I suspect that indeed Python2 is going to be with us for much longer than 2 years...

Collapse
 
linehammer profile image
linehammer

Both range() and xrange() are built-in functions in Python that are used to generate integers or whole numbers in a given range . The python 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.

Deprecation of xrange()

In Python 3.x, the xrange() function does not exist anymore. The range() function now does what xrange() does in Python 2.x, so to keep your code portable, you might want to stick to using range() instead. Of course, you could always use the 2to3 tool that Python provides in order to convert your code, but that introduces more complexity.

net-informations.com/python/pro/xr...

Collapse
 
rohit254 profile image
Rohit Kumar

I don't study Python 2 but in Python 3 I remember that it has only xrange and range function.

I also need help to get review on my blog on pandas please help me it, here is my blog link - [surushatutorials.blogspot.com/2024...]