numpy.arange() in Python


This numpy.arange() function is used to generates an array with evenly spaced values with the given interval. Commonly this function is used to generate an array with default interval 1 or custom interval.

Basic Syntax

numpy.arange() in Python function overview
numpy.arange() in Python function overview

Following is the basic syntax for numpy.arange() function:

arange(start, stop, step, dtype)
Returns Array with evenly spaces values with the given interval.

The Parameters used are:

Parameter Description
start [Optional] provide the start of the interval range.
stop provide end of the interval range.
step [Optional] the spacing between two adjacent values.
dtype [Optional] dtype is the type of output array.

Returns

The arange() function returns the array of evenly spaced values.

Example

Following is the example for Numpy arange() function in Python:

import numpy as np

# generates aray from 0 to 8 with default interval 1 
print("Array = \n", np.arange(9), "\n") 
  
# generates array from 6 to 15 with default interval 1 
print("Array = \n", np.arange(6,15), "\n") 

# generates array from 5 to 50 with default interval 5
print("Array = \n", np.arange(5, 50, 5), "\n")

# generates aray from 0 to 8 with default interval 1 and respapes it 
print("Array = \n", np.arange(9).reshape(3,3), "\n") 

The output should be:

Array =
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Array =
[ 6, 7, 8, 9, 10, 11, 12, 13, 14]
Array =
[ 5, 10, 15, 20, 25, 30, 35, 40, 45]
Array =
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]