Python

NumPy Range

The essential Python library for numerical computing is called NumPy. The array type ndarray is the most crucial one. Numerous array construction methods are available in NumPy for various circumstances. One such function that uses numerical ranges is arange(). Given that np is a frequently used acronym for NumPy, it is sometimes referred to as np.arange(). In this article, we have given the syntax, parameters, and many ways to generate a NumPy array of the arange() function. Additionally, for the reader’s benefit, a variety of programming examples that employ the np.arange() function are also covered. Let’s get started.

NumPy Arange() Function

A range of numbers can be created as an array using the Python NumPy arange() function. Start, Stop, Step, and Dtype are the four arguments that this function requires. Range() only works with integers. On the other hand, arange() works with both int and float types. The syntax and parameters of arange() and range() in Python are similar to what you can find out in the following section.

Syntax of Np.Arange()

NumPy arange has a rather simple syntax. Like other NumPy functions, we can also call the function name and specify a set of parameters afterward.

You invoke the method with np.arange() if you import the NumPy into your environment as np. Then, there are 4 parameters inside the arange() function that you can change.

The range’s starting value is indicated by the “start” option. This option is optional, therefore if you don’t specify it, the default value is set to 0.

The range’s limit is indicated by the”’stop” option. As with any Python indexing, keep in mind that this value is not a part of the final range. Consequently, the series of values effectively include the stop value and continue up to it. Additionally, you must give a stop value because this option is necessary.

The distance between values in the sequence is specified by the “step” argument. This element is not required. If you don’t give any value to the “step” option, it will be set to 1 by default.

The data type is set by the “dtype” option. Numerous data types are available in Python and NumPy which can be used.

Now, you have some idea of the numpy.arange() function and its syntax. Let’s see some example programs to learn how the function works.

Example 1:

We’ll demonstrate how to build an easy NumPy arrange sequence in this example. Make sure that you import the NumPy module into your environment before you begin working through this example and the others that follow. Let’s now demonstrate the code to generate a NumPy array with seven values. The code demonstrates that we first import the NumPy module and then use the np.arrange() function to build an array which contains 7 entries.

You should keep in mind a few factors including the fact that we omit the start value. The sequence starts at “0” as a result. Second, the “7” acts as the stop point when we use the code stop = 7. In response, NumPy generates a sequence of numbers that ranges from 0 to this stop value but does not include it.

The values rise by “steps” of 1. This occurs as a result of the step parameter’s value not being specified. The value of the step argument defaults to 1 if not provided. The data type is also not specified, and Python determines the data type from the other arguments provided to the function.

import numpy as np

print(np.arange(stop=7))

You can see that the previous code is successfully executed in the following:

Another option is to simply leave the argument and not the parameter, like in the following example:

import numpy as np

print(np.arange(7))

Example 2:

We explained how to generate an array in the first example using a sequence of one. Now, we show the steps to create a sequence of numbers that increments by 2. In the code, we’re going to generate a range of numbers in step 2. It starts at 4 and ends at 20. We employ a start position of 4 and a stop position of 20 to accomplish this. We set the “step = 2” to increment the numbers in steps of 2. Look at the following code:

import numpy as np

res = np.arange(start = 4, stop = 20, step = 2)

print(res)

The previous code creates the array like this:

The code generates the NumPy array with the following values: 4, 6, 8, 10, 12, 14, 16, and 18. The start of the output range is 4. We did this by setting the start to 4. The output range then consists of values starting at 4 and increasing by 2 increments through 18: 4, 6, 8, 10, etc.

Because we set the stop value to 20, the range values end at 18. But keep in mind that numpy.arange() only creates a series up to the stop value. As a result, once arange() reaches 18, it stops working. According to the syntax, stop = 20, if it tries to increase by a step value of 2, it results in a value of 20 which should be ignored.

Example 3:

We’ll demonstrate how to count backward in the final example. To do so, you need to employ the “step” option. As we can see from the previous examples, the count is always carried out gradually from beginning to end. The “step” option here is set to a negative integer to count backward. It prints all the integers in the range with a decreasing step if we set it to -1.

It’s important to keep in mind that for this to work, the start value must be higher than the final value. The start value is set to 25 and the end value is set to 10 to return a list from 25 to 10 in reverse. The step is set to -1. Check out the following code:

import numpy as np

res = np.arange(25, 10, -1)

print(res)

In the screenshot below, a range of numbers from 25 to 11 can be seen

Conclusion

This article is defined the NumPy arange() function and demonstrated how to use it to generate an array with specified intervals. The arange() function syntax is also provided along with an explanation of each of its parameters. For you to comprehend the topic at hand and implement the idea in your programs with ease, we also included three separate examples with all of the necessary information.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content