Python Turtle Graphics
A teen-friendly Python tutorial on turtle graphics: draw shapes and patterns with a virtual pen, use loops and functions to make spirals and polygons, with runnable code and a quiz.
Key takeaways
- The turtle module lets you draw by moving a pen-carrying turtle around the screen
- forward(), backward(), left() and right() move and steer the turtle
- penup() and pendown() control whether the turtle draws as it moves
- Loops repeat drawing commands to make polygons, spirals and patterns
- Functions let you name and reuse drawings, like a shape you can call anywhere
What is turtle graphics?
Turtle graphics is a way to draw pictures with code. You control a little turtle that carries a pen. When the turtle moves, it draws a line behind it — like dragging a marker across paper. Tell the turtle where to go, and it paints a picture for you.
It is built right into Python, so there is nothing extra to install. It is also one of the most fun ways to practice loops, functions and angles, because every command shows up instantly as a line on the screen.
Your first turtle
Every turtle program starts the same way: import the module and create a turtle.
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
Let's read it line by line:
import turtlebrings in the drawing tools.t = turtle.Turtle()creates a turtle and stores it in the variablet.t.forward(100)moves the turtle 100 steps forward, drawing a line.turtle.done()keeps the window open so you can see your art.
Run it and you'll see a short horizontal line. That line is your turtle's path.
Moving and steering
The turtle understands a small set of commands. These four are the most important:
| Command | What it does |
|---|---|
t.forward(d) | move forward d steps |
t.backward(d) | move backward d steps |
t.left(a) | turn left a degrees |
t.right(a) | turn right a degrees |
Notice that turning does not move the turtle — it only changes the direction it faces. You move with forward, and you steer with left and right. Combine them to go anywhere.
A worked example: drawing a square
Let's draw a square step by step. A square is four equal sides with a 90-degree turn at each corner.
import turtle
t = turtle.Turtle()
t.forward(100) # bottom side
t.left(90) # turn to face up
t.forward(100) # right side
t.left(90) # turn to face left
t.forward(100) # top side
t.left(90) # turn to face down
t.forward(100) # left side
t.left(90) # back to the start direction
turtle.done()
Trace it in your head: the turtle draws a line, turns left a quarter-turn, draws again, and repeats four times. After four turns of 90 degrees, it has turned a full 360 degrees and is back where it started, facing the way it began. The result is a neat square.
Use a loop to remove repetition
Look at the square code again. The pair of lines forward(100) and left(90) appears four times. Whenever you repeat the same steps, reach for a loop. (For a refresher, see Loops and Repeats.)
import turtle
t = turtle.Turtle()
for side in range(4):
t.forward(100)
t.left(90)
turtle.done()
This draws the exact same square, but the drawing instructions appear only once. The for side in range(4) line means "do the indented block 4 times." Shorter, clearer, and easier to change.
From squares to any polygon
Here is a beautiful trick. To draw any regular polygon, the turn angle is always 360 divided by the number of sides. A triangle turns 120 degrees (360 / 3), a pentagon turns 72 degrees (360 / 5), and so on.
import turtle
t = turtle.Turtle()
sides = 6 # change this to draw any shape
angle = 360 / sides
for side in range(sides):
t.forward(100)
t.left(angle)
turtle.done()
Change sides to 3 for a triangle, 5 for a pentagon, 8 for an octagon. One small change, a whole new shape. This is the power of letting the computer do the math.
Lifting the pen
Sometimes you want to move the turtle without drawing — for example, to start a new shape somewhere else. Use penup() to lift the pen and pendown() to put it back:
import turtle
t = turtle.Turtle()
# first square
for side in range(4):
t.forward(60)
t.left(90)
# move without drawing
t.penup()
t.forward(120)
t.pendown()
# second square
for side in range(4):
t.forward(60)
t.left(90)
turtle.done()
The penup() / pendown() pair leaves a gap between the two squares instead of a connecting line.
Wrapping drawings in a function
When you have a drawing you'll reuse, put it in a function so you can call it by name. (See Python Functions and Parameters for the full idea.)
import turtle
t = turtle.Turtle()
def draw_polygon(sides, length):
angle = 360 / sides
for side in range(sides):
t.forward(length)
t.left(angle)
draw_polygon(3, 80) # triangle
t.penup(); t.forward(150); t.pendown()
draw_polygon(6, 80) # hexagon
turtle.done()
Now draw_polygon is a reusable tool. Give it a number of sides and a length, and it draws that shape. The two inputs (sides and length) are parameters — they let one function draw many different shapes.
Making a spiral pattern
Now for something that looks impressive. If you draw lines while slowly turning and growing the length, you get a spiral:
import turtle
t = turtle.Turtle()
t.speed(0) # fastest drawing
for step in range(120):
t.forward(step) # each line is a little longer
t.left(59) # turn by 59 degrees
turtle.done()
Each time through the loop the line is one step longer (forward(step)) and the turtle turns 59 degrees. Because 59 doesn't divide evenly into 360, the lines never line up, and a hypnotic spiral appears. Try changing 59 to 90, 91, or 144 and watch how the whole pattern transforms.
Try it yourself
- Star: Draw a five-pointed star. Hint: loop 5 times,
forward(150)thenleft(144)each time. - Colorful shapes: Add
t.color("blue")before drawing, and tryt.fillcolor("yellow"),t.begin_fill()...t.end_fill()to fill a shape. - Pattern challenge: Use a loop inside a loop (a nested loop) to draw a square, turn a little, draw another square, and repeat — making a flower of overlapping squares.
import turtle
t = turtle.Turtle()
t.speed(0)
for turn in range(12): # 12 squares around a circle
for side in range(4): # draw one square
t.forward(100)
t.left(90)
t.left(30) # rotate before the next square
turtle.done()
Run that last one — it's a stunning pattern from just a few lines. Once you're comfortable, try combining turtle with what you learn in Making Decisions with If to draw different shapes based on a choice. Happy drawing!
Quick quiz
Test yourself and earn XP
What does `t.forward(100)` do?
forward(100) moves the turtle 100 pixels forward in whatever direction it currently faces, drawing a line if the pen is down.
How many degrees should the turtle turn at each corner to draw a square?
A square has four right-angle corners, so the turtle turns 90 degrees at each one (4 × 90 = 360).
What do penup() and pendown() control?
penup() lifts the pen so the turtle moves without drawing; pendown() puts it back so it draws again.
To draw a regular polygon with N sides, what turn angle do you use?
The exterior angles of any polygon add up to 360 degrees, so each turn is 360 / N degrees.
Why wrap drawing code inside a function?
A function lets you give a drawing a name and call it whenever you want, with different inputs, instead of copying the code.
FAQ
No. The turtle module comes built into Python, so a normal Python install from python.org already has it. You just write 'import turtle' at the top of your program.
You see your code as a picture right away, so mistakes and ideas are easy to spot. It also teaches loops, functions and angles in a fun, visual way before you move on to bigger projects.
Keep exploring
More in Coding