arange() vs range()
00:00
In this video, I’m going to cover the differences and the similarities between the numpy.arange()
function and Python’s built-in range
object.
00:11
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.
00:26
So, as always, import numpy as np
. First, let’s take a look just quickly at what these two different things actually are. np.arange()
is a function arange
.
00:41
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.
00:52
That’s an important pitfall to watch out for. The return value of the arange()
function is a class, an object numpy.ndarray
. So you call arange()
and you get an array out of it.
01:06
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 range
.
01:25 So that’s a really important distinction, and it gives some kind of illumination as to the differences between these two objects. But I’m getting a little ahead of myself.
01:33
Let me just show you how range
works. So, a range()
also has a start
, a stop
, and a step
.
01:40
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 range()
and arange()
, is that range()
only works well with integers—in fact, only works at all with integers.
01:57
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):
print(i)
,
02:18
again, you do not include the stop
value. So essentially, the same philosophy as to the arguments—it’s just range()
only allows integers.
02:27
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 10
here.
02:42
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.
02:58
So range()
here does not have an internal list
and it doesn’t return an array like the np.arange()
does. np.arange()
actually has that array and that is what it returns.
03:14
It’s a 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.
03:58
And so np.arange()
is much faster in creating sequences because of the way that the np.ndarray
class is actually implemented and works under the hood.
04:14
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.
04:42
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 range
.
05:06
So. Those are really the big differences between range
and arange()
. They return different kinds of objects and those different kinds of objects work better in different situations.
05:15 I hope you enjoyed this tutorial and use those really effectively.
Become a Member to join the conversation.