3.6 Ranges

Download notebook Interact

A range is an array of numbers in increasing or decreasing order, each separated by a regular interval. Ranges are useful in a surprisingly large number of situations, so it is worthwhile to learn about them.

We will use the Numpy package to make ranges. When we load the Numpy package, we always rename it as np, like this:

# Load the Numpy package.
import numpy as np

Ranges are defined using the np.arange function, which takes either one, two, or three arguments: a start, and end, and a ‘step’.

If you pass one argument to np.arange, this becomes the end value, with start=0, step=1 assumed. Two arguments give the start and end with step=1 assumed. Three arguments give the start, end and step explicitly.

A range always includes its start value, but does not include its end value. It counts up by step, and it stops before it gets to the end.

Here is what np.arange does when you pass only one argument:

np.arange(end): An array starting with 0 of increasing consecutive integers, stopping before end.
np.arange(5)
array([0, 1, 2, 3, 4])

Notice how the array starts at 0 and goes only up to 4, not to the end value of 5.

Put another way, np.arange(end) creates an array of increasing consecutive integers starting at 0 up to but not including the end value.

If you pass two arguments to np.arange:

np.arange(start, end): An array of consecutive increasing integers from start, stopping before end.
np.arange(3, 9)
array([3, 4, 5, 6, 7, 8])

If you pass three arguments:

np.arange(start, end, step): A range with a difference of step between each pair of consecutive values, starting from start and stopping before end.
np.arange(3, 30, 5)
array([ 3,  8, 13, 18, 23, 28])

This array starts at 3, then takes a step of 5 to get to 8, then another step of 5 to get to 13, and so on.

When you specify a step, the start, end, and step can all be either positive or negative and may be whole numbers or fractions.

np.arange(1.5, -2, -0.5)
array([ 1.5,  1. ,  0.5,  0. , -0.5, -1. , -1.5])
This page has content from the Ranges notebook from the UC Berkeley course. See the Berkeley course section of the license