[Tutor] Why does counting to 20 million stress my computer?

Magnus Lyckå magnus at thinkware.se
Fri Jul 16 22:43:02 CEST 2004


At 08:01 2004-07-16 -0700, Dick Moores wrote:
>Now I'm wondering about a reason to use range(x) instead of xrange(x). 
>Could you give me an example? How can the list range(x) builds be used?

It's a normal Python list, right? Sometimes you *do*
want lists, right?

For instance, you can reverse a list, you can't reverse
an xrange object. (On the other hand, you can generate a
reversed xrange object from the start, such as with
"x = xrange(max-1, -1, -1)" but I think you get the point.

If you play around and experiment interactively with lists
generated from range() and xrange objects, you'll notice the
differences.

I assume that range() predates xrange() in the development of
Python. If that famous time machine really existed, I assume
range would work like xrange does, and if you really wanted a
list, you'd have to do "l=list(range(5))" or whatever.

Maybe that will happen in the mythical Python 3 version.


--
Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language 



More information about the Tutor mailing list