Unit 4 · Modules and File Handling
Turtle Graphics Programs
Solve practical turtle graphics problems — draw squares, circles, stars, spirals, patterns, and houses with step-by-step Python code.
Introduction
After learning the basic turtle commands, it is time to apply them in real programs. This chapter presents a collection of practical turtle graphics programs that are commonly asked in university practical exams and competitive programming assessments.
Each program includes the complete code, a step-by-step explanation, and a description of the expected output. Practice typing these programs yourself to build muscle memory and deepen your understanding.
Table of Contents
Program 1: Draw a Square
Problem Statement
Write a Python program to draw a square of side 150 pixels using turtle graphics.
import turtle
t = turtle.Turtle()
t.speed(5)
t.pensize(2)
t.pencolor("blue")
# Draw a square
for _ in range(4):
t.forward(150)
t.right(90)
turtle.done()
Step-by-Step Explanation
import turtle— imports the turtle module.t = turtle.Turtle()— creates a turtle object namedt.t.speed(5)— sets moderate drawing speed.- The
forloop runs 4 times: move forward 150 pixels, then turn right 90 degrees. - After 4 iterations, the turtle returns to its starting position, completing the square.
Output: A blue square of side 150 pixels drawn in the center of the screen.
Program 2: Draw a Circle
Problem Statement
Write a Python program to draw a circle of radius 100 pixels and fill it with a color.
import turtle
t = turtle.Turtle()
t.speed(8)
t.pencolor("darkred")
t.fillcolor("pink")
t.begin_fill()
t.circle(100)
t.end_fill()
turtle.done()
Step-by-Step Explanation
t.fillcolor("pink")— sets the interior color to pink.t.begin_fill()— starts recording the shape vertices.t.circle(100)— draws a circle of radius 100 pixels.t.end_fill()— fills the enclosed area with pink.
Output: A pink-filled circle with a dark red border drawn from the bottom-center point.
Program 3: Draw a Five-Pointed Star
Problem Statement
Write a Python program to draw a five-pointed star using turtle graphics.
import turtle
t = turtle.Turtle()
t.speed(7)
t.pensize(2)
t.pencolor("gold")
t.fillcolor("yellow")
t.begin_fill()
for _ in range(5):
t.forward(200)
t.right(144)
t.end_fill()
t.hideturtle()
turtle.done()
Step-by-Step Explanation
- A five-pointed star has 5 sides, each turned at an exterior angle of 144 degrees (360/5 = 72, exterior = 180 - 72 = 144).
- The loop runs 5 times: move forward 200 pixels, turn right 144 degrees.
begin_fill()andend_fill()fill the star with yellow.hideturtle()hides the turtle cursor for a cleaner look.
Output: A yellow-filled five-pointed star with a gold border.
Program 4: Draw a Spiral
Problem Statement
Write a Python program to draw a spiral pattern using turtle graphics.
import turtle
t = turtle.Turtle()
t.speed(0) # Fastest speed
t.pensize(1)
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
for i in range(120):
t.pencolor(colors[i % 6])
t.forward(i * 2)
t.right(61) # Slightly more than 60 degrees
t.hideturtle()
turtle.done()
Step-by-Step Explanation
- The loop runs 120 times, each time increasing the forward distance by
i * 2pixels. colors[i % 6]cycles through 6 colors using the modulus operator.- Turning 61 degrees (slightly more than 60) creates a spiraling effect instead of a closed hexagon.
- With
speed(0), the drawing completes quickly.
Output: A colorful spiral that grows outward from the center, cycling through rainbow colors.
Program 5: Draw a House
Problem Statement
Write a Python program to draw a simple house shape using turtle graphics.
import turtle
t = turtle.Turtle()
t.speed(5)
t.pensize(3)
# --- Draw the base of the house (rectangle) ---
t.pencolor("brown")
t.fillcolor("lightyellow")
t.begin_fill()
for _ in range(2):
t.forward(200)
t.left(90)
t.forward(150)
t.left(90)
t.end_fill()
# --- Draw the roof (triangle) ---
t.pencolor("darkred")
t.fillcolor("red")
t.begin_fill()
t.left(90) # Face upward
t.forward(150) # Move to top-left corner
t.right(150) # Angle for roof slope
t.forward(115) # Right side of roof
t.right(60) # Complete the triangle
t.forward(115) # Back to top-left
t.left(30) # Reset heading
t.end_fill()
# --- Draw the door (small rectangle) ---
t.penup()
t.goto(60, -75)
t.pendown()
t.pencolor("brown")
t.fillcolor("sienna")
t.begin_fill()
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(60)
t.left(90)
t.end_fill()
t.end_fill()
t.hideturtle()
turtle.done()
Step-by-Step Explanation
- The base is a rectangle (200 × 150) filled with light yellow.
- The roof is a triangle drawn on top of the rectangle, filled with red.
- The door is a smaller rectangle drawn at the bottom-center of the house, filled with brown.
penup()andgoto()are used to reposition the turtle without drawing.
Output: A simple house with a yellow base, red roof, and brown door.
Program 6: Draw a Colorful Geometric Pattern
Problem Statement
Write a program to draw a repeating geometric pattern using loops and color cycling.
import turtle
t = turtle.Turtle()
t.speed(0)
t.pensize(2)
colors = ["red", "blue", "green", "orange", "purple", "cyan"]
# Draw 6 overlapping squares rotated at different angles
for i in range(6):
t.pencolor(colors[i])
for _ in range(4):
t.forward(120)
t.left(90)
t.right(60) # Rotate before drawing next square
t.hideturtle()
turtle.done()
Step-by-Step Explanation
- The outer loop runs 6 times, each time changing the pen color.
- The inner loop draws a square (4 sides × 90° turn each).
- After each square,
right(60)rotates the turtle so the next square is offset by 60 degrees. - This creates a beautiful flower-like overlapping pattern.
Output: Six overlapping squares rotated at 60-degree intervals, each in a different color, forming a star/flower pattern.
Program 7: Draw Concentric Circles
Problem Statement
Write a program to draw 5 concentric circles with increasing radii.
import turtle
t = turtle.Turtle()
t.speed(5)
t.pensize(2)
colors = ["red", "orange", "yellow", "green", "blue"]
for i in range(5):
t.pencolor(colors[i])
radius = 30 + i * 30 # 30, 60, 90, 120, 150
t.circle(radius)
t.hideturtle()
turtle.done()
Step-by-Step Explanation
- The loop runs 5 times, each drawing a circle with an increasing radius.
radius = 30 + i * 30gives radii of 30, 60, 90, 120, and 150 pixels.- Each circle is drawn in a different color.
- All circles share the same starting point, creating a concentric pattern.
Output: Five concentric circles in different colors, like a target pattern.
University Exam Tips
Tip 1: Memorize the Angle Formula
For regular polygons: exterior angle = 360 / n, where n is the number of sides. For example: square = 90°, triangle = 120°, pentagon = 72°, hexagon = 60°.
Tip 2: Star Angle Formula
For a five-pointed star, the turn angle is 144 degrees. For a general star with k points: exterior angle = 180 - (360 / k) for convex, or 180 × (k-2) / k for the interior angle formula.
Tip 3: Always End with turtle.done()
Without turtle.done() or turtle.mainloop(), the graphics window will close immediately after the program finishes. Always include it as the last line in your turtle programs.
Practice Questions
Q1: Write a program to draw a hexagon using turtle graphics.
Hint: Use a loop of 6 iterations with forward() and right(60).
Q2: Write a program to draw a traffic light using turtle.
Hint: Draw a rounded rectangle for the body, then three filled circles for red, yellow, and green.
Q3: Modify the spiral program to draw with 10 colors instead of 6.
Hint: Add more colors to the list and change i % 6 to i % 10.
Q4: Write a program to draw your initials (e.g., "AB") using turtle graphics.
Hint: Use penup(), goto(), and pendown() to position each letter. Use line segments and angles to draw letter shapes.
Q5: What angle should you use to draw a regular pentagon?
Answer: 72 degrees (360 / 5 = 72). Turn right(72) or left(72) five times.
Key Points
Exterior angle of a regular polygon = 360 / n (where n = number of sides).
Five-pointed star uses a turn angle of 144 degrees.
Use penup() and goto() to reposition the turtle without drawing.
Color cycling with i % len(colors) creates rainbow patterns.
speed(0) sets the fastest drawing speed for long programs.
Always call turtle.done() to keep the window open after drawing.
Use begin_fill() / end_fill() to create filled shapes.
Nested loops are useful for creating repeating geometric patterns.
Summary
This chapter covered seven practical turtle graphics programs ranging from simple shapes (squares, circles) to complex patterns (spirals, concentric circles, houses). Understanding the angle formulas and loop structures is key to creating any turtle graphics program. Practice these programs and modify them with different colors, sizes, and shapes to build confidence for exams.