Skip to content
BetaDE
Lesson 4 of 35Python

Python · Lesson 4 · 45 minutes

Project: Drawing with the turtle

What you'll learn

  • Draw lines, shapes and colours with the turtle
  • Use loops and functions to create patterns
  • Program your own artwork and save it as a picture

The turtle that can draw

Imagine a little turtle on a sheet of paper. It has a pen strapped to its belly, and you give it commands: “Go forward 100 steps”, “Turn left by 90 degrees”. Wherever it walks, a line appears. That’s turtle graphics – a module that comes with every Python.

Press Run and watch:

import turtle

t = turtle.Turtle()
t.shape("turtle")

t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)

The centre of the canvas is the point (0, 0). At the start the turtle faces right.

CommandWhat happens
t.forward(100)100 steps forward
t.back(50)50 steps back
t.left(90) / t.right(90)turn by 90 degrees
t.penup() / t.pendown()lift / lower the pen
t.goto(x, y)walk to a point
t.circle(50)circle with radius 50
t.color("red")choose a colour
t.pensize(4)line thickness
t.speed(0)as fast as possible (1 = slow, 10 = fast)

Loops save work

The same thing four times? That calls for a loop. And with a list of colours it gets colourful – the square brackets […] are a list, more about that in the very next lesson. The for loop takes one colour after another from it:

import turtle

t = turtle.Turtle()
t.pensize(5)
colours = ["red", "orange", "gold", "green"]

for colour in colours:
    t.color(colour)
    t.forward(120)
    t.left(90)

Filling shapes

Between begin_fill() and end_fill() the turtle remembers its path and paints the area afterwards:

import turtle

t = turtle.Turtle()
t.color("darkviolet", "violet")  # pen colour, fill colour

t.begin_fill()
for _ in range(5):
    t.forward(150)
    t.right(144)
t.end_fill()

Why 144 degrees? For a star the turtle turns around twice in total: 2 × 360 ÷ 5 = 144.

The rainbow spiral

Now it gets exciting. If the distance gets a little longer with each step and the angle isn’t quite 90 degrees, you get a spiral. The colours come from a list – thanks to % the list starts over again and again:

import turtle

turtle.bgcolor("black")
t = turtle.Turtle()
t.speed(10)
t.pensize(2)
colours = ["red", "orange", "yellow", "lime", "cyan"]

for i in range(160):
    t.color(colours[i % 5])
    t.forward(i * 1.6)
    t.left(73)

Try changing the angle to 71, 91 or 121 – every time you get a completely different picture.

A flower made of circles

import turtle

t = turtle.Turtle()
t.speed(10)
t.pensize(2)
colours = ["deeppink", "hotpink", "orchid", "mediumpurple"]

for petal in range(36):
    t.color(colours[petal % 4])
    t.circle(80)
    t.left(10)

t.hideturtle()

Pro trick: a tree that draws itself

Here we’re jumping ahead a little – just try it and be amazed, you don’t need to understand it yet. With def you build your own command (a function, there’s a whole lesson on that later). A function may even call itself (recursion). A branch splits into two smaller branches, which split again … That’s how trees, ferns and snowflakes grow in nature:

import turtle

t = turtle.Turtle()
t.speed(10)
t.left(90)
t.penup()
t.goto(0, -200)
t.pendown()


def branch(length, thickness):
    if length < 8:
        return
    t.pensize(thickness)
    t.color("saddlebrown" if length > 30 else "forestgreen")
    t.forward(length)
    t.left(25)
    branch(length * 0.72, thickness * 0.7)
    t.right(50)
    branch(length * 0.72, thickness * 0.7)
    t.left(25)
    t.back(length)


branch(110, 10)
t.hideturtle()

The trick: at the end of each branch the turtle walks back with back(length). That way it’s always standing where the next branch should start.

Tip: With 💾 Save picture below the drawing you can download your artwork as a PNG file. And with turtle.tracer(0) at the start, even a huge picture appears instantly.

Your task

Instead of writing new code for every shape, you use your own command that can do any polygon: triangle, square, pentagon, octagon … The line def polygon(corners, side): is already prepared – you only write what happens indented below it, using corners and side just like variables. The trick with the angle is always the same: after all the corners the turtle has turned around exactly once.

Your task

Your turn

Write a function polygon(corners, side) that uses the turtle t to draw a regular polygon: corners sides that all have the length side. At the end the turtle must be exactly where it started. Tip: after each side it turns by 360 / corners degrees. Use it to draw a pentagon with side length 100.

import turtle

t = turtle.Turtle()
t.color("purple")
t.pensize(3)


def polygon(corners, side):
    t.forward(side)


polygon(5, 100)

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

import turtle

t = turtle.Turtle()
t.color("purple")
t.pensize(3)


def polygon(corners, side):
    for _ in range(corners):
        t.forward(side)
        t.left(360 / corners)


polygon(5, 100)