Turtle is a drawing tool built into Python itself — no download, no
install, it is already there. When you run a Turtle program, a drawing
window opens with a small arrow (the “turtle”) in the middle. Your
instructions move the turtle, and it draws a line wherever it walks.
Because Turtle is part of Python’s standard library, it works in IDLE
exactly like everything else in this course: write the program in a
file, run it, and watch the drawing window appear.
A note on this lesson’s code: Turtle draws pictures on screen, so its
programs cannot be run and checked on a machine with no screen the way
the other lessons’ code was. Every snippet below was checked for
correct syntax and uses only the standard Turtle commands listed in
this lesson — but you should run each one in IDLE yourself and watch
it draw, which is the whole point.
Start with the two commands you will use most. forward() moves the
turtle ahead by a number of steps (think pixels on screen).right() turns it clockwise by a number of degrees.
import turtle
t = turtle.Turtle()
t.forward(100)
t.right(90)
t.forward(100)
turtle.done()
Run this in IDLE. The turtle draws a line 100 steps long, turns right
by 90 degrees (a quarter turn), and draws a second line. Two lines, an
L shape.
Three things to understand in this snippet:
import turtle loads the Turtle tool. Without it, Python has noturtle means.t = turtle.Turtle() creates your turtle and stores it in thet. From now on, t.forward(...) means “my turtle, moveturtle.done() at the end keeps the drawing window open so you canbackward() moves the turtle back the way it faces, and left()
turns it counter-clockwise. Those four moves — forward,backward, right, left — are enough to draw anything made of
straight lines.
Four lines and four quarter-turns make a square. Written out longhand:
import turtle
t = turtle.Turtle()
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
t.forward(100)
t.right(90)
turtle.done()
That works, but it repeats the same two lines four times — exactly the
problem loops were invented to solve in Lesson 9:
import turtle
t = turtle.Turtle()
for side in range(4):
t.forward(100)
t.right(90)
turtle.done()
Same square, in a third of the lines. The loop says “do these two
moves four times.” This is the moment loops stop being abstract: you
can see why repeating without retyping matters.
color() changes the pen’s color. penup() lifts the pen so the
turtle moves without drawing; pendown() puts it back down. Together
they let you draw in several colors without connecting lines:
import turtle
t = turtle.Turtle()
t.color("red")
for side in range(4):
t.forward(100)
t.right(90)
t.penup()
t.forward(150)
t.pendown()
t.color("blue")
t.circle(50)
turtle.done()
Run it: a red square, then the turtle silently moves 150 steps to the
side (pen up, no line), then draws a blue circle with circle(50),
which draws a circle of radius 50. Color names are plain English
words in quotes: "red", "blue", "green", "purple", "orange".
If you misspell one, Turtle will tell you — another friendly error
message, pointing at the exact problem.
You now know enough to invent. A triangle is three sides and three
turns of 120 degrees (because 3 × 120 = 360, a full turn). A house is
a square plus a triangle roof. A star takes five sides and 144-degree
turns — try it and see why the angle is not the obvious one.
The habit to build here is the same one from every lesson: guess,
run, look at what actually happened, and adjust. With Turtle, the
feedback is a picture, which makes the guess-and-check loop feel like
play. That is not an accident — it is why Turtle has been a
beginner’s favorite for decades.
Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 11 slides (PowerPoint, with narration)
Download the Lesson 11 slides (PDF)
penup() andpendown() to move between them without a connecting line.In the lesson, a square is 4 sides with 90-degree turns, and a triangle is 3 sides with 120-degree turns. Here the turn is always 360 divided by the number of sides, so any number of sides closes neatly into a shape.
Try this: Change the sides to 3 and press Run again — the square becomes a triangle, with 120-degree turns.
Press Run to draw.
Save this as lesson11-practice.py and run it with F5 in IDLE.
# Lesson 11: Drawing with Turtle
# WHAT TO DO: Turtle is a drawing tool built into Python. This program
# draws a shape with a loop: go forward, turn right, repeat. A square
# needs 4 sides, but the loop below repeats only 3 times. Fix line 18:
# change range(3) to range(4).
# HINT: Each repeat draws one side and one 90-degree turn, so 4 repeats
# make a square. Only change the number 3.
# NOTE: On a computer with no screen, Turtle cannot open its drawing
# window, so the program prints a message instead of drawing. That is
# expected there — run it in IDLE on your own computer to see the square.
# Run with F5 in IDLE. You are done when the drawing is a closed square.
import turtle
try:
t = turtle.Turtle()
t.color("blue")
for side in range(3): # <-- CHANGE THIS LINE: a square has 4 sides
t.forward(100)
t.right(90)
turtle.done()
except Exception:
print("Turtle needs a screen to draw.")
print("In IDLE, this program draws a square: 4 sides of 100 steps.")
import turtle, then t = turtle.Turtle().forward() and backward() move; right() and left() turn in degrees.color() changes pen color; penup()/pendown() control whether movement draws.circle(50) draws a circle of radius 50; turtle.done() keeps the window open.Next lesson: Lists.
import turtle do, and why is it needed?t.forward(100) and t.right(90) do?penup() and pendown() do?turtle.done() do, and what happens without it?Answer key: 1. It loads the Turtle tool; without it Python does not know what turtle means. 2. Move forward 100 steps; turn right 90 degrees. 3. It says the two moves once and repeats them four times, instead of retyping them. 4. penup() lifts the pen so movement draws nothing; pendown() puts it back down to draw again. 5. It keeps the drawing window open; without it the window may close at once.