Hexagon Spiral in Python Turtle
Intermediate · Patterns and spirals · 32 lines · draws in about 16 seconds
Every hexagon starts at the same corner, a little bigger and turned 5 degrees further than the last, so 72 of them curl into a rainbow shell.

How the code works
A function for one hexagon
Six equal lines with a 60 degree turn after each make a hexagon, and the turtle ends where it started, facing the same way. side sets how big it is.
def draw_hexagon(side): for i in range(6): t.forward(side) t.left(60)
Start off-centre
Every hexagon grows up and to the right of the corner it starts from. Starting at (-65, -32) puts the middle of the finished shell in the middle of the screen.
# Start a little left and down, so the finished spiral sits in the middle t.penup() t.goto(-65, -32) t.pendown()
Bigger, turned and recoloured
hue goes from 0 to almost 1, and colorsys.hsv_to_rgb() turns it into a colour of the rainbow, from red through yellow, green and blue to purple. Each side is 7 + i * 1.9 steps, so every hexagon is bigger than the last, and left(5) turns the next one.
# 72 hexagons, each a little bigger and turned 5 degrees (72 x 5 = 360) for i in range(72): hue = i / 72 t.pencolor(colorsys.hsv_to_rgb(hue, 0.9, 1)) draw_hexagon(7 + i * 1.9) t.left(5)
Make it yours
Change the code above and press Run. Some ideas:
- Change the
0.9inhsv_to_rgb(hue, 0.9, 1)to0.4for soft pastel colours. - Inside
draw_hexagon, changerange(6)torange(4)andleft(60)toleft(90)to build the shell from squares. - Change
t.left(5)tot.left(15)and the hexagons spread into a lacy swirl that goes round three times.
What it uses
- functions with parameters
- nested loops
- colorsys for rainbow colours
Every turtle command the site supports is listed on Python turtle online.