Select Page

The Numpy arange function (sometimes called np.arange) is a tool for creating numeric sequences in Python.

If you’re learning data science in Python, the Numpy toolkit is important. The NumPy arange function is particularly important because it’s very common; you’ll see the np.arange function in a lot of data science code.

Having said that, this tutorial will show you how to use the NumPy arange function in Python.

It will explain how the syntax works. It will also show you some working examples of the np.arange function, so you can play with it and see how it operates.

Numpy arange creates sequences of evenly spaced values

The NumPy arange function returns evenly spaced numeric values within an interval, stored as a NumPy array (i.e., an ndarray object).

That might sound a little complicated, so let’s look at a quick example.

We can call the arange() function like this:

numpy.arange(5)

Which will produce a NumPy array like this:

A numpy array with 5 elements, made with NumPy arange.

What happened here?

The np.arange function produced a sequence of 5 evenly spaced values from 0 to 4, stored as an ndarray object (i.e., a NumPy array).

Having said that, what’s actually going on here is a little more complicated, so to fully understand the np.arange function, we need to examine the syntax. Once we look at the syntax, I’ll show you more complicated examples which will make everything more clear.

The syntax of numpy arange

The syntax for NumPy arange is pretty straightforward.

Like essentially all of the NumPy functions, you call the function name and then there are a set of parameters that enable you to specify the exact behavior of the function.

An explanation of the NumPy arange function.

Assuming that you’ve imported NumPy into your environment as np, you call the function with np.arange().

Then inside of the arange() function, there are 4 parameters that you can modify:

  • start
  • stop
  • step
  • dtype

Let’s take a look at each of those parameters, so you know what each one does.

start (optional)
The start parameter indicates the beginning value of the range.

This parameter is optional, so if you omit it, it will automatically default to 0.

stop (required)
The stop parameter indicates the end of the range. Keep in mind that like all Python indexing, this value will not be included in the resulting range. (The examples below will explain and clarify this point.) So essentially, the sequence of values will extend up to but excluding the stop value.

Additionally, this parameter is required, so you need to provide a stop value.

step (optional)
The step parameter specifies the spacing between values in the sequence.

This parameter is optional. If you don’t specify a step value, by default the step value will be 1.

dtype (optional)
The dtype parameter specifies the data type.

Python and NumPy have a variety of data types that can be used here.

Having said that, if you don’t specify a data type, it will be infered based on the other arguments to the function.

Examples: how to use numpy arange

Now, let’s work through some examples of how to use the NumPy arange function.

Before you start working through these examples though, make sure that you’ve imported NumPy into your environment:

# IMPORT NUMPY
import numpy as np

Ok, let’s get started.

Create a simple NumPy arange sequence

First, let’s use a simple case.

Here, we’re going to create a simple NumPy array with 5 values.

np.arange(stop = 5)

Which produces something like the following array:

A numpy array with 5 elements, made with NumPy arange.

Notice a few things.

First, we didn’t specify a start value. Because of this, the sequence starts at “0.”

Second, when we used the code stop = 5, the “5” serves as the stop position. This causes NumPy to create a sequence of values starting from 0 (the start value) up to but excluding this stop value.

Next, notice the spacing between values. The values are increasing in “steps” of 1. This is because we did not specify a value for the step parameter. If we don’t specify a value for the step parameter, it defaults to 1.

Finally, the data type is integer. We didn’t specify a data type with the dtype parameter, so Python has inferred the data type from the other arguments to the function.

One last note about this example. In this example, we’ve explicitly used stop = parameter. It’s possible to remove the parameter itself, and just leave the argument, like this:

np.arange(5)

Here, the value “5” is treated a positional argument to the stop parameter. Python “knows” that the value “5” serves as the stop point of the range. Having said that, I think it’s much clearer to explicitly use the parameter names. It’s clearer if you just type np.arange(stop = 5).

Create a sequence in increments of 2

