numpy arange demonstration

Using the numpy arange() method

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

numpy arange demonstration

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.

Let’s look at some examples of using the numpy arange() function.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

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.

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.

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 –

  1. By default, the np.linspace() function generates values in the range [start, stop] where as the np.arange() generates values in the half-open interval [start, stop).
  2. You can specify the number of values you want to generate with np.linspace() but not with np.arange()
  3. You can specify the step-size with np.arange() but not with np.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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top