← Teacher guide

Lists

Students keep many values in one list, pick one by its place, and count them with len, learning that places start at 0.

Python Foundations with Turtle · Choices and lists · about 30 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, 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 10 min · Investigate 10 min · Modify 6 min · Mix it up 4 min

Before class

Run the coloured shape. The idea to have ready: the first item in a list is in place 0, so a list of 5 items has places 0 to 4. Asking for place 5 stops the program with an IndexError.

Running the lesson

  1. Predict: ask what colour the third side is. Many say orange: they count the places from 1. Do not correct it yet; run it and let the picture do the work.
  2. Run: read colours[i] aloud as 'the colour in place i'. Ask: what is i on the first pass, and so which colour?
  3. Investigate: students say which item is in each place before running. The number question is the same idea backwards: the last place is one less than the length.
  4. Modify: adding colours changes the picture without touching the loop. Ask why that is useful (the data changes, the instructions do not).
  5. Mix it up: the turn is worked out from len, so it changes when the list does.

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)

Places start at 0, so the last place is one less than the length. Ask how many items there are and what the last place number is.

When students get stuck

What you will seeA question to askThe fix
An IndexError: list index out of range.How many items are in the list, and what is the last place number?The last place is one less than the length. Loop with range(len(colours)) so the count always matches.
A NameError, or the colour name is printed instead of used.Does every colour have quotes round it, with commas between them?Text in a list needs quotes: ["red", "blue"].
A student expects colours[1] to be red.What is the place of the very first item?The first place is 0. colours[0] is red, colours[1] is orange.

Answers to the checks

  1. Predict: What colour is the third side?
    Yellow. The third pass has i = 2, and colours[2] is the item in place 2. Places count from 0, so place 0 is red, place 1 is orange and place 2 is yellow.
  2. Find the place: Fill in the item at each place in the list (counting from 0).
    orange, yellow, green, blue
    Counting starts at 0, so the first item is colours[0] and the last of five is colours[4].
  3. Last place: A list has 8 items. What is the place number of the last one?
    7
    Places run from 0 to 7, so the last of eight is colours[7]. The length is always one more than the last place.
  4. Spot the bug: This program should draw three circles, but it stops with an error. Click the line that causes it.
    line 6
    The loop runs four times, so on the last time it asks for colours[3]. The list has only places 0, 1 and 2. range(len(colours)) would always match the list.
  5. Mix it up: sizes = [20, 40, 60] and the turn is 360 / len(sizes). How many degrees does the turtle turn on each pass?
    120
    len(sizes) is 3, and 360 / 3 is 120. The turn is worked out from the list, so a longer list gives a smaller turn.

Solutions

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

Modify › Add two colours

from turtle import *

colours = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]

pensize(8)
for i in range(len(colours)):
    pencolor(colours[i])
    forward(50)
    right(360 / len(colours))

Modify › Change the first colour

from turtle import *

colours = ["pink", "orange", "yellow", "green", "blue"]

pensize(8)
for i in range(len(colours)):
    pencolor(colours[i])
    forward(50)
    right(72)

Stretch and support

Stretch: Make a second list of sizes and use sizes[i] alongside colours[i], so each side has its own colour and length.

Support: Give students the list on paper with the places written under each item, and ask them to point to colours[2] before running the program.

Exit ticket

colours = ["red", "blue", "green", "gold"]. What is colours[1], and what is the place of "gold"?

colours[1] is "blue" (counting starts at 0). "gold" is in place 3.

What each step is for

Where it leads

Next lesson visits every item without counting places: a for-each loop. Lists return in the game's path and the buttons of a control panel. Playlists, class rolls and leaderboards are lists.