← Teacher guide Markdown for Prepare

Commands, loops and values: cheat sheet

Python Foundations with Turtle · In any playground, open Reference to copy these.

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

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

speed

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

speed(10)

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

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

named turn

Name the number of sides once; the turn is worked out from it.

sides = 5
for i in range(sides):
    forward(60)
    right(360 / sides)
Example
sides = 6
for i in range(sides):
    forward(60)
    right(360 / sides)
What it draws
A hexagon: change sides and the turn follows

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

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

square (loop)

Draw a square by repeating forward and right four times.

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

running total

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

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

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

print

Show text or a value in the output box.

print("Hello")