# NumPy: numpy.arange() function

## numpy.arange() function

The numpy.arange() function is used to generate an array with evenly spaced values within a specified interval. The function returns a one-dimensional array of type numpy.ndarray.

Syntax:

`numpy.arange([start, ]stop, [step, ]dtype=None)` Parameters:

Name Description Required /
Optional
start Start of interval. The interval includes this value. The default start value is 0. Optional
stop End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out. Required
step Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given. Optional
dtytpe The type of the output array. If dtype is not given, infer the data type from the other input arguments. Optional

Return value:

arange : ndarray - Array of evenly spaced values.
For floating point arguments, the length of the result is ceil((stop - start)/step). Because of floating point overflow, this rule may result in the last element of out being greater than stop.

Example: arange() function in NumPy.

``````>>> import numpy as np
>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(5.0)
array([ 0.,  1.,  2.,  3.,  4.])
``````

In the above example the first line of the code creates an array of integers from 0 to 4 using np.arange(5). The arange() function takes only one argument, which is the stop value, and defaults to start value 0 and step size of 1.
The second line of the code creates an array of floating-point numbers from 0.0 to 4.0 using np.arange(5.0). Here, 5.0 is provided as the stop value, indicating that the range should go up to (but not include) 5.0. Since floating-point numbers are used, the resulting array contains floating-point values.
Both arrays have the same length and contain evenly spaced values.

Pictorial Presentation:  Example: Numpy arange function with start, stop, and step arguments

``````>>> import numpy as np
>>> np.arange(5,9)
array([5, 6, 7, 8])
>>> np.arange(5,9,3)
array([5, 8])
``````

In the above code 'np.arange(5,9)' creates an array of integers from 5 to 8 (inclusive).
In the second example, 'np.arange(5,9,3)' creates an array of integers from 5 to 8 (inclusive) with a step size of 3. Since there is no integer between 5 and 8 that is evenly divisible by 3, the resulting array only contains two values, 5 and 8.

Python - NumPy Code Editor: