Contents

- 1 What is Numpy Arange function in Python
- 2 Numpy Arange : numpy.arange()
- 3 Examples of Numpy Arange
- 3.1 Import Numpy Library
- 3.2 Example 1 â€“ Simple Example of Numpy Arange
- 3.3 Example 2 â€“ Numpy Arange with Both Start and Stop
- 3.4 Example 3 â€“ Numpy Arange with Step Interval
- 3.5 Example 4 â€“ np.arange with dtype parameter
- 3.6 Example 5 â€“ Numpy Arange with Float Step Interval
- 3.7 Example 6 â€“ Issues with Float Step Interval

**What is Numpy Arange function in Python**

The Numpy Arange function is used to create a numpy array whose elements are evenly distributed within a given range. Â In this tutorial, we will understand the syntax of np.arange() and go through multiple examples by using its various parameters.

**Numpy Arange : numpy.arange()**

**Syntax**

numpy.arange(start=0, stop, step=1, dtype)

(optional) â€“ It denotes the starting value of the range, if not specified the default value is 0.*start*â€“ It denotes the ending value of the interval, it should be noted that this is excluded from the range of values of the numpy array. However, in some situations where theÂ step is a float number the round-off effect can result in the inclusion of this value.*stop*(optional) â€“ It denotes the steps or interval between the values of the numpy array, if not specified the default is 1.*step*(optional) â€“ It denotes the data type of the numpy array, if not specified, then it will derive the data type from the other input arguments.*dtype*

**Examples of Numpy Arange**

**Import Numpy Library**

Before starting the examples, we shall import the numpy library as below.

In [0]:

importÂ numpyÂ asÂ np

Â

**Example 1 â€“ Simple Example of Numpy Arange**

We start with the very basic example of numpy array where we pass just the only mandatory argument stop with a value of 10. This creates a numpy array with the default start value of 0 and the default step of 1.

In [1]:

arrÂ =Â np.arange(10); print(arr)

[0 1 2 3 4 5 6 7 8 9]

**Example 2 â€“ Numpy Arange with Both Start and Stop**

Let us rewrite the first example by including the parameter start = 0 and it can be seen that the same numpy array is produced. This also confirms the fact that the default value of stop is 0.

In [2]:

arrÂ =Â np.arange(startÂ =Â 0,Â stopÂ =Â 10); print(arr)

[0 1 2 3 4 5 6 7 8 9]

Â

**Example 3 â€“ Numpy Arange with Step Interval**

This time let us modify the previous example by including the step parameter as 2 which creates the numpy array with evenly spaced values between 0 and 10 at an interval of 2.

In [3]:

arrÂ =Â np.arange(startÂ =Â 0,Â stopÂ =Â 10,Â Â step=2); print(arr)

Out[3]:

[0 2 4 6 8]

Â

**Example 4 â€“ np.arange with dtype parameter**

By default, the arange function infers the data type of the numpy array from input parameters, however, we can define the data type by using dtype parameter.

In the below example we have used dtype = float which produces the output array with all values as float type.

In [4]:

arrÂ =Â np.arange(startÂ =Â 0,Â stopÂ =Â 10,Â step=2,Â dtype=float); print(arr)

Out[4]:

[0. 2. 4. 6. 8.]

**Example 5 â€“ Numpy Arange with Float Step Interval**

In this example, we assign the step parameter a float value of 1.5 which creates the corresponding ndarray as shown below.

In[5]:

arrÂ =Â np.arange(startÂ =Â 0,Â stopÂ =Â 10,Â step=1.5,Â dtype=float); print(arr)

Out[5]:

[0. 1.5 3. 4.5 6. 7.5 9. ]

**Example 6 â€“ Issues with Float Step Interval**

Although we can use float value for the step size, however, the documentation of numpy gives a caution while using it that the â€œ*output might not be numerically stable*â€œ. This is due to how the array population calculation takes place internally.

Such an example can be seen below.

In[6]:

arrÂ =Â np.arange(startÂ =Â 0,Â stopÂ =Â 5,Â stepÂ =Â 0.5,Â dtype=int) print(arr)

Out [6]:

[0 0 0 0 0 0 0 0 0 0]

Â

As we can see above that numpy array generation can go haywire while using the float value of step. Hence the documentation recommends using numpy linspace function instead for the non-integer or float step values.

You can learn more about linspace function below â€“

Â

Â