Python Turtle Graphics — Core Concepts
What turtle does
Python’s turtle module provides a canvas and a programmable cursor (the turtle) that draws lines as it moves. It is part of the standard library — no installation needed. Turtle is built on top of tkinter, Python’s default GUI toolkit.
import turtle
t = turtle.Turtle()
t.forward(100)
t.left(90)
t.forward(100)
turtle.done()
This opens a window with a turtle that draws two lines forming an L-shape.
Coordinate system
The turtle starts at (0, 0) — the center of the window — facing right (east). The canvas uses standard Cartesian coordinates: positive x is right, positive y is up. You can reposition the turtle with:
t.goto(100, 50) # move to (100, 50), drawing a line
t.setx(200) # change x only
t.sety(-100) # change y only
t.home() # return to (0, 0) facing east
Query position and heading:
x, y = t.position()
angle = t.heading() # degrees, 0 = east, 90 = north
Movement commands
Core movement methods:
forward(distance)/fd(distance)— move in the current directionbackward(distance)/bk(distance)— move in reverseleft(angle)/lt(angle)— turn counterclockwiseright(angle)/rt(angle)— turn clockwisecircle(radius, extent=360)— draw a circle or arcspeed(n)— set drawing speed: 1 (slowest) to 10, or 0 (instant)
Pen control
Control whether the turtle draws and how:
t.penup() # lift pen (move without drawing)
t.pendown() # lower pen (draw while moving)
t.pensize(3) # line width in pixels
t.pencolor("red") # line color (name, hex, or RGB tuple)
t.pen(pencolor="blue", pensize=2, speed=5) # set multiple at once
Color and filling
Fill closed shapes with color:
t.fillcolor("yellow")
t.begin_fill()
for _ in range(4):
t.forward(100)
t.left(90)
t.end_fill()
Set both pen and fill color simultaneously:
t.color("red", "orange") # pen=red, fill=orange
Use RGB tuples with turtle.colormode(255):
turtle.colormode(255)
t.pencolor(65, 105, 225) # royal blue
Drawing patterns with loops
Turtle shines when combined with loops. A simple star:
for _ in range(5):
t.forward(150)
t.right(144)
A spiral:
for i in range(100):
t.forward(i * 2)
t.left(91)
Nested loops create complex patterns:
for _ in range(36):
for _ in range(4):
t.forward(80)
t.left(90)
t.left(10)
This draws 36 squares, each rotated 10 degrees — producing a circular pattern.
Stamps and shapes
The turtle cursor can take different shapes:
t.shape("turtle") # arrow, turtle, circle, square, triangle, classic
Stamp the current shape onto the canvas:
for _ in range(12):
t.stamp()
t.forward(50)
t.left(30)
Register custom shapes from images:
turtle.register_shape("rocket.gif")
t.shape("rocket.gif")
Event handling
Make programs interactive with keyboard and mouse events:
def move_forward():
t.forward(20)
def turn_left():
t.left(15)
screen = turtle.Screen()
screen.onkey(move_forward, "Up")
screen.onkey(turn_left, "Left")
screen.listen()
turtle.done()
Mouse click events:
def draw_dot(x, y):
t.penup()
t.goto(x, y)
t.dot(10, "blue")
screen.onclick(draw_dot)
Screen control
Configure the window:
screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("My Drawing")
Save the drawing:
canvas = screen.getcanvas()
canvas.postscript(file="drawing.eps")
For PNG output, convert the EPS with Pillow or Ghostscript.
Writing text
Draw text on the canvas:
t.penup()
t.goto(-100, 100)
t.write("Hello, Turtle!", font=("Arial", 24, "bold"), align="center")
The align parameter accepts "left", "center", or "right".
The one thing to remember: Turtle translates Python control flow — loops, functions, conditionals — into visible movement on a canvas, making abstract programming concepts concrete and immediate.
See Also
- Python Arcade Library Think of a magical art table that draws your game characters, listens when you press buttons, and cleans up the mess — that's Python Arcade.
- Python Audio Fingerprinting Ever wonder how Shazam identifies a song from just a few seconds of noisy audio? Audio fingerprinting is the magic behind it, and Python can do it too.
- Python Barcode Generation Picture the stripy labels on grocery items to understand how Python can create those machine-readable barcodes from numbers.
- Python Cellular Automata Imagine a checkerboard where each square follows simple rules to turn on or off — and suddenly complex patterns emerge like magic.
- Python Godot Gdscript Bridge Imagine speaking English to a friend who speaks French, with a translator in the middle — that's how Python talks to the Godot game engine.