7 min read
Nested loops and patterns
A loop inside a loop
Once you are comfortable with a single loop, the next step is to put one loop inside another. A loop can live inside another loop. The outer loop counts the rows; the inner loop counts the columns. On each row, the inner loop runs all the way through before the outer loop moves on. This is exactly what you need for a grid, a table, or a pattern of stars.
The key thing to picture is the timing. The outer loop takes one step, then pauses while the inner loop runs from start to finish, then the outer loop takes its next step and the inner loop runs all the way through again. If the inner loop runs five times, and the outer loop runs five times, the inner block runs twenty five times in total.
A multiplication table
Say you want a multiplication table for the numbers 1 through 5. Writing one loop per row would be five loops stacked up, and a table to 20 would be twenty. Nesting collapses all of that:
for row in range(1, 6):
for col in range(1, 6):
print(row * col, end="\t")
print()
The outer loop picks a row. For that row, the inner loop runs across every col, printing row * col with a tab (\t) between them. The bare print() after the inner loop ends the line so the next row starts fresh. The output is a tidy 5 by 5 grid, and going up to 20 by 20 means changing two numbers, not writing fifteen more lines.
Drawing patterns
The same idea draws shapes out of characters. Here two nested loops print a solid block of stars, three rows tall and three columns wide:
for row in range(3):
for star in range(3):
print("*", end="")
print()
The inner loop prints three stars on one line, with no line break because of end="". The bare print() then ends that line, and the outer loop starts the next row. Change the inner range and you change the width; change the outer range and you change the height. A pattern is really just a grid where you print a character instead of a number.
Lab
Try turning the block into a triangle. Write a nested loop where the outer loop runs 5 times and the inner loop prints one more star each row, so row 1 has one star and row 5 has five. Hint: the inner loop can be for star in range(row): and you print a star with no line break, then a plain print() to end the row.
Check yourself
- In a nested loop that prints a multiplication table, which loop chooses the row and which chooses the column, and how many times does the inner loop run for each single run of the outer loop?
- A table from 1 to 5 needs a 5 by 5 grid, and without nesting you would stack five separate loops. With a nested loop, what do you change to grow the table to 20 by 20 instead of writing more loops?
- In the star-block example, what job does the bare
print()after the inner loop do, and what would the output look like if you removed it?
Where this lesson comes from
Built from
- Programming in a Data World: Lecture 3 deck (nested loops) and Lecture Notebook
alphaPlan courses are built from taught programmes rather than invented for the web. Where a claim rests on an outside standard or a reported case, it is named above so you can check it rather than take our word for it.
This course was developed by alphaPlan Center from programs delivered in partnership with the American Corners network.
Found something unclear, outdated or improvable? Suggest an improvement