Unit 4 · Modules and File Handling
Turtle Graphics
Learn how to use Python's built-in Turtle module to create graphics, draw shapes, and understand the fundamentals of programmatic drawing.
Introduction
Turtle Graphics is one of the most exciting ways to learn Python programming visually. The turtle module comes built into Python's standard library, so no installation is needed. It provides a virtual turtle that moves around the screen, leaving a trail (pen) behind it to create drawings.
Turtle graphics is an excellent starting point for understanding coordinate systems, loops, functions, and modular programming. It is also a frequently asked topic in university practical exams.
University Definition
Turtle Graphics is a built-in Python module that provides a virtual canvas and a cursor (called a "turtle") that can be programmed to move and draw on the screen. It is based on the LOGO programming language and is used to teach programming concepts through visual feedback.
Table of Contents
What is Turtle Graphics?
Turtle Graphics works by controlling a virtual "turtle" on the screen. The turtle has:
- A position (x, y coordinates on the screen)
- A direction (heading — which way it's facing)
- A pen that can be up or down (drawing or not drawing)
- A color, speed, and shape
The default screen is 400x300 pixels with the turtle starting at the center (0, 0). The x-axis goes from left (-200) to right (200), and the y-axis goes from bottom (-150) to top (150).
Real-life Analogy: Imagine placing a pen on a piece of paper and commanding it to move forward, turn left, move again, etc. The trail left by the pen creates the drawing — this is exactly how turtle graphics works.
Importing and Setting Up
There are several ways to import and use the turtle module:
# Method 1: Import the entire module
import turtle
# Method 2: Import with an alias
import turtle as t
# Method 3: Import specific functions
from turtle import *
# Create a screen (optional)
screen = turtle.Screen()
screen.title("My Turtle Program")
screen.bgcolor("white")
screen.setup(width=600, height=500)
# Create a turtle object
pen = turtle.Turtle()
pen.shape("turtle")
pen.color("blue")
University Exam Tip
In exams, always use import turtle and create a turtle object explicitly (e.g., t = turtle.Turtle()). This demonstrates proper modular programming practice and avoids namespace pollution.
Movement Commands
These are the core commands to move the turtle around the screen:
| Command | Description |
|---|---|
| forward(d) / fd(d) | Move forward by d pixels |
| backward(d) / bk(d) | Move backward by d pixels |
| right(d) / rt(d) | Turn right by d degrees |
| left(d) / lt(d) | Turn left by d degrees |
| setheading(angle) | Set the turtle's heading to angle (0=East, 90=North) |
| goto(x, y) | Move turtle to absolute position (x, y) |
| home() | Return turtle to origin (0, 0) facing East |
import turtle t = turtle.Turtle() # Move forward and turn t.forward(100) # Draw a line 100 pixels long t.right(90) # Turn 90 degrees right t.forward(100) # Draw another line # Check position print(t.pos()) # (100.00, -100.00) print(t.heading()) # 270.0 (facing South) turtle.done()
Pen Control Commands
Pen control determines whether the turtle draws as it moves and the appearance of the drawn lines:
| Command | Description |
|---|---|
| penup() / pu() | Lift the pen — turtle moves without drawing |
| pendown() / pd() | Lower the pen — turtle draws while moving |
| pensize(width) | Set the pen width in pixels |
| pencolor(color) | Set the pen color (e.g., "red", "#FF0000") |
| fillcolor(color) | Set the fill color for shapes |
| begin_fill() | Start recording vertices for filling |
| end_fill() | Fill the shape drawn since begin_fill() |
| speed(speed) | Set drawing speed (0=fastest, 1=slowest, 10=fast) |
import turtle
t = turtle.Turtle()
t.speed(3) # Moderate speed
t.pensize(3) # Thick pen
t.pencolor("blue") # Blue lines
# Draw a filled square
t.fillcolor("yellow")
t.begin_fill()
for _ in range(4):
t.forward(100)
t.right(90)
t.end_fill()
turtle.done()
Turtle Appearance
You can customize how the turtle looks on screen:
import turtle
t = turtle.Turtle()
# Shape: "arrow", "turtle", "circle", "square", "triangle", "classic"
t.shape("turtle")
# Color of the turtle shape
t.color("green")
# Hide the turtle cursor
t.hideturtle()
# Show the turtle cursor again
t.showturtle()
# Stamp a copy of the turtle shape at current position
t.stamp()
turtle.done()
Examples with Output
Example 1: Drawing a Simple Line
import turtle t = turtle.Turtle() t.forward(200) # Draws a horizontal line from center to the right turtle.done() # Output: A single horizontal blue line of 200 pixels
Example 2: Drawing a Triangle
import turtle
t = turtle.Turtle()
t.pensize(2)
t.pencolor("red")
for _ in range(3):
t.forward(150)
t.left(120)
turtle.done()
# Output: An equilateral triangle drawn with red lines
Example 3: Drawing a Filled Rectangle
import turtle
t = turtle.Turtle()
t.color("black", "lightblue") # pen color, fill color
t.begin_fill()
for _ in range(2):
t.forward(200)
t.left(90)
t.forward(100)
t.left(90)
t.end_fill()
turtle.done()
# Output: A filled lightblue rectangle (200x100 pixels)
Example 4: Drawing a Circle
import turtle
t = turtle.Turtle()
t.speed(5)
t.pensize(3)
t.pencolor("darkgreen")
t.circle(80) # Circle with radius 80
t.penup()
t.forward(200)
t.pendown()
t.circle(40) # Smaller circle with radius 40
turtle.done()
# Output: Two circles — a large one and a smaller one to the right
Example 5: Drawing a Star
import turtle
t = turtle.Turtle()
t.speed(8)
t.pensize(2)
t.pencolor("purple")
t.hideturtle()
for _ in range(5):
t.forward(150)
t.right(144)
turtle.done()
# Output: A five-pointed star drawn in purple
Example 6: Drawing Using Absolute Coordinates
import turtle t = turtle.Turtle() t.speed(5) # Draw an "L" shape using coordinates t.penup() t.goto(-100, -100) t.pendown() t.goto(-100, 100) # Vertical line up t.goto(-50, 100) # Horizontal line right # Move to another position t.penup() t.goto(50, -100) t.pendown() t.goto(50, 100) # Vertical line t.goto(100, 100) # Horizontal line turtle.done() # Output: Two L-shaped figures on the screen
Example 7: Pen Up/Down for Gap Drawing
import turtle
t = turtle.Turtle()
t.speed(5)
for _ in range(4):
t.forward(100)
t.penup()
t.forward(20) # Gap — no drawing
t.pendown()
turtle.done()
# Output: A square with gaps at each corner
Key Points
The turtle module is part of Python's standard library — no pip install required.
Default screen size is 400x300 pixels with (0,0) at the center.
The turtle starts facing East (0 degrees) at position (0, 0).
Use penup()/pendown() to move without drawing.
Use speed(0) for the fastest drawing speed.
begin_fill() and end_fill() are used to fill shapes with color.
Use turtle.done() or turtle.mainloop() to keep the window open.
circle(radius) draws a circle; stamp() leaves a turtle-shaped stamp.
Turn angles: left() is counterclockwise, right() is clockwise.
Heading values: 0°=East, 90°=North, 180°=West, 270°=South.
Practice Questions
Q1: Write a Python program to draw a square using turtle graphics.
Hint: Use a for loop with forward() and right(90) four times.
Q2: What is the difference between penup() and pendown()?
Answer: penup() lifts the pen so the turtle moves without drawing. pendown() lowers the pen so it draws while moving.
Q3: How do you fill a shape with color in turtle graphics?
Answer: Use fillcolor() to set the fill color, then begin_fill() before drawing and end_fill() after drawing.
Q4: Write a program to draw a five-pointed star using turtle.
Hint: Use a for loop 5 times with forward(150) and right(144).
Q5: What is the default starting position and heading of the turtle?
Answer: The turtle starts at (0, 0) facing East (heading = 0 degrees).
Summary
Turtle Graphics is a built-in Python module for creating drawings programmatically.
Use import turtle and create a Turtle object to start drawing.
Movement commands: forward(), backward(), left(), right(), goto().
Pen commands: penup(), pendown(), pensize(), pencolor().
Fill shapes using begin_fill(), end_fill(), and fillcolor().
Always end with turtle.done() to keep the graphics window open.