Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson


00:00 In this video, I’m going to cover how to manipulate the ranges returned by NumPy’s arange() function. As always, before you use this function you’re going to have to import numpy, and it’s preferable to do it as np, just as a convention.

00:16 You’ll remember from previous videos that I can use any of these parameters start, stop, step, and dtype, but for simplicity I’m just going to say an arange() generated with 5.

00:27 And you’ll remember that starts at 0 by default and counts up by the default step size of 1 all the way up until, but not including, the stopso 0 through 4.

00:38 The great thing about what arange() returns is that it is—and I’m going to show you what it is—it has the type of a numpy.ndarray. And ndarray is awesome because you can perform operations directly on the array and they’ll be performed element-wise on the elements of the array. So 2 ** x, the ndarray(x), gives us an array which is each element in x, but 2 raised to the power of that element. So 2 ** 0 is 1, 2 ** 1 is 2, and so on. So it’s pretty cool.

01:15 And I could also say x to the power of 2—squared—and I get each element in here squared, which is pretty cool. And then you can do essentially any math operation.

01:27 You can do x / 3 and get these floating-point numbers, which are the result of division. You can say x + 10, you could get that.

01:35 So it’s really pretty much unlimited the things that you can do there.

01:39 You can also use NumPy functions. So if for example, I’m going to make a new arange() here, and I’m going to start at -10, I’m going to go to 10, and I’m going to go by .5, just to increase the diversity of the numbers that we have in here. So here you go, starting at -10 and going all the way up to 9.5.

01:58 So you can do np.abs(), which is the absolute value of x. It gives you the absolute value of all these numbers. You can do np.sin(), or even cos() (cosine) or whatever, and get the sine values of all these. Pretty amazing, and it makes it really easy, for example, if you want to create a plot using a library like Matplotlib or something like that, because you can just generate all of these values and get their results super, super quickly and easily.

02:28 If you need your arange() to have a different shape, for example if you want to have, this has—I think I can query the .shape here.

02:36 So the .shape, this means it has nine columns and zero—or just one row.

02:43 If you want it to have a different shape, you could say x.reshape(), I could say 3 by 3, and it would give me a 3 by 3 array, but arranged this way.

02:57 So one, two, three; one, two, three; one, two, three; so on. You can reshape it into essentially whatever you want. If you want to generate some kind of wacky, giant array with a really complex shape, but you know that the numbers in it form a kind of linear pattern, you can use arange() and then .reshape() just as needed.

03:16 So x.reshape(), and I’m just going to show you that it does indeed generate a different shape. .shape, and it goes 3 by 3, et cetera.

03:25 Really simple and really, really cool, which is nice. NumPy is an awesome library in general, and I encourage you to check out NumPy as a whole. There are many Real Python tutorials on it, many worthwhile videos to watch and lessons to read. So go check that out, but in the meantime, feel free to use this arange() function and reshaping it however you can.

Avatar image for B S K Karthik

B S K Karthik on March 25, 2020

Hi, what is the editor and background colour used in the tutorials.

Thank you, Karthik

Avatar image for Liam Pulsifer

Liam Pulsifer RP Team on March 25, 2020

Hi Karthik – I’m using ptpython in the Hyper terminal (default Hyper background color on MacOS I think, but it might be a solarized light theme). The syntax highlighting is the xcode theme on ptpython.

Avatar image for B S K Karthik

B S K Karthik on March 26, 2020

Thank you

Become a Member to join the conversation.