Lists
This content is not available in your language yet.
How to write a list
Section titled How to write a listName 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
Section titled A Simple ExampleThis 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
Section titled A Better ExampleIf 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
Section titled ColoursHere’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
Section titled A Colour ExampleIn 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 turtlescreen = Screen()screen.bgcolor("lightblue")
t = Turtle()t.fillcolor("yellow") # Set the shape's fill colourt.color("red") # Set the turtle's drawing colour
# Draw a filled circlet.begin_fill()t.circle(50) # Turtle draws a red circle outlinet.end_fill() # Circle is filled with yellow