Repeating Actions with Loops Jump to this section

Loops let you repeat the same steps without writing the same lines over and over. This is perfect for drawing polygons and repeating patterns.

The basic for loop Jump to this section

import turtle

t = turtle.Turtle()
for i in range(4):
    t.forward(100)
    t.left(90)

This draws a square by repeating the same forward/turn steps four times.

i is just a temporary variable used in for loops, while range(4) makes the code loop 4 times.

Repeating patterns Jump to this section

Combine loops and small angle changes to get interesting patterns.

import turtle

t = turtle.Turtle()

t.speed(10)

for i in range(36):
    for _ in range(4):
        t.forward(80)
        t.left(90)
    t.left(10)  # rotate slightly and draw again

This draws a rotating-square pattern by repeating the square drawing and rotating the turtle a bit each time.

Exercise Jump to this section

  • Change the inner loop to draw triangles instead of squares and change the turn angle to create new patterns.