Color Lists¶
By the end of this lesson you'll be able to:
- Create a list of color strings and access items by index number
- Loop through every item in a list using
for color in my_list: - Draw a row of colored shapes by iterating over a color list
A list is an ordered collection of values. In Python you write it with square brackets. Lists are perfect for storing a palette of colors and then painting shapes one by one through the whole palette.
Welcome to This Lesson!
What if you could tell Monty: "here are ten colors — paint one circle for each"?
That's exactly what lists let you do! Let's code it together!
Creating and Using a List¶
A list stores multiple values in one variable, separated by commas inside square brackets:
1 | |
Access any item by its index — its position in the list, counting from 0:
1 2 3 | |
To do something with every item, use a for loop directly over the list:
1 2 | |
Scratch Bridge
In Scratch you used the List block with numbered slots (item 1, item 2, …).
In Python, lists use the same idea — but counting starts at 0, not 1.
So colors[0] is what Scratch calls "item 1 of colors".
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
What Do You Think Will Happen?
The list has 7 colors and the loop runs once per color.
How many circles will appear, and what order will the colors go in?
Make your guess — then click Run!
Try It Now¶
Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.
Were you right? Seven filled circles spread in a row, each painted a different color from the list — a rainbow palette!
How It Works¶
for color in colors: is a for-each loop — on each repetition, color is automatically set to the next item in the list. The first time through it's 'red', then 'orange', and so on until the list is exhausted.
forward(45) moves the turtle 45 steps to the right after each circle so the next one appears beside it without overlapping.
Explanation Table¶
| Line | What it does |
|---|---|
colors = ['red', …] |
Creates a list of color strings |
colors[0] |
Accesses the first item ('red'); indexes start at 0 |
for color in colors: |
Loops once for each item; color holds the current item |
monty.goto(-150, 0) |
Moves to the left side of the canvas as the starting point |
monty.forward(45) |
Shifts right 45 steps before drawing the next circle |
Index Out of Range
If your list has 7 items, valid indexes are 0–6. Trying colors[7] causes an
IndexError — Python can't find item 7 because it doesn't exist.
Use len(colors) to find out how many items are in a list.
Learning Check¶
Your Turn — Draw Squares Instead
The program below is supposed to draw a row of small filled squares (not circles),
but the loop body is incomplete. Add two lines inside the loop — a for loop
that draws four sides — so each color becomes a filled square instead of a circle.
Experiments¶
Try these changes to the color-circles code. Predict first, then run!
-
Add
'gold'and'brown'to the end of thecolorslist. You'll know it worked when two extra circles appear to the right of the original seven. -
Change
monty.circle(20)tomonty.circle(15)andmonty.forward(45)tomonty.forward(35). You'll know it worked when the circles are smaller and tighter together. -
Print the length of the list: add
print(len(colors))after the list definition and before the loop. You'll know it worked when7(or your new count) appears in the output box below the editor. -
Access a single color by index: add
print(colors[3])after the list definition. You'll know it worked whengreenappears in the output (it's at index 3). -
Try a
forloop using an index: replacefor color in colors:withfor i in range(len(colors)):and then usecolors[i]instead ofcolor. You'll know it worked when the output looks the same — this is an alternative way to iterate.
Great Work!
Lists let you store whole collections of data and loop through them automatically!
In the next lab you'll combine lists with random numbers and functions to draw random stars.
Let's code it together!