---
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
---

# More on Arrays

It's often necessary to compute something that involves data from more than one
array. If two arrays are of the same size, Python makes it easy to do
calculations involving both arrays.

For our first example, we return once more to the temperature data.  This time,
we create arrays of average daily
[high](http://berkeleyearth.lbl.gov/auto/Regional/TMAX/Text/global-land-TMAX-Trend.txt)
and
[low](http://berkeleyearth.lbl.gov/auto/Regional/TMIN/Text/global-land-TMIN-Trend.txt)
temperatures for the decades surrounding 1850, 1900, 1950, and 2000.

First we get Numpy, renamed as `np`:

```{python}
import numpy as np
```

Next we create the array:

```{python}
highs = np.array([13.6, 14.387, 14.585, 15.164])
highs
```

```{python}
lows = np.array([2.128, 2.371, 2.874, 3.728])
lows
```

Suppose we'd like to compute the average daily *range* of temperatures for
each decade.  That is, we want to subtract the average daily high in the 1850s
from the average daily low in the 1850s, and the same for each other decade.

We could write this laboriously using `.item`:

```{python}
np.array([
    highs.item(0) - lows.item(0),
    highs.item(1) - lows.item(1),
    highs.item(2) - lows.item(2),
    highs.item(3) - lows.item(3)
])
```

As when we converted an array of temperatures from Celsius to Fahrenheit,
Python provides a much cleaner way to write this:

```{python}
highs - lows
```

![](../images/array_subtraction.png)


What we've seen in these examples are special cases of a general feature of arrays.


## Elementwise arithmetic on pairs of numerical arrays

If an arithmetic operator acts on two arrays of the same size, then the
operation is performed on each corresponding pair of elements in the two
arrays. The final result is an array.

For example, if `array1` and `array2` have the same number of elements, then
the value of `array1 * array2` is an array. Its first element is the first
element of `array` times the first element of `array2`, its second element is
the second element of `array1` times the second element of `array2`, and so
on.

{ucb-page}`More_on_Arrays`
