← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 11 of 15

Python Turtle Graphics

What is Turtle Graphics?

Turtle graphics is a beginner-friendly way to learn programming concepts visually: you control an on-screen turtle with drawing commands, and its trail traces out shapes as it moves. It requires an actual graphical display to run -- browser-based environments like Pyodide can render it directly in a web page.

Example: What is Turtle Graphics?

python
import turtle
t = turtle.Turtle()
print(type(t))

Moving the Turtle

The turtle is moved with forward() and backward() to travel in the direction it's currently facing, and left()/right() to rotate it by a given number of degrees before its next forward movement -- this relative movement model (not absolute x/y coordinates) is what makes turtle graphics intuitive for drawing shapes.

Example: Moving the Turtle

python
import turtle
t = turtle.Turtle()
t.forward(100)
t.left(90)
t.forward(50)

Drawing Basic Shapes

Repeating a short sequence of forward-and-turn commands inside a loop is how you draw regular shapes: a square is four repetitions of 'move forward, turn 90 degrees,' and any regular polygon follows the same pattern with a different turn angle and repeat count.

Example: Drawing Basic Shapes

python
import turtle
t = turtle.Turtle()
for _ in range(4):
    t.forward(100)
    t.left(90)

Customizing Colors and Pens

color() sets the pen's drawing color, pensize() sets how thick the drawn line is, and speed() controls how fast the turtle animates its movement -- purely cosmetic controls that don't affect the shape being drawn, only how it looks and how quickly it appears.

Example: Customizing Colors and Pens

python
import turtle
t = turtle.Turtle()
t.color("blue")
t.pensize(3)
t.speed(1)
t.forward(100)

Filling Shapes with Color

begin_fill() starts recording the outline of a shape for filling, and end_fill() closes that outline and fills the enclosed area with the current color. Forgetting to call end_fill() after begin_fill() leaves the shape outlined but unfilled, since the fill only actually renders once the pair is complete.

Example: Filling Shapes with Color

python
import turtle
t = turtle.Turtle()
t.color("red")
t.begin_fill()
for _ in range(4):
    t.forward(100)
    t.left(90)
t.end_fill()

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.