arange() vs range()
And I should be clear that Python’s built-in
range is actually an object, rather than a function like
arange() is. So, that’s going to be an important distinction between them as I move over into the terminal, and you can take a look at how these actually work in the real world.
And what is the return value of that function? Well, we can take a look at the type here by just doing—oh, I’m sorry, it doesn’t have an attribute
.type, I need to call the
type() function on it.
Now let’s take a look at
range, which is built into Python, so I don’t have to import anything to get at it.
range is a
<class 'range'>, and let’s take a look at the
type() of the thing that’s returned when you call
range(). You get a
<class 'range'>. So you just instantiate an object
However, as you can see from my REPL here, these parameters are required to be integers. They can’t be decimal numbers. That’s the first important difference between
arange(), is that
range() only works well with integers—in fact, only works at all with integers.
But it does have essentially the same characteristics as the
np.arange(). They work essentially the same way. That
start is where the range starts and
stop is where it stops. But if you’ll notice, when I do
for i in range(1, 10):
As you might notice as well, when I called
range() up here, I just got a
range back. I didn’t get an array. If you really want an array, you have to convert a
range into a
list. I’ll just do
So then that is a
list. But what does this mean for your actual code? Well, what it means is that when you use a
range, what’s happening under the hood is that as you iterate through a
range, the numbers are being generated on demand.
numpy.ndarray, so it’s a little different than just a
list, but still it returns this whole thing. So if you’re trying to iterate through a huge thing, if you want to say something like
for element in—well, actually not
element—it’ll just be
for i in range(), and then you need it to be, you know, this big—gigantic—but you know that you might break out of it for some reason, then it might be best for you to use a
range object because the numbers are generated on demand, and so you never have to create this whole list. However, this means that for each step in the iteration, you’re going to have to do more work because you have to generate the number as you go along.
I would encourage you to check out the article that this video course is based on because it has some really fun timing challenges that the author goes through to kind of show you the differences in performance between the two. But for a short little introduction, I’m not going to get into all that timing stuff and I’ll just say that if you need to work with numbers on demand and you are concerned that you might have issues with the memory,
range is a better choice if you can work with integers.
But if you need to work with floating-point numbers—so remember that you can do
1.0 and get floating-points—if you need to work with floating-point numbers or if you want pure speed, you’re probably going to want NumPy, because NumPy just has some really awesome optimizations, which increase the speed of manipulating list comprehensions and things like that much more effectively than the inbuilt
Become a Member to join the conversation.