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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

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

Give Feedback

You now know how to use NumPy arange(). The function np.arange() is one of the fundamental NumPy routines often used to create instances of NumPy ndarray. It has four arguments:

  1. start: the first value of the array
  2. stop: where the array ends
  3. step: the increment or decrement
  4. dtype: the type of the elements of the array

You also learned how NumPy arange() compares with the Python built-in class range when you’re creating sequences and generating values to iterate over.

You saw that there are other NumPy array creation routines based on numerical ranges, such as linspace(), logspace(), meshgrid(), and so on.

Download

Course Slides (.pdf)

260.2 KB

00:00 This is just going to be a quick conclusion video to this course on NumPy’s arange() function. If you remember from the very first video in this course, the arange() function is a useful way to get evenly spaced ranges of numeric values.

00:15 That means integer or floating-point values returned in a numpy.ndarray, and you provide the start, stop, and step parameters, and the arange() function then generates this range of evenly spaced values for you, with an optional dtype (data type) argument. And those data types, as I talked about, can be essentially various sizes of integer and floating-point numbers, which might allow you faster or more efficient performance in certain applications like machine learning or image processing, where images or sets of data that you’re working with are very large and it might be more efficient to encode them in a smaller data size.

00:56 I talked about the comparison with the inbuilt range object.

01:01 The arange() is better than Python’s inbuilt range for work that requires a lot of manipulation of the resulting sequences. So, comprehensions and performing NumPy operations on a NumPy array will normally be faster than with a range object, and you get the advantage of working with floating-point numbers, which the inbuilt range does not allow you to do.

01:22 But the inbuilt range can be better for looping, especially if early exiting is possible, because a range generates values lazily, rather than the arange() function, which generates values all at once into an array.

01:36 I also covered some other NumPy functions like linspace() or geomspace() or meshgrid(), which have similar syntax and output and can be used for really similar purposes, just with slight idiosyncrasies or different philosophies for how to actually generate those array numbers. I hope you found this useful, and if you use NumPy in other work, I strongly suggest that you use arange() in your work because it shares that kind of characteristic NumPy efficiency and philosophy, which really can help speed up your data science applications and anything that requires heavy number processing.

02:12 So. I hope you enjoyed this course and come back for the next one. Thank you very much.

Become a Member to join the conversation.