Now that you’ve seen a basic example, let’s look at something a little more complicated.

Here, we’re going to create a range of values from 0 to 8, in increments of 2.

To do this, we will use the start position of 0 and a stop position of 2. To increment in steps of 2, we’ll set the step parameter to 2.

np.arange(start = 0, stop = 8, step = 2)

The code creates a ndarray object like this:

The output of np.arange(start = 0, stop = 8, step = 2).

Essentially, the code creates the following sequence of values stored as a NumPy array: 0, 2, 4, 6.

Let’s take a step back and analyze how this worked. The output range begins at 0. This is because we set start = 0.

The output range then consists of values starting from 0 and incrementing in steps of 2: 2, 4, 6.

The range stops at 6. Why? We set the stop parameter to 8. Remember though, numpy.arange() will create a sequence up to but excluding the stop value. So once arange() gets to 6, the function can’t go any further. If it attempts to increment by the step value of 2, it will produce the value of 8, which should be excluded, according to the syntax stop = 8. Again, np.arange will produce values up to but excluding the stop value.

Specify the data type for np.arange

As noted above, you can also specify the data type of the output array by using the dtype parameter.

Here’s an example:

np.arange(start = 1, stop = 5, dtype = 'float')

A NumPy array of floats from 1 to 4, made with np.arange.

First of all, notice the decimal point at the end of each number. This essentially indicates that these are floats instead of integers.

How did we create this? This is very straightforward, if you’ve understood the prior examples.

We’ve called the np.arange function starting from 1 and stopping at 5. And we’ve set the datatype to float by using the syntax dtype = 'float'.

Keep in mind that we used floats here, but we could have one of several different data types. Python and NumPy have a couple dozen different data types. These are all available when manipulating the dtype parameter.

Create a 2-dimensional array with np.arange

It’s also possible to create a 2-dimensional NumPy array with numpy.arange(), but you need to use it in conjunction with the NumPy reshape method.

Ultimately, we’re going to create a 2 by 2 array with the values from 1 to 9.

But before we do that, there’s an “intermediate” step that you need to understand.

…. let’s just create a 1-dimensional array with the values from 1 to 9:

np.arange(start = 1, stop = 10, step = 1)

This code creates a NumPy array like this:

A numpy array from 1 to 9.

Notice how the code worked. The sequence of values starts at 1, which is the start value. The final value is 9. That’s because we set the stop parameter equal to 10; the range of values will be up to but excluding the stop value. Because we’re incrementing in steps of 1, the last value in the range will be 9.

Ok. Let’s take this a step further to turn this into a 2 by 2 array.

Here, we’re going to use the reshape method to re-shape this 1-dimensional array into a 2-dimensional array.

np.arange(start = 1, stop = 10, step = 1).reshape((3,3))

Notice that this is just a modification of the code we used a moment ago. We’re using np.arange(start = 1, stop = 10, step = 1) to create a sequence of numbers from 1 to 9.

Then we’re calling the reshape() method to re-shape the sequence of numbers. We’re reshaping it from a 1-dimensional NumPy array, to a 2-dimensional array with 3 values along the rows and 3 values along the columns.

np.arange is common, so make sure you master it

In the last few examples, I’ve given you an overview of how the NumPy arange function works.

It’s pretty straightforward once you understand the syntax, and it’s not that hard to learn.

Having said that, make sure that you study and practice this syntax. Like all programming techniques, the key to mastery is repeated practice. Use the simple examples that I’ve shown above and try to recall the code. Practice and review this code until you know how the syntax works from memory.

If you have more questions about the NumPy arange function, leave your questions in the comments below.

For more Python data science tutorials, sign up for our email list

Here at Sharp Sight, we teach data science. We want to help you master data science as fast as possible.

If you sign up for our email list, you’ll receive Python data science tutorials delivered to your inbox.

Want to learn data science in Python? Sign up now.