• Post author:
• Post category:NumPy / Python

Python Numpy arange() function is used to create an array with regularly spaced values within a specified range. It is similar to the built-in `range()` function but returns a NumPy array. 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.

``````
# Quick examples of numpy arange() function

# Example 1: Use arange() with single argument
#  Generate array
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

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()` you can create a `ndarray` is one of the fundamental tools in NumPy. ndarray is obtained certain numerical ranges using the `numpy.arange()` function. You 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).

When you use `numpy.arange()` with a single argument, it creates an array with values ranging from 0 to the specified value (exclusive) with a default step of 1. For instance, `numpy.arange(9)` generates an array with values from 0 to 8 (inclusive), as the default starting value is 0, and the default step is 1.

``````
# Import numpy
import numpy as np

# Use numpy.arange() with single argument
# Generate the array
arr = np.arange(9)
print("Array:",arr)
``````

Yields below output.

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

You can use the `numpy.arange()` function to generate an array with values from 1 to 11 (12 exclusive). For example, `numpy.arange(1, 12)` generates an array with values from 1 to 11 (inclusive), as you specified the starting value as 1, and the default step is 1. The resulting array contains integers from 1 to 11.

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

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

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

You can use `numpy.arange()` to create an array with a custom step size. For example, `numpy.arange(3, 22, 4)` generates an array starting from 3 and ending at (but not including) 22, with a step size of 4. The resulting array contains values 3, 7, 11, 15, and 19, which are generated by incrementing the previous value by the specified step size. This demonstrates how you can use `numpy.arange()` to create arrays with custom step sizes.

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

# Output:
# Array: [ 3  7 11 15 19]

# Custom step-size
arr = np.arange(start=2, stop=22, step=3)
print("Array:",arr)

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

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

Alternatively, you can use `numpy.arange()` to generate an array with float values. For instance, `numpy.arange(1.0, 5.0, 0.5)` generates an array with values starting from 1.0 and ending at (but not including) 5.0, with a step size of 0.5. The resulting array contains floating-point numbers with increments of 0.5. Adjust the `start`, `stop`, and `step` values as needed for your specific use case.

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

# Output:
# Array with float values:
#  [1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
``````

## 7. Use Negative Arguments

Similarly, you can also use `numpy.arange()` to create an array with negative values. For example, `numpy.arange(-13, -2)` generates an array with values starting from -13 and ending at (but not including) -2, with a default step size of 1. The resulting array contains negative integers within the specified range. Adjust the start, stop, and step values as needed for your specific use case.

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

# Output:
# Array with negatives values:
#  [-13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3]

# Providing negative arguments
arr = np.arange(-13, -2, 3)
print("Array with negatives values:\n",arr)

# Output:
# Array with negatives values:
# [-13 -10  -7  -4]
``````

## Frequently Asked Questions

What does the arange() function do in NumPy?

The `arange()` function in NumPy is used to create an array with regularly spaced values within a specified range. It is similar to the `range()` function in Python but returns a NumPy array.

How to create an array with integer values using arange()?

To create an array with integer values using `numpy.arange()`, you can specify the start, stop, and optionally, the step size. For example, `np.arange(5)` generates an array with values from 0 to 4 (exclusive) with a default starting value of 0 and a default step size of 1

How to create an array with float values using arange()?

To create an array with float values using `numpy.arange()`, you can provide float arguments for the start, stop, and optionally, the step size. For example, `np.arange(1.0, 5.0, 0.5)` generates an array with float values starting from 1.0 and ending at (but not including) 5.0, with a step size of 0.5. You can adjust the start, stop, and step values to create an array with the desired range and precision.

Can I use negative values with arange()?

You can use negative values with `numpy.arange()`. You can specify negative values for the start, stop, and step arguments as needed. For example, `np.arange(-5, 1, 1.5)` generates an array starting from -5 and ending at (but not including) 1, with a step size of 1.5.

How do I specify the data type of the array?

You can specify the data type of the array created by `numpy.arange()` using the `dtype` parameter. For example, `np.arange(0, 5, dtype=float)` generates an array with float values, as the `dtype` parameter is set to `float`. You can replace `float` with other NumPy data types like `int`, `np.float64`, `np.int32`, and so on. If you don’t specify the `dtype`, NumPy will infer it based on the type of the input arguments.

Is the stop value included in the generated array?

The stop value is not included in the generated array when using `numpy.arange()`. The array includes values up to, but not including, the stop value.

## Conclusion

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

Happy Learning!!