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

Python Turtle Graphics

Turtle graphics lets you draw pictures by steering a little turtle around the screen, like guiding a pen with commands. Forward, turn, and you have made a shape.
Syntax
python
import turtle

t = turtle.Turtle()
t.forward(distance)
t.left(angle)
turtle.done()

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()  # the on-screen turtle that draws as it moves
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)  # moves in the direction it's currently facing
t.left(90)  # rotates 90 degrees before the next move
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):  # four repetitions traces out a square
    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")  # sets the pen's drawing color
t.pensize(3)  # sets the line thickness
t.speed(1)  # controls animation speed, purely cosmetic
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()  # starts recording the outline to fill
for _ in range(4):
    t.forward(100)
    t.left(90)
t.end_fill()  # closes the outline and fills it with the current color
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not calling turtle.done() or mainloop(), so the drawing window closes immediately.
  2. Naming your own file turtle.py, which shadows the module and breaks the import.
  3. Forgetting begin_fill() before drawing and end_fill() afterwards, so shapes are not filled.

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.