In this tutorial, we will look at the syntax and usage of the numpy arange() function with the help of some examples.
What does numpy arange() do?

The numpy arange() function is used to create an array of evenly spaced values in a given interval with a uniform step size. The following is the syntax:
import numpy as np # np.arange with all the default paramters arr = np.arange(start=0, stop, step=1, dtype=None, like=None) # mostly you'll be using only these paramters arr = np.arange(start, stop, step)
It returns a numpy array of values that are evenly spaced. Note that the values are generated in the half-open interval [start, stop)
. That is the interval including start but excluding stop.
Also, this function is similar to the numpy linspace() function which also generates evenly spaced values in linear space. More on the difference between the two at the end of this tutorial.
Examples
Let’s look at some examples of using the numpy arange() function.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
1. Generate numbers from 0
If you only pass a single number to the np.arange()
function, it will return an array of values starting from 0 with a step-size of 1 till (but not including) the number you passed. For example, to generate values from 0 to 4 –
import numpy as np # generate values from 0 to 4 arr = np.arange(5) # display the returned array print(arr)
Output:
[0 1 2 3 4]
Here, we only pass one value to the function, 5 which is treated as the value for the parameter stop. For paramters start and step, their default values – 0 and 1 are used respectively. Thus we get an array of values from 0 to 5 (5 not included) with a step-size of 1.
2. Evenly spaced values between two numbers
Let’s now generate values between two custom numbers. For example, values between 2 and 9 –
# generate values between 2 and 9 arr = np.arange(2, 9) # display the returned array print(arr)
Output:
[2 3 4 5 6 7 8]
Here, 2 is the start value and 9 is the stop value. Since we have not specified the step-size, its default value of 1 is used.
3. Using a custom step-size
Let’s now generate values between 2 and 9 with a step-size of 2. For this, pass the desired step-size to the step
parameter.
# generate values between 2 and 9 arr = np.arange(2, 9, step=2) # display the returned array print(arr)
Output:
[2 4 6 8]
You can see that the resulting values are still evenly spaced but now have a step-size of two.
For more on the numpy arange() function, refer to its documentation.
Numpy linspace() vs arange()
Both the numpy linspace() and arange() functions are used to generate evenly spaced values in a given interval but there are some differences between the two –
- By default, the
np.linspace()
function generates values in the range[start, stop]
where as thenp.arange()
generates values in the half-open interval[start, stop)
. - You can specify the number of values you want to generate with
np.linspace()
but not withnp.arange()
- You can specify the step-size with
np.arange()
but not withnp.linspace()
Thus if you want to generate evenly-spaced values between two numbers and want a spcific step-size use np.arange()
or if you want to generate a fixed number of values that are evenly-spaced between two numbers use np.linspace()
.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.