Editor

Koch Snowflake in Python Turtle

Intermediate · Fractals · 48 lines · draws in about 13 seconds

Each side of a triangle becomes a Koch line, and a Koch line is four smaller Koch lines with a bump in the middle, four levels deep.

Koch Snowflake in Python Turtle: the finished drawing
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("midnightblue")
screen.title("Koch snowflake")
t = turtle.Turtle()
t.speed(10)
t.pensize(2)
t.color("white", "lightskyblue")
def koch(length, depth):
# Base case: a straight line
if depth == 0:
t.forward(length)
return
# Recursive case: four smaller Koch lines, each a third as long,
# with a bump in the middle that points outwards
length = length / 3
koch(length, depth - 1)
t.left(60)
koch(length, depth - 1)
t.right(120)
koch(length, depth - 1)
t.left(60)
koch(length, depth - 1)
# Start at the top left corner of a triangle centred on the screen:
# its middle is a third of the way down from the top edge
side = 340
height = side * math.sqrt(3) / 2
t.penup()
t.goto(-side / 2, height / 3)
t.pendown()
# Three Koch lines, turning right after each one, make the snowflake
t.begin_fill()
for _ in range(3):
koch(side, 4)
t.right(120)
t.end_fill()
t.hideturtle()
turtle.done()

How the code works

A Koch line

At depth 0 a Koch line is a straight line. Otherwise it is four Koch lines a third as long, and the turns left(60), right(120), left(60) lift the middle into a pointed bump. Every smaller line does the same, so the bumps get bumps.

def koch(length, depth):
    # Base case: a straight line
    if depth == 0:
        t.forward(length)
        return

    # Recursive case: four smaller Koch lines, each a third as long,
    # with a bump in the middle that points outwards
    length = length / 3
    koch(length, depth - 1)
    t.left(60)
    koch(length, depth - 1)
    t.right(120)
    koch(length, depth - 1)
    t.left(60)
    koch(length, depth - 1)

Centre the triangle

The snowflake is built on a triangle with sides of 340. Its middle is a third of its height below the top edge, so starting at (-side / 2, height / 3) puts the finished snowflake in the centre.

# Start at the top left corner of a triangle centred on the screen:
# its middle is a third of the way down from the top edge
side = 340
height = side * math.sqrt(3) / 2
t.penup()
t.goto(-side / 2, height / 3)
t.pendown()

Three sides make a snowflake

Three Koch lines with right(120) between them close the shape. The turtle goes round clockwise, so every bump points outwards, and begin_fill() with end_fill() colours the inside light blue.

# Three Koch lines, turning right after each one, make the snowflake
t.begin_fill()
for _ in range(3):
    koch(side, 4)
    t.right(120)
t.end_fill()

Make it yours

Change the code above and press Run. Some ideas:

  • Change koch(side, 4) to koch(side, 1), then 2 and 3, to watch each level add smaller bumps.
  • Delete the t.begin_fill() and t.end_fill() lines to see the outline on its own.
  • Change t.color("white", "lightskyblue") to t.color("gold", "orange") for a golden star.

What it uses

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