---
jupyter:
  anaconda-cloud: {}
  jupytext:
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.0'
      jupytext_version: 0.8.1
  kernelspec:
    display_name: Python 3
    language: python
    name: python3
  language_info:
    codemirror_mode:
      name: ipython
      version: 3
    file_extension: .py
    mimetype: text/x-python
    name: python
    nbconvert_exporter: python
    pygments_lexer: ipython3
    version: 3.5.2
---

# Ranges

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:

```{python}
# 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.

```{python}
np.arange(5)
```

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.

```{python}
np.arange(3, 9)
```

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.

```{python}
np.arange(3, 30, 5)
```

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.

```{python}
np.arange(1.5, -2, -0.5)
```

{ucb-page}`Ranges`
