Kodewick
Start learning →

Python Turtle Spiral

Each step a little longer, a 59 degree turn, six colors in turn. Change the angle and the same code draws something else entirely.

Python Turtle Spiral
The full code (14 lines)
import turtle

screen = turtle.Screen()
screen.bgcolor("black")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)

colors = ["red", "orange", "yellow", "lime", "deepskyblue", "violet"]
for i in range(240):
    t.pencolor(colors[i % 6])
    t.pensize(i / 80 + 1)
    t.forward(i * 1.2)
    t.left(59)

How it is drawn

Step i goes i * 1.2 forward, longer each time, then turns left 59 degrees.

The colors are in a list; colors[i % 6] gives steps 0, 6, 12… the same color, so the colors go round in turn.

59 is just under 60 (a hexagon's turn), so the picture is nearly a hexagon that slips a little each round — a swirl. Try 60, 90, 120, 144.

pensize(i / 80 + 1) makes the line thicker further out.

Questions

Why is 90 a square?

Four turns of 90 make a full turn, so it is a square spiral; 120 gives triangles, 144 a star.

What does % do?

It is the remainder: i % 6 is what is left after dividing i by 6, always 0 to 5 — one of the six colors.

Try another