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.
Draw a square pattern with two loops
This example draws six squares, turning 15 degrees after each one:
#1 Best Overall
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.
Rank #2
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.
Recommended Free Tools
Build the pattern one change at a time
- Draw one square. Use a single four-iteration loop. Predict the number of sides and the turtle’s heading when it finishes.
- 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.
- 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.
- 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.
Rank #3
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.
Quick Recap
Best Value
Rank #4
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.




