Editor

Sierpinski Triangle in Python Turtle

Intermediate · Fractals · 45 lines · draws in about 10 seconds

A Sierpinski triangle is three half-size Sierpinski triangles with a hole in the middle, so the function that draws one calls itself three times.

Sierpinski Triangle in Python Turtle: the finished drawing
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("#1d1f3f")
screen.title("Sierpinski triangle")
t = turtle.Turtle()
t.speed(9)
t.color("darkorange", "gold")
def draw_triangle(x, y, size):
# A filled triangle with its bottom left corner at (x, y)
height = size * math.sqrt(3) / 2
t.penup()
t.goto(x, y)
t.pendown()
t.begin_fill()
t.goto(x + size, y)
t.goto(x + size / 2, y + height)
t.goto(x, y)
t.end_fill()
def sierpinski(x, y, size, depth):
# Base case: a plain triangle
if depth == 0:
draw_triangle(x, y, size)
return
# Recursive case: three half-size Sierpinski triangles,
# bottom left, bottom right and top, with a hole in the middle
half = size / 2
height = half * math.sqrt(3) / 2
sierpinski(x, y, half, depth - 1)
sierpinski(x + half, y, half, depth - 1)
sierpinski(x + half / 2, y + height, half, depth - 1)
# One big triangle, 440 wide, centred on the screen
sierpinski(-220, -190, 440, 5)
t.hideturtle()
turtle.done()

How the code works

One filled triangle

The smallest pieces are plain triangles. draw_triangle moves to the bottom left corner with the pen up, then uses goto() to visit the other two corners and come back, filling the shape gold. The top corner is size * math.sqrt(3) / 2 above the bottom edge.

def draw_triangle(x, y, size):
    # A filled triangle with its bottom left corner at (x, y)
    height = size * math.sqrt(3) / 2
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    t.goto(x + size, y)
    t.goto(x + size / 2, y + height)
    t.goto(x, y)
    t.end_fill()

Three copies and a hole

At depth 0 the function draws a triangle. Otherwise it calls itself three times at half the size: bottom left, bottom right and top. The space in the middle is never drawn, and that is the hole.

def sierpinski(x, y, size, depth):
    # Base case: a plain triangle
    if depth == 0:
        draw_triangle(x, y, size)
        return

    # Recursive case: three half-size Sierpinski triangles,
    # bottom left, bottom right and top, with a hole in the middle
    half = size / 2
    height = half * math.sqrt(3) / 2
    sierpinski(x, y, half, depth - 1)
    sierpinski(x + half, y, half, depth - 1)
    sierpinski(x + half / 2, y + height, half, depth - 1)

Start with one big triangle

Depth 5 means five rounds of splitting, which makes 243 small triangles (3 multiplied by itself 5 times). A bottom left corner at (-220, -190) and a width of 440 centre it on the screen.

# One big triangle, 440 wide, centred on the screen
sierpinski(-220, -190, 440, 5)

Make it yours

Change the code above and press Run. Some ideas:

  • Change the 5 in sierpinski(-220, -190, 440, 5) to 3 for bigger triangles and fewer holes.
  • Change t.color("darkorange", "gold") to t.color("white", "deeppink") for pink triangles.
  • Use screen.bgcolor("white") and t.color("black", "black") for the classic black and white look.

What it uses

Every turtle command the site supports is listed on Python turtle online.