 Python

# How to Use Python NumPy arange() Function

Many functions exist in the Python NumPy library to perform different types of numerical and scientific operations. Creating different types of arrays for various purposes is one of the practical uses of the NumPy library. Python has a built-in function named arange() to create a list of sequential numbers. arange()  is one of the array creation functions of the NumPy library to create an array of numeric ranges. The uses of the NumPy arange() function have explained in this tutorial.

## Syntax

np.array  np.arange([start, ]stop, [step, ], dtype=None)

This function can take four arguments. The start argument is optional that defines the starting value of the array. If the start value is used in the function, then the mandatory argument, stop, will require defining the ending value of the array. The step argument is optional that defines the difference between the elements. The default value of the step is 1, and this argument’s value can’t be 0. The fourth argument, dtype, defines the array element’s data type, and the default value of this argument is None. This function returns an array object based on the argument values.

## Use of arange() function

You have to install the python NumPy library before practicing the examples of this tutorial. The uses of arange() function with one argument, two arguments, and three arguments have been shown in this section of this tutorial by using multiple examples.

## Example-1: Use of arange() function with one argument

When the arange() function of the NumPy library is used with one argument, then the array’s upper value is set as the argument value. The following script will create a NumPy array of range values and print the different array attributes and array values. 12 has been used as the argument value of the arange() function that will create a NumPy array of 12 elements that will start from 0 and end to 11. Next, the dimension, size, and data type of the array will be printed. The array values will be printed later.

# Import NumPy

import numpy as np

# Create a one-dimensional NumPy array of sequential numbers

np_array = np.arange(12)

# Print the different attributes of the NumPy array

print('The dimension of the array: ', np_array.ndim)

print('The length of the array: ', np_array.size)

print('The data type of the array: ', np_array.dtype)

# Print the values of the NumPy array

print('Array values are:', np_array)

Output:

The following output will appear after executing the script. ## Example-2: Use of arange() function with two arguments

When the arange() function is used with two arguments, then the array’s starting and ending values will be set as the argument values. The following example shows how to create an array with the lower and upper values by using arange() function. The first array is created by setting 10 in the start value and 25 in the stop value. An array of 15 sequential integer number will be created. Next, two attributes and array values will be printed. The second array is created by setting 0.5 in the start value and 5.5 in the stop value. An array of 5 sequential floating number will be created. The same attributes and the values of this array will be printed later.

# Import NumPy  library

import numpy as np

# Create a NumPy array of integer numbers with the start and end values

np_array1 = np.arange(10, 25)

# Print different attributes of the array

print('The size of the array: ', np_array1.size)

print('The data type of the array: ', np_array1.dtype)

# Print the values of the NumPy array

print('Array values are:', np_array1)

# Create a NumPy array of float numbers with the start and end values

np_array2 = np.arange(0.5, 5.5)

# Print different attributes of the array

print('\nThe size of the array: ', np_array2.size)

print('The data type of the array: ', np_array2.dtype)

# Print the values of the NumPy array

print('Array values are:', np_array2)

Output:

The following output will appear after executing the script. ## Example-3: Use of arange() function with three arguments

The following example shows the use of arange() function with three arguments. 10 is set for the start argument, 20 is set for the stop argument, and 2 is set for the arange() function’s step argument value. It will create an array of 5 integer values. The size, data type, and values of the array will be printed as the output.

# Import Numpy

import numpy as np

# Create a NumPy array of integer numbers with the start, end, and step values

np_array = np.arange(10, 20, 2)

# Print different attributes of the array

print('The size of the array: ', np_array.size)

print('The data type of the array: ', np_array.dtype)

# Print the values of the NumPy array

print('Array values are:', np_array)

Output:

The following output will appear after executing the script. ## Example-4: Use of arange() function with the negative argument values

The following example shows the use of arange() function with the negative argument values. 50 is set for the start argument, 20 is set for the stop argument, and 2 is set for the arange() function’s step argument value. It will create an array of 9 negative integer numbers. The size, data type, and values of the array will be printed as the output.

# Import Numpy

import numpy as np

# Create a NumPy array of negative numbers with the start, end, and step values

np_array = np.arange(-50, -5, 5)

# Print different attributes of the array

print('The size of the array: ', np_array.size)

print('The data type of the array: ', np_array.dtype)

# Print the values of the NumPy array

print('Array values are:', np_array)

Output:

The following output will appear after executing the script. ## Conclusion

The ways of creating a NumPy array by using arange() function have been described in this tutorial by using multiple examples. The array creation with the sequential positive and negative numbers by using this function have shown here. I hope the purpose of using the arange()  function will be cleared for the readers after reading this tutorial. 