October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Nested Loops with Python Turtle: Draw Repeating Shapes

Use an inner loop to draw each turtle shape and an outer loop to repeat it. Follow a square-pattern example and learn how to count iterations and place turns correctly.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A nested loop lets Python repeat the steps for one turtle shape, then repeat that whole shape-making routine. In a square pattern, the inner loop draws four sides; after it finishes, the outer loop turns the turtle before the next square.

What a nested loop does

A nested loop is a loop inside another loop. For each pass of the outer loop, Python runs the inner loop through all its iterations before returning to the outer loop. In turtle graphics, this is useful because one loop can make a shape from repeated movements, while another repeats that shape or motif.

As an Amazon Associate I earn from qualifying purchases.

The turtle draws by changing its state: movement follows its current heading, and turns change the direction of later movement. The placement and indentation of a turn therefore determine whether it happens after each side or after each completed shape. Python’s turtle documentation introduces these movement commands and includes a nested-loop pattern.

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

Draw a square pattern with two loops

This example draws six squares, turning 15 degrees after each one:

import turtle

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

turtle.done()

Read the loops from the inside out

  • range(4) makes the inner loop run four times. Each pass moves the turtle forward 60 units and turns it 90 degrees, drawing one square.
  • When those four passes finish, Python returns to the outer loop. Its body then turns the turtle 15 degrees before the next square begins.
  • range(6) makes the outer loop draw six squares in total. The inner loop therefore runs four times for each of six outer passes: 24 side-drawing passes altogether.

The inner loop’s 90-degree turns bring the turtle around a square. The additional 15-degree turn changes its heading between squares, producing a repeated design rather than tracing the same square in the same orientation. The turtle does not automatically reset its position or heading between outer-loop passes.

Choose the turn angle for a polygon

For a regular polygon with n sides, a standard turtle routine 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 instructional slides show repeated turtle commands for squares and octagons.

Keep the polygon’s turn inside the side loop, because the turtle must turn after each side. Put a pattern-changing turn outside that loop, after the shape is complete. Changing which loop contains a command changes how often Python runs it.

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

Build the pattern one change at a time

  1. Draw one square. Use a single four-iteration loop. Predict the number of sides and the turtle’s heading when it finishes.
  2. Add an outer loop. Put the square-drawing loop inside it, then add a turn after the inner loop to change the heading between squares.
  3. Vary one setting. Change the outer-loop count, the between-shape turn, the side length, or the color. Changing one value at a time makes its effect easier to see.
  4. Trace a pass. Write down each movement and turn for one outer-loop iteration, then count how many times that sequence repeats.

For more guided practice, the University of Oxford Turtle Project offers a progression of programming material that includes “Turtle Python 2 – Spirals and Shapes.” The University of Edinburgh’s turtle loops lesson focuses on loops, while the Python documentation is useful as an API reference and example source. These materials serve different purposes; their formats alone do not establish that one is more effective than another.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the loop when a drawing surprises you

  • Count both loops. Multiply the number of inner-loop passes by the number of outer-loop passes to find how many times the inner body runs in total.
  • Check indentation. A turn aligned with the inner-loop body happens after each side; a turn aligned with the outer-loop body happens after each completed shape.
  • Track position and heading. The turtle carries its position and direction forward. If a later shape starts somewhere unexpected, trace the movement and turns that came before it.
  • Watch the drawing area. A high outer-loop count or large movement can take parts of a pattern beyond the visible window.

When debugging, trace one outer pass by hand and account separately for all commands in the inner loop. This helps distinguish a counting mistake from a geometry or placement issue.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.