Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

NumPy arange(): How to Use np.arange() (Overview)

NumPy is the fundamental Python library for numerical computing. Its most important type is an array type called ndarray. NumPy offers a lot of array creation routines for different circumstances. arange() is one such function based on numerical ranges. It’s often referred to as np.arange() because np is a widely used abbreviation for NumPy.

Creating NumPy arrays is important when you’re working with other Python libraries that rely on them, like SciPy, Pandas, Matplotlib, scikit-learn, and more. NumPy is suitable for creating and working with arrays because it offers useful routines, enables performance boosts, and allows you to write concise code.

By the end of this course, you’ll know:

  • What np.arange() is
  • How to use np.arange()
  • How np.arange() compares to the Python built-in class range
  • Which routines are similar to np.arange()

Let’s see np.arange() in action!

Download

Course Slides (.pdf)

260.2 KB

00:00 Hi there! In this lesson I’m going to take you through NumPy’s arange() function, its parameters, and how you might go about using it. So, what is the arange() function? Well, as you might guess from its name, it returns a range of numeric values, when you give it start and stop parameters, and then these values are evenly spaced by a step parameter.

00:23 Quite simply, it just gives you a range of evenly spaced numeric values with a certain start and stop. Let’s make it a little more clear by looking at the actual parameters.

00:33 This is directly from the NumPy documentation. It shows you that the arange() function takes in a start, a stop, a step, and then sometimes a dtype (data type). It returns a numpy.ndarray, which for all intents and purposes you can treat as a list when you’re using this function.

00:53 start is the number—and it can be integer or decimal—that defines the first value in the array. stop is the number that defines the end of the array, but it’s not included.

01:03 So if I were to use the arange() function with a start of 1 and a stop of 10, it would include numbers up to and including 9, but it would not include 10, the stop, because the stop is not included in the results. The step is the third numeric parameter, and that defines the difference between consecutive elements in the array.

01:26 This defaults to 1, so it defaults to integer ranges, but it can be any value—a floating-point or integer. It just says how far apart are these evenly spaced values.

01:37 Then finally, the dtype, the only non-numeric parameter, is the type of the elements of the output array. In the documentation, you’ll read that it defaults to None, and you’ll see that in the signature, but really that’s a bit of a misnomer because when the dtype is None, the arange() function itself attempts to figure out what the proper data type for your array should be based on the start, stop, and step values that you provide.

02:04 Really, it does default to None but when it’s None, the behavior is that arange() attempts to figure out the type for you, and this is perfect for most use cases.

02:14 Let’s take a look at the terminal and see how this is actually used in code. What you’ll first notice is that you have to import numpy in order to use arange(), and as convention in Python, you normally import numpy as np.

02:30 So np.arange()—you can see, it’s a built-in function. Unlike the built-in Python range object, this is a function, arange(), and it returns an array. So np.arange() and you can see here the parameters start, stop, step, and dtype.

02:47 As you can see, the only required one is the stop parameter, and I’ll tell you a little bit about why that is in a second. But to begin, I’m going to be very explicit about all of the arguments that I’m inputting here, and the data type I’m going to leave to be figured out by arange().

03:03 We’ll talk more about data types in the next lesson, but for now it’s not super important. So, as you can see, start of 1, stop of 10, but we don’t include the stop, and then the step size is 1, so [1, 2, 3, 4, 5, 6, 7, 8, 9].

03:21 Once you are a little more familiar with this, you can just say things like start, stop, and step without using any of the actual keywords, and then if you really want to get into it, you can remove the step parameter, because the step defaults to 1 and get the same thing. And then finally, you can even remove the start parameter, but the default start is actually 0. So this array is slightly different, but it defaults to starting at 0 and then just going to the stop value that you give it with a step of 1.

03:48 But if I wanted to have a different step, I could say step=3.

03:54 And again, it would include all of the values that would be included by the step, except any values greater than or equal to the stop, so 10 is not included in this array, and it would not have been even if we had said that the stop would be 12—it still would not be included, even though that would be the next logical step value.

04:15 And you can use floats here. So you can say “I want this to start at 1.0 and go to 10 with a step size of 1,” and that will return—and as you can tell by querying the data type—will return floats in that array, because it infers that you need a float here.

04:32 And so you can use floating-point steps—

04:35 I’ll delete this—and you can go up by .1, by .5, whatever you really need. You can go up by any floating-point number, of course.

04:45 But something interesting to note here is just that if I were to change the stop value here and make it 10.1 instead of 10,

04:54 then 10 would be included in the output, because the way that these numbers are generated is that the arange() function starts at 1 and keeps appending to its array—of course, it does it in a very efficient fashion, it’s not like just straight up appending to a list—but it starts at 1 and just appends values going up by the step until it reaches or goes over the stop value.

05:17 This could even be 10.0000001, and 10 would still be included because 10 is less than the stop. So that’s something that’s important to note. However, if for some reason you wanted to do this and you wanted to say that the start and the stop were equal, then you would just get an empty array at the end.

05:38 And that’s not specifically related to the stopping thing that I was talking about a second ago, but it’s kind of an interesting corollary. And can you guess what would happen if you made the step so big that it just jumped from the start all the way to the next thing? Well, of course, you would just get the start value and that’s it.

05:58 So you’ll always get the start value unless the start and the stop are exactly the same, in which case, since the stop is never included, you would just get an empty array. So it won’t return None, it won’t return an error—it’ll just return an empty array.

06:12 One last little wrinkle that’s cool to note is that if your start is higher than your stop and you provide a negative step, you can have an array that is a descending.

06:25 And so, just remember that again, your stop won’t be included. So if you want to get, like, 1 in this example, you’ll need to make the stop=0. But you can also make the stop=-10, and this works the other direction, too.

06:37 You can start with negative numbers, or you can go down to negative numbers. So, you know, the step could be 4, the stop could be 100, and the start could be -10, and you’d start at -10, go up by 4 until you got all the way to 100. So it’s a really flexible, versatile function, and it can return you a wide variety of evenly spaced numeric values.

07:00 That’s numpy.arange(). Hope you enjoyed.

Become a Member to join the conversation.