Skip to content

Lists

This content is not available in your language yet.

How to write a list

Name the list and use square brackets, separate the different items out with commas and add quotation marks before and after each word. The list can then be called later in the code. For example, numList = [1, 2, 3].

A Simple Example

This code works because the loop goes 6 times, and there are 6 colours in the list.

list = ["red", "orange", "yellow", "green", "blue", "purple"]
t = Turtle()
for i in range(6):
t.color(list[i])
t.forward(100)
t.left(360/6)

A Better Example

If the list has fewer colours than the number of loops, it would stop before the loop finishes. To fix that, we use the % symbol. This symbol makes the list repeat itself after it reaches the end, so we never run out of colours.

list = ["red", "orange", "yellow", "green", "blue", "purple"]
t = Turtle()
for i in range(20):
t.color(list[i % len(list)])
t.forward(100)
t.left(360/20)

Colours

Here’s a little bit about the colour functions in Turtle:

bgcolor() # This changes the background colour of the whole screen.
fillcolor() # This sets the colour for filling shapes.
color() # This changes the turtle’s drawing colour (the outline of the shape).

A Colour Example

In this example we set the background to light blue, then make the turtle draw a circle outline in red. Then circle is filled with yellow inside.

from turtle import Turtle, Screen
# Set up the screen and turtle
screen = Screen()
screen.bgcolor("lightblue")
t = Turtle()
t.fillcolor("yellow") # Set the shape's fill colour
t.color("red") # Set the turtle's drawing colour
# Draw a filled circle
t.begin_fill()
t.circle(50) # Turtle draws a red circle outline
t.end_fill() # Circle is filled with yellow

Checklist

  • I know what a modulo (%) does.
  • I can create a list.
  • I can make my turtle draw different colours.
  • I can use different functions to set what I’m colouring in.