← Teacher guide

Whole lists of numbers with numpy

An optional extension after pandas: students make a numpy array from a list, multiply or average it in one line, and use a test inside square brackets to keep some numbers.

Data detective · Reading data · about 25 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 7 min · Modify and Make 10 min

Running the lesson

  1. Predict: the repeated-list option catches students who know that list * 60 repeats a list. An array does the sum instead.
  2. Run: show the table's loop column to remind them of the loops from earlier lessons.
  3. Investigate: read the filter from the inside out. The test makes True and False for every number.
  4. Make: the test uses a number, so no quotes round 4.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Reading a filter in one line. Say it aloud: the lengths where the length is over 4.

When students get stuck

What you will seeA question to askThe fix
NameError: name 'np' is not definedDid you import numpy first?The first line is import numpy as np.
The mean looks like a very long decimal.Did you round it?round(short.mean(), 1) gives one decimal place.
Nothing is filtered, or all numbers print.Is the test inside the square brackets?lengths[lengths < 4] keeps rows; lengths < 4 alone only gives True and False.

Answers to the checks

  1. Predict: The list holds six song lengths in minutes. What does the last line print?
    Each length times 60. An array does the sum on every number at once, so 3.5 becomes 210.0 seconds, 4.2 becomes 252.0, and so on. A plain Python list would repeat itself 60 times instead.
  2. Count the long songs: Run the code. How many songs are longer than 4 minutes?
    3
  3. Read the filter: What does `lengths > 4` give on its own?
    One True or False for each number
    The test is asked of every number, so you get a True or False for each one. Inside square brackets, the True ones are kept.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

import numpy as np

lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])

print("Average:", round(lengths.mean(), 2), "minutes")
print("Average:", round((lengths * 60).mean()), "seconds")
print("Longest:", lengths.max(), "minutes")

Make

import numpy as np

lengths = np.array([3.5, 4.2, 2.8, 5.1, 3.9, 4.4])

short = lengths[lengths < 4]
print(len(short), "songs under 4 minutes")
print("Average:", round(short.mean(), 1), "minutes")

Stretch and support

Stretch: Print the lengths in seconds as whole numbers with np.round(lengths * 60).

Support: Give the whole program with the 4 blank for students to fill in.

Exit ticket

What does lengths[lengths < 4] do?

It keeps only the numbers that are less than 4, and gives them back as a smaller array.

What each step is for

Where it leads

Pandas uses numpy underneath, so the same square-bracket test you used on a table works on an array too.