In this Python article, we’ll learn about the specified Python NumPy arange function. We’ll understand the working of this NumPy function with the help of easy Python code examples.
What is Python NumPy arange Function?
It’s like the normal Python range function but it returns an array and not a list. The arange() function creates/returns an array with some specific distance of data items and can assign a different datatype to the resulting array items.
Syntax of arange Function
numpy.arange(start, stop, step, dtype=None)
- The start parameter is optional. If we don’t provide any value to it, then by default 0(zero) will be used.
- The stop parameter’s value will not be included in the resulting array. For instance, if we have a stop value of 15 then the array will range to 14. We can also take it like stop – 1.
- The step parameter defines the size between each item of the array. For instance, if the step size is 2 and the value starts from 1 to 8 then the array will be 1, 3, 5, 7. If not specified, then by default 1 is used. It also means that specifying this parameter is also optional.
- The type specifies the output array items datatype. It’s also optional. If no data is provided, then the datatype will be specified using the datatype of items.
5 Code examples of arange() NumPy Function
Let’s now understand the arange() function with code examples. For that, we’ll first import Python NumPy. See below:
import numpy as np
Ex.1 NumPy Array with no Starting Point(Stop)
We’ll just provide one parameter to the arange function which will take it as an ending point. See below:
array([0, 1, 2, 3, 4])
Not providing it a starting point results in the range starting from 0(zero). Also, the end is specified as end -1. It means the range will be one value less than the ending point/value.
Ex.2 Simple NumPy Array(Start, Stop)
Let’s create a simple array that ranges from 1 to 10 using arrange(). See below:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
This is the simple start and end arange() function that has returned an array. We can see that the last item was not taken. Understand it like this, 1 has been removed (11-1) from the last/end item.
Ex.3 Step NumPy Array(Start, Stop, Step)
Let’s give it a step of 2 so that all the items will have a space difference of 2. See the below code:
array([2, 4, 6])
The items have a distance of 2 means 2 is taken, then jump to 4, and then jumpy to 6, and so on until the range ends. We can also see that 8 is not taken as the range ends at 7. Try it with some other step values as well.
Ex.4 Items Datatype of Resulting Array(dtype)
We can change/modify the datatype of the returned array items. Let’s change the integer items datatype to float. See the below example:
array([1., 2., 3., 4., 5., 6., 7., 8.])
The type of the resulting array is changed from integer to floating point using the type parameter of arange function.
Ex.5 Step having Float Datatype
We can pass a float datatype to the step parameter as well. See below:
array([2. , 3.3, 4.6])
It’s making the steps but in floating point as well. For instance, 0 to 1 in the float will be 0.0, 0.1, 0.2, …, 0.9, 1.0. In the above example, the step is 1 but an additional .3 of float as well. So the jump between items will be 1 plus .3 values.
In conclusion, we’ve learned how to make use of the Python NumPy arange function to create an array. We’ve also learned about the starting range, ending range, steps, and the parameter to change the datatype of items in the array that is returned by the arange function using Python code examples.
We’ve created articles on other Python Numpy functions for you, so must check them out. Thank you very much for reading this article.