Post: Python- range vs arange vs xrange ?
08-23-2011, 08:16 AM #1
Hbhgahbf
Save Point
(adsbygoogle = window.adsbygoogle || []).push({}); Hi. So I've been trying to teach myself some python basics because I will be taking a class where I need to know some python beforehand....

and I keep coming across range, arange, and xrange but I haven't really noticed what the difference is between these. It seems they do the same thing?

So can someone explain to me the differences between them and why I would want to use one over another and in what circumstances?


Thank you!
(adsbygoogle = window.adsbygoogle || []).push({});
08-23-2011, 11:47 AM #2
schnzrs
Do a barrel roll!
The range function returns a list of numbers between the two arguments (or one) you pass it.

For example, range(2) would return -> [0, 1]
range(0, 2) would return -> [0, 1] also

The xrange function is the same but returns an object. The benefit in this is much less memory used.

For example, xrange(2) would return -> xrange object
list(xrange(2)) would return -> [0, 1]

If you use both in a for loop, the result would be the same, except xrange would use less memory.

I would not worry about arange, it is used in numpy and returns an array instead of a list. It is pretty much a range function.

The following user thanked schnzrs for this useful post:

Epic?
08-23-2011, 09:21 PM #3
uhhh wutttttt

The following user groaned I SmG I for this awful post:

Epic?
08-23-2011, 09:39 PM #4
Originally posted by I
uhhh wutttttt

No ofennse, but don't go post bosting now! :dumb:
Well your new...but still for future refernce buddy =D

The following user thanked Nick_Buckeyes for this useful post:

Epic?
08-24-2011, 05:12 PM #5
post bosting???

---------- Post added at 01:12 PM ---------- Previous post was at 01:11 PM ----------

dont assume shit that isnt true . k thanks.

The following user groaned I SmG I for this awful post:

Epic?

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo