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 article, you’ll know:
- What
np.arange()
is - How to use
np.arange()
- How
np.arange()
compares to the Python built-in classrange
- Which routines are similar to
np.arange()
Let’s see np.arange()
in action!
Free Bonus: Click here to get access to a free NumPy Resources Guide that points you to the best tutorials, videos, and books for improving your NumPy skills.
Return Value and Parameters of np.arange()
NumPy arange()
is one of the array creation routines based on numerical ranges. It creates an instance of ndarray
with evenly spaced values and returns the reference to it.
You can define the interval of the values contained in an array, space between them, and their type with four parameters of arange()
:
numpy.arange([start, ]stop, [step, ], dtype=None) -> numpy.ndarray
The first three parameters determine the range of the values, while the fourth specifies the type of the elements:
start
is the number (integer or decimal) that defines the first value in the array.stop
is the number that defines the end of the array and isn’t included in the array.step
is the number that defines the spacing (difference) between each two consecutive values in the array and defaults to1
.dtype
is the type of the elements of the output array and defaults toNone
.
step
can’t be zero. Otherwise, you’ll get a ZeroDivisionError
. You can’t move away anywhere from start
if the increment or decrement is 0
.
If dtype
is omitted, arange()
will try to deduce the type of the array elements from the types of start
, stop
, and step
.
You can find more information on the parameters and the return value of arange()
in the official documentation.
Range Arguments of np.arange()
The arguments of NumPy arange()
that define the values contained in the array correspond to the numeric parameters start
, stop
, and step
. You have to pass at least one of them.
The following examples will show you how arange()
behaves depending on the number of arguments and their values.
Providing All Range Arguments
When working with NumPy routines, you have to import NumPy first:
>>>
>>> import numpy as np
Now, you have NumPy imported and you’re ready to apply arange()
.
Let’s see a first example of how to use NumPy arange()
:
>>>
>>> np.arange(start=1, stop=10, step=3)
array([1, 4, 7])
In this example, start
is 1
. Therefore, the first element of the obtained array is 1
. step
is 3
, which is why your second value is 1+3, that is 4
, while the third value in the array is 4+3, which equals 7
.
Following this pattern, the next value would be 10
(7+3), but counting must be ended beforestop
is reached, so this one is not included.
You can pass start
, stop
, and step
as positional arguments as well:
>>>
>>> np.arange(1, 10, 3)
array([1, 4, 7])
This code sample is equivalent to, but more concise than the previous one.
The value of stop
is not included in an array. That’s why you can obtain identical results with different stop
values:
>>>
>>> np.arange(1, 8, 3)
array([1, 4, 7])
This code sample returns the array with the same values as the previous two. You can get the same result with any value of stop
strictly greater than 7
and less than or equal to 10
.
However, if you make stop
greater than 10
, then counting is going to end after 10
is reached:
>>>
>>> np.arange(1, 10.1, 3)
array([ 1., 4., 7., 10.])
In this case, you get the array with four elements that includes 10
.
Notice that this example creates an array of floating-point numbers, unlike the previous one. That’s because you haven’t defined dtype
, and arange()
deduced it for you. You’ll learn more about this later in the article.
You can see the graphical representations of these three examples in the figure below: