← Reference

Python functions and events: cheat sheet

Python functions and events · In any playground, open Reference to copy these.

Start from a working program

forward

Move the turtle forward. The number is how many steps.

forward(100)
What it draws

right

Turn clockwise by this many degrees.

right(90)
Example
forward(60)
right(90)
forward(60)
What it draws

goto

Jump to a position. (0, 0) is the centre; x goes right, y goes up.

goto(0, 0)
Example
goto(80, 60)
What it draws
From the centre to (80, 60)

pendown

Put the pen down so moving draws again.

pendown()
Example
forward(40)
penup()
forward(40)
pendown()
forward(40)
What it draws
Pen down again for the last part

pencolor

Set the line colour, using a colour name.

pencolor("red")
Example
pencolor("red")
forward(100)
What it draws

begin_fill / end_fill

Everything drawn between these two lines is filled in.

begin_fill()

end_fill()
Example
fillcolor("orange")
begin_fill()
for i in range(4):
    forward(60)
    right(90)
end_fill()
What it draws

square (loop)

Draw a square by repeating forward and right four times.

for i in range(4):
    forward(100)
    right(90)
What it draws

if / else

Do one thing or the other, depending on a condition.

if x > 0:
    pass
else:
    pass
Example
for x in [-90, -30, 30, 90]:
    penup()
    goto(x, 0)
    if x > 0:
        dot(30, "red")
    else:
        dot(30, "blue")
hideturtle()
What it draws
Red when x > 0, else blue

variable

Give a value a name, so you can reuse and change it.

size = 50
Example
length = 120
forward(length)
left(90)
forward(length / 2)
What it draws
length = 120, then half of it

grow value

Change a value every pass: read the old one, store a new one.

size = 5
for i in range(20):
    forward(size)
    right(90)
    size = size + 5
Example
size = 5
for i in range(30):
    forward(size)
    right(90)
    size = size + 5
What it draws
The same spiral, grown by an update

backward

Move the turtle backward without turning.

backward(100)
What it draws

left

Turn anticlockwise by this many degrees.

left(90)
Example
forward(60)
left(90)
forward(60)
What it draws

penup

Lift the pen so moving does not draw.

penup()
Example
forward(40)
penup()
forward(40)
pendown()
forward(40)
What it draws
Pen up for the middle part

pensize

Set how thick the line is.

pensize(5)
Example
pensize(1)
forward(50)
pensize(8)
forward(50)
What it draws
pensize(1), then pensize(8)

fillcolor

Choose the colour to fill shapes with.

fillcolor("blue")
Example
fillcolor("blue")
begin_fill()
circle(40)
end_fill()
What it draws

speed

How fast the turtle moves, from 1 (slow) to 10 (fast).

speed(10)

circle

Draw a circle with this radius.

circle(50)
What it draws
circle(50) starts at the turtle and curves left

for loop

Repeat the indented lines this many times.

for i in range(4):
    pass
Example
for i in range(5):
    forward(15)
    penup()
    forward(10)
    pendown()
What it draws
range(5): five dashes

every other

i % 2 == 0 is true on every second pass.

for i in range(8):
    if i % 2 == 0:
        pass
    else:
        pass
Example
for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    pensize(6)
    forward(30)
    right(45)
What it draws
Red and blue sides, taking turns

counter step

Use the counter i to make each pass different.

for i in range(20):
    forward(i * 5)
    right(90)
Example
for i in range(30):
    forward(i * 5)
    right(90)
What it draws
A square spiral: each line longer than the last

list

Keep many values in order under one name. The first item is place 0.

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

Functions

dot

Draw a filled dot of this size.

dot(20)
Example
dot(30)
hideturtle()
What it draws

def (function)

Name a group of steps, then run it by calling its name.

def shape():
    pass

shape()
Example
def zigzag():
    left(45)
    forward(30)
    right(90)
    forward(30)
    left(45)

zigzag()
zigzag()
zigzag()
What it draws
Defined once, called three times

for each item

Work through a list, one item at a time.

for c in colours:
    pass
Example
pensize(6)
for c in ["red", "blue", "green"]:
    pencolor(c)
    forward(40)
What it draws
One line per colour in the list

print

Show text or a value in the output box.

print("Hello")

in

True if the item is in the list (or the key is in the dictionary, or the letters are in the text).

if item in bag:
    pass

Interaction

setheading

Face a direction. 0 is right, 90 is up, 180 is left.

setheading(0)
Example
setheading(90)
forward(60)
What it draws
setheading(90): facing up

bgcolor

Set the background colour.

bgcolor("lightblue")
Example
bgcolor("lightblue")
forward(80)
What it draws

clear

Wipe the drawing.

clear()

onscreenclick

Run a function every time the picture is clicked. It receives the click position.

def stamp(x, y):
    goto(x, y)
    dot(20)

onscreenclick(stamp)
listen()
mainloop()
Example
def stamp(x, y):
    goto(x, y)
    dot(20)

penup()
stamp(-60, 30)
stamp(50, -20)
hideturtle()
What it draws
After two clicks

write

Write text at the turtle's position.

write("Hello")
Example
write("Hello", False, "left", ("Arial", 20, "normal"))
hideturtle()
What it draws

hideturtle

Hide the turtle so only the drawing shows.

hideturtle()
Example
forward(80)
hideturtle()
What it draws
The line stays; the turtle is hidden

running total

Start the total at 0 once, then add to it on every pass.

total = 0
while total < 10:
    total += int(input("Add: "))

onkey

Run a function every time a key is pressed. Click the picture first so it receives keys.

def go_up():
    forward(20)

onkey(go_up, "Up")
listen()
mainloop()
Example
def go_up():
    forward(20)

go_up()
go_up()
go_up()
What it draws
After pressing Up three times

Final challenge: Catch the turtle

random number

Pick a random whole number between two values.

import random

n = random.randint(1, 10)

ontimer

Run a function again after a number of milliseconds. A function can start its own timer.

def tick():
    forward(5)
    ontimer(tick, 50)

tick()
mainloop()