Write Python. Understand every error.

Starting Python in your browser…

Fun with Python · topic 25 of 25

Drawing with turtle

The turtle module lets you draw pictures with code. Imagine a small turtle holding a pen, sitting in the middle of the screen and facing right. You give it instructions, and it draws a line wherever it walks.

import turtle

t = turtle.Turtle()
t.forward(100)   # walk 100 steps
t.left(90)       # turn 90 degrees to the left
t.forward(100)

The drawing appears in the Output panel when your program finishes. Every shape is just a sequence of walks and turns, which makes turtle a perfect way to practise loops.

  • t.forward(n) and t.backward(n) move the turtle
  • t.left(angle) and t.right(angle) turn it
  • t.penup() lifts the pen, t.pendown() puts it back
  • t.color("red") changes the pen colour, t.width(3) makes lines thicker
  • t.goto(x, y) jumps to a point; the centre is (0, 0)
  • t.circle(r) draws a circle of radius r

A regular shape with n sides needs n equal walks and n equal turns. The turns always add up to 360 degrees, so each turn is 360 / n degrees. With a loop, one pattern can draw a triangle, a square or an octagon just by changing n.

import turtle

t = turtle.Turtle()
sides = 6
for i in range(sides):
    t.forward(80)
    t.left(360 / sides)

Drawing programs cannot be checked automatically like the other exercises, because the result is a picture, not printed text. The Practice tab gives you creative challenges instead: compare your drawing with the description.

Key takeaways

  • The turtle starts in the centre facing right. forward, left and right move and turn it.
  • penup() lifts the pen so the turtle can move without drawing, and pendown() puts it back.
  • A regular shape with n sides needs n equal walks and turns of 360 / n degrees.
  • A loop turns repeated drawing steps into a short program.

Common mistakes

  • Turning by the inside angle instead of the turning angle. A triangle needs 120-degree turns, not 60.
  • Forgetting penup() before goto(), which draws a line to the new position.
  • Forgetting to import turtle or to create the turtle with t = turtle.Turtle(). Read the NameError guide.

Share this page:

Ctrl + Enter to run
Line 1, column 1Tab indents 4 spaces. Press Esc then Tab to leave the editor.