Lesson 11: Drawing with Turtle: Shapes, Colors, and Loops You Can See


What you'll learn in this lesson


Meet the turtle

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.


Moving and turning

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:

backward() 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.


A square, then a square with a loop

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 10:

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.


Colors and lifting the pen

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.


Design a shape of your own

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.


Hands-on exercise

  1. Draw a square with a loop, exactly like the lesson, and run it in IDLE.
  2. Change the loop to draw a triangle (3 sides, 120-degree turns) and run it.
  3. Draw two shapes in different colors, using penup() and pendown() to move between them without a connecting line.
  4. Invent one shape of your own — a house, a star, or anything else — and write down how many sides and what turn angle it needed.

Quick quiz

  1. What does import turtle do, and why is it needed?
  2. What do t.forward(100) and t.right(90) do?
  3. Why is the loop version of the square better than writing all eight lines out?
  4. What do penup() and pendown() do?
  5. What does 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.


Key takeaways

Next lesson: Lists.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson11-practice.pyDownload the solution

# 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.")

Save the file as lesson11-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

Check yourself

Five quick questions on this lesson. Take the Lesson 11 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.