Turtle Graphics is a beginner-friendly module in Python used to draw shapes and patterns by controlling a virtual cursor called the turtle.
Turtle Graphics provides a virtual drawing canvas. A turtle (cursor) moves around the screen based on commands you give it. As it moves, it draws lines — just like a pen on paper.
It is widely used to teach programming concepts such as loops, functions, and coordinates in a visual way.
Before using Turtle Graphics, you must import the turtle module:
import turtle
After importing, all turtle functions are accessed using the turtle. prefix.
| Function | Description |
|---|---|
turtle.forward(d) | Move the turtle forward by d pixels in the current direction |
turtle.backward(d) | Move the turtle backward by d pixels (heading unchanged) |
turtle.goto(x, y) | Move the turtle directly to coordinates |
| Function | Description |
|---|---|
turtle.right(angle) | Turn the turtle clockwise by angle degrees |
turtle.left(angle) | Turn the turtle counter-clockwise by angle degrees |
| Function | Description |
|---|---|
turtle.penup() | Lift the pen — turtle moves without drawing |
turtle.pendown() | Lower the pen — turtle draws as it moves |
turtle.pensize(width) | Set the thickness of the drawing line |
| Function | Description |
|---|---|
turtle.color('color') | Set both pen and fill color |
turtle.pencolor('color') | Set only the pen (trail) color |
turtle.fillcolor('color') | Set only the fill color |
| Function | Description |
|---|---|
turtle.reset() | Clear the drawing and return turtle to with default settings |
turtle.clear() | Clear the drawing but keep the turtle's current position |
turtle.done() | Keep the window open after drawing is complete |
When a Turtle Graphics window opens, the turtle starts at the center of the screen, which is the origin in the Cartesian coordinate system. The turtle initially faces right (east, ).
Using a for loop makes it easy to draw regular shapes.
import turtle
for i in range(4):
turtle.forward(100)
turtle.right(90)
turtle.done()
Explanation: A square has 4 sides. At each corner, the turtle turns clockwise. After 4 iterations, the square is complete.
import turtle
for i in range(3):
turtle.forward(100)
turtle.right(120)
turtle.done()
Explanation: An equilateral triangle has 3 sides. The exterior angle at each corner is .
For a regular polygon with sides, the turtle turns:
| Shape | Sides () | Turn Angle |
|---|---|---|
| Triangle | 3 | |
| Square | 4 | |
| Pentagon | 5 | |
| Hexagon | 6 |
import turtle
turtle.forward(100) # Draw first line
turtle.penup() # Lift pen
turtle.forward(50) # Move without drawing
turtle.pendown() # Lower pen
turtle.forward(100) # Draw second line
turtle.done()
This draws two separate lines with a gap between them.
import turtle
turtle.pencolor('blue')
turtle.pensize(3)
for i in range(4):
turtle.forward(100)
turtle.right(90)
turtle.done()