Learning goals

  • Repeat commands with a for loop.
  • Choose how many times to repeat with range().
  • Draw a square, a triangle and a rectangle with the right turns.

Example: Draw a square with a loop

This program repeats two commands four times to draw a square.

import turtle as otto

for side in range(4):
    otto.forward(60)
    otto.left(90)

Animation of Turtle drawing a square with a for loop.

The indented commands repeat four times. Each time, the turtle draws one side and turns.

Summary

Code or idea What it does
for loop Repeats a group of commands.
range(4) Gives four numbers, from 0 to 3. The loop repeats four times.
Loop variable (side) Holds the current number. A variable is a name for a value.
Indentation Shows which commands belong to the loop.
Square Four equal sides. Turn 90 degrees after each side.
Triangle Three equal sides. Turn 120 degrees after each side.
Rectangle Long and short sides take turns. Turn 90 degrees after each side.