← Python Foundations with Turtle

Lists

Keep many values under one name, pick one by its place, and count them with len.

A playlist, a shopping cart, a class roll and a leaderboard are all lists: many values kept in order under one name.

Names hold one value. A list holds many, in order, under a single name. You pick one by its place.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lists.py
from turtle import *

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

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

colours[i] is the item in place i. The counter i goes 0, 1, 2 and so on. Read the code. What colour is the third side?

What colour is the third side?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction.

Line What it does Why
colours = ["red", ...] Keeps five colours under one name Each one is in quotes, with commas between
colours[0] The item in place 0: "red" The first place is 0, not 1
len(colours) How many items: 5 The loop runs once for each
pencolor(colours[i]) Picks the colour for this pass i is the place
lists.py
from turtle import *

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

pensize(8)
for i in range(len(colours)):
    pencolor(colours[i])
    forward(50)
    right(72)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lists.py
from turtle import *

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

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

Work out which item sits in each place.

Use the length to find the last item.

Here is a version where something is wrong.

Fill in the item at each place in the list (counting from 0).

placeitem
0red
1
2
3
4

A list has 8 items. What is the place number of the last one?

This program should draw three circles, but it stops with an error. Click the line that causes it.

ModifyChange the code on purpose and check the result.

Modify

Put two more colours into the list and run it. Does the loop need to change?

Need a hint?

Each colour is a word in quotes, separated from the next by a comma.

Replace the first colour (place 0) with "pink".

Need a hint?

Replace the first word in the list. Nothing else changes.

Another hint

The first item is colours[0]. The loop still uses it first.

Try it yourselfYour edits are saved in this browser.

Change the first colour in the list.

    Run your program. This ticks when your code does all of the above.

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    This mixes three lessons: a name, the counter, and a list. Work it out on paper first.

    lists.py
    from turtle import *
    
    sizes = [20, 40, 60]
    
    for i in range(len(sizes)):
        forward(sizes[i])
        right(360 / len(sizes))
    

    sizes = [20, 40, 60] and the turn is 360 / len(sizes). How many degrees does the turtle turn on each pass?

    For teachers

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

    Open the teacher guide: answers, solutions, slides and a worksheet.

    Students will be able to

    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.

    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

    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.

    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.

    What each step is for

    Now you can
    Next lesson10. For each itemVisit every item in a list in turn, without counting places.