Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In Python Turtle, a nested loop lets an inner loop draw one complete shape or motif each time an outer loop runs. For example, use an inner loop for the four sides of a square, then turn the turtle after that loop finishes to set up the next square. The key is to track both loop counts and the turtle’s position and heading.

What nested loops do in Turtle

A nested loop is a loop inside another loop. The outer loop controls how many times a larger action repeats; for every outer-loop pass, the inner loop completes all of its own iterations. In a Turtle drawing, the inner loop commonly repeats the moves and turns for one shape, while the outer loop changes the turtle’s heading, position, size, or color before the next shape.

Python’s turtle module turns movement and turning commands into visible drawings. The Python 3.11 Turtle documentation illustrates nested loops with an outer loop over step values and an inner loop that cycles through three colors while moving and turning the turtle.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Draw repeated squares with nested loops

This example draws six squares, turning the turtle 15 degrees after each square:

import turtle

for square in range(6):
    for side in range(4):
        turtle.forward(60)
        turtle.right(90)
    turtle.right(15)

turtle.done()

The inner loop runs four times, drawing one 60-unit side and turning 90 degrees on each pass. Those four passes make one square. After the inner loop ends, the outer loop’s turtle.right(15) changes the heading before the next square begins. The outer loop runs six times, so the inner loop’s four iterations occur six times: 24 side-drawing passes in total.

The turtle’s movement is stateful: its current heading affects the direction of the next forward move. A turn after the inner loop therefore rotates the turtle between completed squares; moving that turn inside the inner loop would instead change the direction after every side and alter the shape.

Build the pattern one loop at a time

Start with one square

First write a single loop that draws four sides. Ask what the turtle’s heading will be when the loop ends. Four right turns of 90 degrees total 360 degrees, so the turtle finishes facing its original direction, although it is at the square’s starting position only if the movement sequence closes the shape.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add an outer loop

Place the square-drawing loop inside a second loop. Put the turn that should happen between squares at the outer-loop indentation level, after the inner loop. Trace one outer pass to see exactly when the turn occurs.

Change one property at a time

Once the repeated square works, vary a single value—such as the outer-loop count, the angle between shapes, the side length, or a color. Changing one thing at a time makes it easier to connect the code to the visual result.

Choose turn angles for regular polygons

For a regular polygon with n sides, a common Turtle pattern repeats a forward move and a turn of 360 / n degrees. A square has four sides, so its turn is 90 degrees; an octagon has eight sides, so its turn is 45 degrees. The University of Texas at Austin’s Python instruction slides show repeated Turtle commands for squares and octagons.

Debug a pattern that looks wrong

  • Check indentation. A turn indented inside the inner loop happens after each side; a turn aligned with the inner loop happens after the whole shape.
  • Count both loops. Multiply the inner-loop iteration count by the outer-loop count to find how many times the inner actions run. For the example above, that is four times six.
  • Trace the turtle’s state. Record its position and heading before and after one outer pass. Turtle does not automatically reset either state between repetitions.
  • Watch the drawing window. A large side length or outer-loop count can carry the drawing beyond the visible area. Reduce one value and run the pattern again to isolate the cause.

If a result surprises you, follow one complete inner loop first, then account for the commands that run after it before the next outer pass. This separates shape errors from repetition or heading errors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Find a lesson or reference for more practice

These resources serve different purposes; none is established as more effective than the others:

Resource Format and purpose
Python 3.11 Turtle documentation Official module documentation with API details and examples, including a nested-loop pattern.
University of Oxford Turtle Project Guided sequence for programming with the Turtle system, including “Turtle Python 2 – Spirals and Shapes.”
University of Edinburgh: Loops — Python and Turtles A lesson focused on loops with Turtle.
University of Texas at Austin instructional slides Classroom slides with repeated Turtle commands for polygon examples.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.