Updated March 30, 2023
Introduction to NumPy arange
The inbuilt function in Numpy which is used to create an array object in which the elements of the array are spaced evenly in a pre-defined interval is called arange function in Numpy. It takes the first parameter to define the beginning of the interval the second parameter to define the end of the interval and the third parameter to define the spacing between each element in the array and the array consisting of elements that are spaced evenly within the given interval as specified in the arange function is returned by this arange function in NumPy and by making use of arange function in NumPy, we can create an array object not only of integers but also of other data types.
The syntax for NumPy zeros in Python is as follows:
arange(start, stop, step)
where start specifies the beginning of the interval representing the first element of the array that is to be returned after using arange function,
stop specifies the end of the interval representing the last element of the array that is to be returned after using arange function and
step represents the spacing between each element in the array that is to be returned after using arange function.
Working of NumPy arange
- Whenever there is a need to create an array object in which the elements of the array are spaced evenly in a pre-defined interval, we make use of a built-in function in NumPy called arange function.
- The arange function takes the first parameter to define the beginning of the interval representing the first element of the array that is to be returned after using arange function.
- The arange function takes the second parameter to define the end of the interval representing the last element of the array that is to be returned after using arange function.
- The third parameter step represents the spacing between each element in the array that is to be returned after using arange function.
- By making use of arange function in NumPy, we can create an array object not only of integers but also of other data types.
Examples of NumPy arange
Here are the following examples mentioned below.
Example #1
Python program to demonstrate arange function to create an array consisting of elements in the interval one to ten and the spacing between each element is two:
#importing the package numpy
import numpy as num
#creating a variable arrayname to store the array created using Numpy arange function. Here we are creating an array which consists of ten elements with the first element of the array being 1 and the last element of the array being less than 11 and the spacing between each element is two
arrayname = num.arange(1, 11,2)
print("The array created by using arange function within the specified interval with specified spacing is:")
#the array aranged using NumPy arange function is displayed as the output on the screen
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of arange function. Then a variable called arrayname is created to store the array created using arange function. Here we are creating an array that consists of ten elements with the first element of the array being 1 the last element of the array is less than 11 and the spacing between each element is two using NumPy arange function. Then the array arange using the arange function is displayed as the output on the screen.
Example #2
Python program to demonstrate NumPy arange function to create an array consisting of elements in the interval two to four and the spacing between each element is zero point two:
#importing the package numpy
import numpy as num
#creating a variable arrayname to store the array created using Numpy arange function. Here we are creating an array which consists of ten elements with the first element of the array being 2.0 and the last element of the array is less than 4.0 and the spacing between each element is 0.2
arrayname = num.arange(2, 4, 0.2)
print("The array created by using arange function within the specified interval with specified spacing is:")
#the array aranged using NumPy arange function is displayed as the output on the screen
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of arange function. Then a variable called arrayname is created to store the array created using this function. Here we are creating an array that consists of ten elements with the first element of the array being 2.0 the last element of the array is less than 4.0 and the spacing between each element is 0.2 using arange function. Then the array arange using this function is displayed as the output on the screen.
Example #3
Python program to demonstrate arange function to create an array consisting of elements in the interval one to twenty and the spacing between each element is five:
#importing the package numpy
import numpy as num
#creating a variable arrayname to store the array created using Numpy arange function. Here we are creating an array which consists of four elements with the first element of the array being 1 and the last element of the array is less than 20 and the spacing between each element is 5
arrayname = num.arange(1, 20, 5)
print("The array created by using arange function within the specified interval with specified spacing is:")
#the array aranged using NumPy arange function is displayed as the output on the screen
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of arange function. Then a variable called arrayname is created to store the array created using arange function. Here we are creating an array that consists of ten elements with the first element of the array being 1 the last element of the array is less than 20 and the spacing between each element is 5 using arange function. Then the array aranged is displayed as the output on the screen.
Conclusion
In this tutorial, we understand the concept of NumPy arange in Python through definition, the syntax of NumPy arange, and working of arange in Python through programming examples and their outputs.
Recommended Articles
This is a guide to the NumPy arange. Here we discuss the Working of NumPy arange along with the examples and outputs. You may also have a look at the following articles to learn more –