Python Turtle Graphics
In this page:
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?
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
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
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
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
import turtle
t = turtle.Turtle()
t.color("red")
t.begin_fill()
for _ in range(4):
t.forward(100)
t.left(90)
t.end_fill()
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects