(Grade X)Chapter 4- Programming in Python-Part 4: Graphics Using Turtle
Introduction to Turtle Graphics
The Turtle library is a built-in Python graphics library used to create drawings, shapes, and simple animations. It allows users to control a virtual turtle (cursor) that moves around the screen according to commands. It is commonly used to learn programming and computer graphics.
Importing the library:
import turtle
t = turtle.Turtle()
Basic Movement Functions
The turtle understands direction in terms of forward/backward movement and left/right turning, similar to how a person would walk and turn. The table below lists the most common movement functions.
Function | Purpose | Example |
forward(x) or fd(x) | Moves the turtle forward by x pixels in the direction it is facing. | t.forward(100) |
backward(x) or bk(x) | Moves the turtle backward by x pixels. | t.backward(50) |
left(angle) | Turns the turtle left (anticlockwise) by the given angle in degrees. | t.left(90) |
right(angle) | Turns the turtle right (clockwise) by the given angle in degrees. | t.right(90) |
goto(x, y) | Moves the turtle to the given coordinate point. | t.goto(0, 0) |
speed(n) | Sets the drawing speed from 0 (fastest) to 10 (slowest). | t.speed(5) |
Example 1: Drawing a Square
A square has 4 equal sides and each turn between sides is 90°. A for loop is used to repeat the forward and turn commands four times.
import turtle screen = turtle.Screen() screen.bgcolor("white") t = turtle.Turtle() t.speed(0) t.pensize(3) t.color("blue") for i in range(4): t.forward(120) t.right(90) screen.mainloop() |
Output:
Example 2: Drawing a Triangle
An equilateral triangle has 3 equal sides. The exterior angle at each corner is 120° (360° ÷ 3).
import turtle screen = turtle.Screen() t = turtle.Turtle() t.speed(0) t.pensize(3) t.color("green") for i in range(3): t.forward(150) t.left(120) screen.mainloop() |
Output:
Pen Control: penup(), pendown() and pensize()
Sometimes we need to move the turtle without drawing a line — for example, to shift its starting position. This is done using penup() and pendown().
Function | Purpose | Example |
penup() or pu() | Lifts the pen up so the turtle moves without drawing. | t.penup() |
pendown() or pd() | Puts the pen down so the turtle draws again. | t.pendown() |
pensize(x) or width(x) | Sets the thickness of the line drawn. | t.pensize(3) |
Example 3: Using penup() and pendown()
This program draws a line, lifts the pen to leave a gap, then draws another line — producing a broken (dashed-style) line.
import turtle t = turtle.Turtle() t.speed(0) t.pensize(3) t.forward(100) # draws a line t.penup() # lifts the pen t.forward(60) # moves without drawing t.pendown() # puts the pen down again t.forward(100) # draws another line turtle.done() |
Output:
The turtle module lets us change the colour of the line (pen colour) and the colour used to fill closed shapes (fill colour).
Function | Purpose | Example |
color(c) | Sets both the pen colour and fill colour to c. | t.color("red") |
pencolor(c) | Sets only the colour of the line drawn. | t.pencolor("blue") |
fillcolor(c) | Sets only the colour used to fill a shape. | t.fillcolor("yellow") |
begin_fill() | Marks the start of a shape that will be filled. | t.begin_fill() |
end_fill() | Fills the shape drawn since begin_fill() with the fill colour. | t.end_fill() |
Example 4: Filling a Square with Colour
import turtle t = turtle.Turtle() t.speed(0) t.pensize(3) t.color("black") # pen (outline) colour t.fillcolor("yellow") # fill colour t.begin_fill() for i in range(4): t.forward(120) t.right(90) t.end_fill() turtle.done() |
Output:
Drawing Circles
The circle(radius) function draws a circle directly, without needing a loop. A positive radius draws the circle to the left of the turtle's direction, and a negative radius draws it to the right.
Example 5: Drawing a Circle
import turtle t = turtle.Turtle() t.speed(0) t.pensize(3) t.color("purple") t.circle(100) # draws a circle of radius 100 pixels turtle.done() |
Output:
Changing the Turtle's Shape
By default, the turtle appears as a small arrow. The shape(name) function can change how the turtle itself looks on screen. Built-in shapes include "arrow", "turtle", "circle", "square", "triangle" and "classic".
Example 6: Changing the Turtle Shape
import turtle t = turtle.Turtle() t.shape("turtle") # changes the cursor to a turtle icon t.color("darkgreen") t.speed(0) t.pensize(2) for i in range(4): t.forward(100) t.right(90) turtle.done() |
Output:
Drawing Patterns Using Loops
Turtle graphics becomes really powerful when combined with loops (for or while). By repeating a movement with a changing angle or length, we can create stars, spirals and other patterns.
Example 7: Drawing a Star
A five-pointed star can be drawn by moving forward and turning by 144° (360° × 2 ÷ 5), repeated five times.
import turtle t = turtle.Turtle() t.speed(0) t.pensize(3) t.color("red") for i in range(5): t.forward(150) t.right(144) turtle.done() |
Output:
Example 8: Drawing a Spiral
A spiral is drawn by slightly increasing the forward distance on every repetition of the loop.
import turtle t = turtle.Turtle() t.speed(0) t.pensize(2) t.color("blue") for i in range(60): t.forward(i * 2) t.right(45) turtle.done() |
Output:
Step-by-Step Explanation
Step 1: Import the turtle module: import turtle
This brings in the turtle library, which lets
us draw shapes on screen using a "turtle" (like a pen) that moves
around.
This
creates our drawing turtle and names it t. Think of t as our pen/pencil that
will draw on the screen.
This
sets the turtle's speed to the fastest setting (0 = fastest, 1 = slowest, up to
10).
This
makes the line thickness 2 pixels wide, so the drawing is clearly visible.
This
makes the turtle draw in blue color.
for
i in range(60):
t.forward(i * 2)
t.right(45)
This
is a for loop that repeats 60 times (i takes values from 0 to 59).
·
t.forward(i
* 2) → moves the turtle forward by a distance of i × 2 pixels.
Since i increases every time (0, 1, 2,
3...), the distance grows bigger each round (0, 2, 4, 6, 8...). This is why the
spiral gets bigger and bigger.
·
t.right(45)
→ turns the turtle 45 degrees to the right after each move.
Because
the turtle keeps turning by a fixed angle (45°) while moving a growing
distance, it creates a spiral pattern.
This
keeps the drawing window open so students can see the final spiral instead of
it closing immediately.