Making and filling arrays.#

import numpy as np

Making arrays#

You have seen how to create arrays from a sequence of values:

np.array([1, 2, 3])
array([1, 2, 3])

You have also seen how to create arrays that are sequential integers, using np.arange:

np.arange(5)
array([0, 1, 2, 3, 4])

We often find that we want to create an empty or default array, that we will fill later.

Numpy has routines for that. The main ones we will use are np.zeros and np.ones.

You can guess what they do:

np.zeros(5)
array([0., 0., 0., 0., 0.])
np.ones(5)
array([1., 1., 1., 1., 1.])

These arrays aren’t very useful at the moment; usually, we will want to fill in the elements of these arrays with other values.

Filling arrays#

We put values into arrays using assignment.

A refresher on assignment#

Remember the basic assignment statement. We have so far learned that the assignment statement is a name followed by = followed by an expression (a recipe that returns a value).

# An assignment statement
a = 1
a
1

Here the left hand side (LHS) is a.

The right hand side (RHS) is an expression: 1.

We can read a = 1 as “a gets the value 1.” We can also read it as: “Make the location called ‘a’ point to the value 1.”

So far, the left hand side (LHS) has always been a name.

In fact, the LHS can be anything we can assign to.

A refresher on indexing#

Remember too that we can retrieve values from an array by indexing.

Here is an example array:

my_array = np.arange(1, 11)
my_array
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

We can retrieve one or more values by indexing:

my_array[1]
2
my_array[5:]
array([ 6,  7,  8,  9, 10])

Assignment with indexing#

In fact we can use these exact same specifications on the LHS of an assignment statement:

my_array[1] = 99
my_array
array([ 1, 99,  3,  4,  5,  6,  7,  8,  9, 10])
my_array[5:] = 100
my_array
array([  1,  99,   3,   4,   5, 100, 100, 100, 100, 100])

When you use array indexing on the LHS, it means specify the elements to assign. So:

  • my_array[5] on the RHS means - get the value at offset 5 in my_array

  • my_array[5] on the LHS means - use this location to store the data returned from the RHS.

So we can read my_array[1] = 99 as “Make the location ‘my_array[5]’ point to the value 99.”.

We will use this kind of assignment to get a little closer to a good solution to the three girl problem, in leaping ahead.