Signup/Sign In

NumPy arange() function

In this tutorial, we will cover numpy.arange() function of the Numpy library which is used for array creation.

The NumPy arange() function is one of the array creation routines that is usually based on numerical ranges. This method basically creates an instance of ndarray with evenly spaced values and returns the reference to it.

  • In this function, you can define the interval of the values that are contained in an array, the space between them, and their type.

  • The values are usually generated using the half-opened interval: [Start, Stop). It usually means the interval includes the start but will exclude(not include) the stop.

  • There is an advantage of numpy.arange() over the normal built-in range() function that with the help of numpy.arange() we are allowed to generate sequences of numbers that are not integers.

Syntax of numpy.arange()

Below we have the required syntax to use this function:

numpy.arange(start, stop, step, dtype)  

Note: In the above syntax the first three parameters are used to determine the range of the values, while the fourth parameter is used to specify the type of the elements.

Parameters:

Let us now discuss the above-mentioned parameters of this function:

  • start
    This is an optional parameter used for indicating the start of the interval. The default value of this parameter is 0. This value is included in the interval.

  • stop
    This parameter is a number(integer or decimal) that is used to represent the value at which the interval ends excluding this value.

  • step
    This is an optional parameter indicating the step size of the interval and it is a number by which the interval values change.

  • dtype
    This option is used to indicate the data type of the numpy array items. The default value of this parameter is None.

Returned Values:

This method will return the array within the specified range.

Now it's time to cover a few examples.

Example 1: Creating a basic array

In the example given below, we are going to provide all the range arguments and will check the resulting output:

import numpy as np

a=np.arange(start=2, stop=12, step=2)
print("The Output is :",a)


The Output is : [ 2 4 6 8 10]

Example 2:

In the example given below we will provide only two range arguments and then check the result:

import numpy as np

a=np.arange(start=2, stop=12)
print("The Output is :",a)

In the above code snippet as we have not passed the step argument, thus it will take the default value of step i.e 1. Thus the output is as follows:


The Output is : [ 2 3 4 5 6 7 8 9 10 11]

Example 3: Using arange() with single Argument

You need to provide at least one argument to the arange() function. And it is recommended that the one argument you provide should be stop (instead of start) and then the method automatically takes the default value of start and step. Thus start will be taken as 0 and the step will be taken as 1. In the example given below, we will provide only one range argument and then check the result. The code snippet is as follows:

import numpy as np

a=np.arange(12)
print("The Output is :",a)


The Output is : [ 0 1 2 3 4 5 6 7 8 9 10 11]

Example 4: Providing Negative Arguments

In case we want to provide negative values for start or both start and stop, and have a positive value for the step argument, the arange()function will work normally with the negative values and will create an array with negative values. Let us now take a look at the code snippet given below:

import numpy as np

a=np.arange(-10, -1)
print("The output is:")
print(a)


The output is:
[-10 -9 -8 -7 -6 -5 -4 -3 -2]

Summary

This tutorial was all about numpy.arange() function which is the primary array creation routine in the Numpy library. We also covered its syntax, parameters as well as the value returned by this function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.