In Python, there is a fun drawing library called turtle. It allows you to “draw” by writing code! When you run a turtle program, a white window appears on the screen, and a small triangle shows up in the center — that’s your “pen turtle.” By giving it commands, you can make it move forward, turn, lift or lower the pen, and even change colors or fill shapes. Each command leaves a visible trail, just like a real pen drawing on paper.
To use turtle, you start by importing it. Python already includes this library, so no installation is needed.
import turtle
Then you create your turtle — your virtual pen:
t = turtle.Turtle()
If you make it move forward, for example by 100 pixels:
t.forward(100)
You’ll see the turtle draw a line to the right. The turtle starts in the middle of the screen, facing east (right). You can imagine the screen as a coordinate plane, with the x-axis pointing right and the y-axis pointing up.
To change direction, you can make the turtle turn right or left:
t.right(90)
t.forward(100)
This means the turtle turns right by 90 degrees (facing downward) and then moves 100 pixels. By combining “forward” and “turn,” you can draw all sorts of shapes.
Next, you’ll learn how to move the turtle without drawing lines. You can lift the pen using penup() and lower it again with pendown(). The following example shows how to move to a new location and continue drawing from there:
t.penup()
t.goto(-100, 100)
t.pendown()
t.forward(200)
Here, the turtle jumps to a new position in the upper-left area, then draws a horizontal line. The command goto(x, y) moves the pen to an exact coordinate, giving you precise control of where to start drawing.
You can also set the pen color and thickness:
t.color("blue")
t.pensize(5)
t.forward(150)
This draws a thick blue line. Colors can be written as English words like “red,” “green,” or “purple,” or as hexadecimal color codes like “#00FF00.”
Sometimes you want to fill a shape with color. You can do that like this:
t.color("black")
t.fillcolor("yellow")
t.begin_fill()
for i in range(4):
t.forward(100)
t.right(90)
t.end_fill()
begin_fill() starts filling, and end_fill() stops it. You’ll see a yellow square with a black outline. Python automatically fills the area enclosed by your path.
If your turtle moves slowly, you can change its speed. t.speed(0) makes it move instantly. You can also control the screen refresh for smoother animation when drawing many lines:
screen = turtle.Screen()
screen.tracer(False)
for angle in range(0, 360, 10):
t.setheading(angle)
t.forward(120)
t.backward(120)
screen.update()
This draws lines radiating out from the center like sunbeams. tracer(False) turns off automatic refreshing, and update() redraws everything at once.
When you want to bring the turtle back to the center, use t.home(). To face a specific direction (for example, straight up), use t.setheading(90). You can also draw circles or arcs with t.circle(r):
t.home()
t.color("green")
t.circle(80)
t.circle(60, 180)
circle(80) draws a full circle with a radius of 80, while circle(60, 180) draws a half circle of radius 60.
As you continue experimenting, you’ll discover that by controlling direction and step size, you can draw almost anything. Every movement in turtle is a mix of math and programming. What you’re really learning is how to express position, angle, and shape using code.