Numpy.arange() method returns an array with evenly spaced elements as per the interval. The interval mentioned is half-opened, i.e., [Start, Stop).
Syntax
numpy.arange(start, stop, step, dtype)
Parameters
- start (optional, number): Start of an interval. The default start value is 0.
- stop: number. End of the interval.
- step (number, optional): The step can’t be zero. Otherwise, you’ll get a ZeroDivisionError.
- dtype: The type of an output array. If the dtype is not given, infer the data type from the other input arguments.
Return Value
It returns an array.
Example 1: Basic usage of np.arange()
import numpy as np
print(np.arange(4), "\n")
Output
[0 1 2 3]
Example 2: Specifying start and stop
import numpy as np
arr = np.arange(18, 22)
print(arr)
Output
[18 19 20 21]
Example 3: Specifying step size
import numpy as np
arr = np.arange(17, 22, 2)
print(arr)
Output
[17 19 21]
Example 4: Passing ‘float’ arguments
import numpy as np
arr = np.arange(2, 11.1, 3)
print(arr)
Output
[ 2. 5. 8. 11.]
Example 5: Creating a multi-dimensional array
import numpy as np
print(np.arange(4).reshape(2, 2), "\n")
Output
[[0 1]
[2 3]]
That’s it.