NumPy Tutorial NumPy Statistics NumPy References

NumPy - arange() function



The NumPy arange() function returns evenly spaced values within a given interval. The values are generated in the range [start, stop) with specified step size.

Syntax

numpy.arange(start, stop, step, dtype=None)

Parameters

start Optional. Specify start of the interval (inclusive). The default value is 0.
stop Required. Specify end of the interval (exclusive).
step Optional. Specify the step size. The default value is 1.
dtype Optional. Specify the type of the output array. If dtype is not given, infer the data type from the other input arguments.

Return Value

Returns an array of evenly spaced values.

Example: creating array

In the example below, the function is used to create a 1-D array using arange() function.

import numpy as np

#creating array using range [10,55) 
#and step size = 5
Arr1 = np.arange(10,55,5)
print("Arr1 is:", Arr1)

#creating array using range [10,16) 
#and step size = 1 and dtype=float
Arr2 = np.arange(10,16,dtype=float)
print("Arr2 is:", Arr2)

The output of the above code will be:

Arr1 is: [10 15 20 25 30 35 40 45 50]
Arr2 is: [10. 11. 12. 13. 14. 15.]

Example: creating matrix

With the use of reshape function, the array can be converted into matrix. Consider the following example.

import numpy as np

#creating array using range [10,55) 
#and step size = 5
Arr = np.arange(10,55,5)
print("Arr is:", Arr)

#reshaping the Arr
Arr = Arr.reshape(3,3)
print("\nArr is:")
print(Arr)

The output of the above code will be:

Arr is: [10 15 20 25 30 35 40 45 50]

Arr is:
[[10 15 20]
 [25 30 35]
 [40 45 50]]

❮ NumPy - Functions

5