NumPy | arange method
Start your free 7-days trial now!
NumPy's arange(~)
method is used to create a NumPy array with equally spaced values, similar to Python's range(~)
method.
Parameters
1. start
| number
| optional
The starting value. This will be the first element in the NumPy array.
2. stop
link | number
The end value. Just like Python's range(~)
method, the interval does not include this value.
3. step
link | number
| optional
Spacing between values. By default, step=1
.
4. dtype
link | string
or type
| optional
The desired data type for the NumPy array. By default, the type is inferred based on the other parameters.
Return value
A NumPy array with equally spaced values.
Examples
Specifying only the end value
To obtain a NumPy array from 0 (inclusive) to 3 (exclusive):
np.arange(3)
array([0, 1, 2])
Notice how the value 3, which is the stop
parameter, is excluded from the NumPy array.
Specifying start and end value
To obtain a NumPy array from 2 (inclusive) to 5 (exclusive):
np.arange(start=2, stop=5)
array([2, 3, 4])
Specifying step size
To obtain a NumPy array where the values are evenly spaced by 3 units:
np.arange(1, 10, 3)
array([1, 4, 7])
Specifying a negative step size
np.arange(6, 2, -1)
array([6, 5, 4, 3])
Specifying dtype
To create a NumPy array of floats starting from 2.0 (inclusive) and ending at 5 (exclusive):
np.arange(start=2, stop=5, dtype="float")
array([2., 3., 4.])