# How to use Python NumPy arange() Function

• Post author:
• Post category:NumPy / Python

Python NumPy `arange()` function is used to create an array with evenly spaced values within a given interval. This function takes four parameters start, stop, step, dtype. The arange() and Python range() contain the same similarities like syntax and parameters but range() works only with integers, it doesn’t support the float type whereas arrange() supports both int and float types.

In this article, I will explain the NumPy `arange()` function using its syntax, parameters, and how to create a NumPy array in several ways with examples. For more examples of NumPy, refer to  NumPy Tutorial.

## 1. Quick Examples of NumPy arange() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy `arange()` function.

``````
# Below are a quick example

# Example 1: Generate array Use arange() with single argument
arr = np.arange(9)

# Example 2: Get the array Use arange() function
arr = np.arange(1, 12)

# Example 3: Get the array Use arange()
# with custom step-size
arr = np.arange(3, 22, 4)

# Example 4: custom step-size
arr = np.arange(start=2, stop=22, step=3)

# Example 5: Get the array with float values
arr = np.arange(1.5, 5.5, 0.5)

# Example 6: Get the array with negatives
arr = np.arange(-13, -2)

# Example 8: providing negative arguments
arr = np.arange(-13, -2, 3)
``````

## 2. Syntax of NumPy arange()

Following is the syntax of the numpy.arange() function.

``````
# Syntax of arange()
numpy.arange([start, ]stop, [step, ]dtype=None)
``````

### 2.2 Parameters of arange()

Following are the parameters of the arange() function.

• `start` – This is an optional parameter used to specify the start of the interval. Its default value is 0. It includes in the interval.
• `stop` – This parameter is used to specify the end of the interval. The interval does not include this value.
• `step` – This is an optional parameter indicating the step size of the interval. Its default value is 1.
• `dtype` – This Parameter is optional and controls the data type of the resultant Numpy array. The default value of this parameter is None.

### 2.3 Return Value of arange()

It returns a `ndarray` of evenly spaced values. If the array returns floating-point elements the array’s length will be `ceil((stop-start)/step)`.

`Note`: arange() never includes the stop number in its result.

## 3. Usage NumPy arange() Function

The NumPy library has various numeric and mathematical tools to operate on multi-dimensional arrays and matrices. Using `arange()` we can create a `ndarray` is One of the fundamental tools in NumPy. Ndarray is obtained certain numerical ranges using the `NumPy.arange()` function. we can define the interval of the values contained in an array, space between them, and their type with four parameters of `arange()`.

The main difference between range() and arange() is that `range` is a built-in Python class, while `arange()` is a function that belongs to a third-party library (NumPy).

To pass one parameter to the `numpy.arange()` function, by default, considers the value to be the stop parameter. Thus by default start value is `0` and the step value is `1`. For example,

``````
import numpy as np
# Use numpy.arange() with single argument
# generate the array
arr = np.arange(9)
print(arr)

# Output
# [0 1 2 3 4 5 6 7 8]
``````

## 4. Use numpy.arange() Function to Generate NumPy Array

Create ndarray by passing two parameters that start and stop to the `numpy.arange()`. The step parameter is not passed to this function, by default, considers it to have a value of 1. You can see that even though you didn’t pass the step parameter, the array was created with the values of `[1 2 3 4 5 6 7 8 9 10 11]`.

``````
# Use numpy.arange() function
# Get the array
arr = np.arange(1, 12)
print(arr)

# Output
# [ 1  2  3  4  5  6  7  8  9 10 11]
``````

## 5. Use arange() to Custom Step-Size

As we know step value in the arange() takes default value 1. We can customize the step value by passing it as an argument. For example, create a NumPy array whose elements are between 3 to 22(exclusive) having an interval of 4.

``````
#  Use numpy.arange() to custom step-size
arr = np.arange(3, 22, 4)
print(arr)

# Output
# [ 3  7 11 15 19]

# custom step-size
arr = np.arange(start=2, stop=22, step=3)
print(arr)

# Output
# [ 2  5  8 11 14 17 20]
``````

## 6. Get Array with Float values using arange()

Pass float numbers to its start, stop, and step argument to create an array with float values. For example, `np.arange(1.5, 5.5, 0.5)` will return the sequence of floating-point numbers starting from 1.5 up to 5.5.

``````
# Get the array with float values
arr = np.arange(1.5, 5.5, 0.5)
print(arr)

# Output
# [1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
``````

## 7. Use Negative Arguments

As we know from the above, we have created ndarrays with evenly spaced values, on the other hand, we can create ndarray with negative values. For that, we have to provide negative values of start and stop. This function will work normally with the negative values and will create an array with negative values. For example,

``````
# Get the array with negatives values
arr = np.arange(-13, -2)
print(arr)

# Output
# [-13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3]

# providing negative arguments
arr = np.arange(-13, -2, 3)
print(arr)

# Output
# [-13 -10  -7  -4]
``````

## 8. Conclusion

In this article, I have explained how to use` arange()` function and using this how we can create an array with specified intervals with example.

Happy Learning!!

## Reference 