Table of Contents » Chapter 3 : Processing : Repetition : Nested Loops
Nested Loops
Contents
Overview
Now that we have learned a bit about while and for loops, now we can consider nested loops. A nested loop structure is constructed by placing one loop inside another loop. This is often called repeating a repetition. Nested loops can be while loops or for loops, or a combination, that is, a for loop nested inside a while loop or visa versa.
The general form of the code of the nested for loop is as follows:
pre_loop_statement(s)
for outer_loop_variable in sequence:
for inner_loop_variable in sequence:
inner_loop_statement_1
inner_loop_statement_1
inner_loop_statement_1
.
inner_loop_statement_n
outer_loop_statement_1
outer_loop_statement_2
.
outer_loop_statement_n
post_loop_statement(s)
General Form Details:
- Nested loops have an outer loop and an inner loop.
- Notice the indentation in this general form. The inner for loop is indented inside the outer for loop and each of the two loops has its indented statements as well.
- Each time the outer loop runs (iterates) the inner loop runs the number of times specified in the inner loop signature.
- See the functional code example below.

The outer loop of the functional code example above iterates five times (0 thru 4) and each time the outer loop runs the inner loop iterates ten times (0 thru 9).


Code Details:
- As specified in the outer loop signature, the outer loop repeats five times, with x set to 0 initially, then 1, then 2, then 3, and then 4.
- Each time the outer loop runs, it prints the value of x (0) and a space.
- Then the inner loop runs, as specified in the inner loop signature, it repeats 10 times with y set to 0 initially, then 1, then 2, ... thru to 9.
- After the inner loop runs, the execution leaves the inner loop and returns to the next statement of the outer loop which is the print() statement. That empty print statement moves the (invisible) cursor to the next line (like pressing the Enter key on the keyboard).
- All of those steps create one row of output, the first time it looks like 0 0 1 2 3 4 5 6 7 8 9,
- The first 0 is the 0 from the outer loop, then the 0 thru 9 are the values of y as the inner loop runs.
- Next, execution returns to the outer loop signature.
- The second time the outer loop runs, x is set to 1 and prints the value of x (1) and a space.
- Then the inner loop runs again, just as it did the first time, it repeats 10 times with y set to 0 initially, then 1, then 2, ... thru to 9.
- Again, after the inner loop runs, the execution leaves the inner loop and returns to the next statement of the outer loop which is the print() statement. That empty print statement moves the (invisible) cursor to the next line (like pressing the Enter key on the keyboard).
- These steps create the second row of output, the first time it looks like 1 0 1 2 3 4 5 6 7 8 9,
- The first digit (1) is the 1 from the outer loop, then the 0 thru 9 are the values of y as the inner loop runs.
- Next, execution returns to the outer loop signature.
- These steps repeat again for x = 2, x = 3 and x = 4.
There are many uses of nested loops. One very common use is working with rows and columns of data, which we will explore when we study data structures a little later in the book.
Examples 1 & 2 are intended to be studied and compared.
Example 1
for i in range(5):
for j in range(5):
print("*", end="")
print()
Example 1 Output
*****
*****
*****
*****
*****
Example 2
for i in range(5):
for j in range(i + 1):
print("*", end="")
print()
Example 2 Output
*
**
***
****
*****
Question
Compare the loop signatures between Examples 1 & 2 above and answer the following questions:
- Note that in Example 2, we changed the range parameter to i + 1. Why did making this change alter the output to appear as a triangle of * characters, instead of the rectangle of * characters we see in Example 1's output?
- What difference would there have been to the output of Example 2 if we had not included + 1 in the Example 2 range parameter?
Examples 3 & 4 add decision statements to demonstrate how to combine decisions with loops to achieve intended results.
Example 3
rows = 5
cols = 5
for i in range(rows):
for j in range(cols):
if j < i:
print(" ", end=" ")
else:
print("*", end=" ")
print()
Example 3 Output
* * * * *
* * * *
* * *
* *
*
Example 4
rows = 5
cols = 10
for i in range(1, rows + 1):
for j in range(1, cols + 1):
if i == 1 or i == rows or j == 1 or j == cols:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Example 4 Output
* * * * * * * * * *
* *
* *
* *
* * * * * * * * * *
Question
Questions: Answer the following questions about Examples 3 ad 4:
- Example 3: What is the purpose of the if j < i if statement in the Example 3 code?
- Example 3: What would be the effect of changing Line 5 to if i < j? And why?
- Example 4: What is the purpose of the compound if decision on Line 5?
- Example 4: How could you change this code to print the box with double the spaces between the * characters and also a blank line between each row in the rectangle shape? Like this (compare this to the Example 4 Output above):
* * * * * * * * * *
* *
* *
* *
* * * * * * * * * *
When you are learning programming structures, especially those that repeat steps, it is often very useful to manually trace the execution of that structure. For loops, this is called loop tracing. When we trace code execution it is important to, first, understand how the structure works and, second, to write out each step the way the interpreter will execute each step in the computer.
For example, consider the following nested for loop:
for i in range(4):
print("Row", i + 1, sep=" ", end=": ")
for j in range(3):
print(j + 1, end=" ")
print()
Output:
Row 1: 1 2 3
Row 2: 1 2 3
Row 3: 1 2 3
Row 4: 1 2 3
Execution Trace Table
i Iteration | print("Row", i+1) | j iterations | print() | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
0 | Row 1: |
|
|||||||||
1 | Row 2: |
|
|||||||||
2 | Row 3: |
|
|||||||||
3 | Row 4: |
|
Problem 1
Write a Python program that prints a triangle of digits using nested for loops. Prompt the user several rows, then for that number of rows print the row number repeated each row's number of times. Here is an example of the prompt and output:
Please enter the number of rows: 5
1
22
333
4444
55555
Problem 2
Write a Python program that prompts the user for an integer value and then uses nested for loops to create a multiplication (times) table based on that number. For example, if the user enters 5, your program should produce the times table displayed below. Notice the spacing between numbers on each row and the blank lines between rows, your program should produce that spacing as well. Hint: Take a look at the Escape Sequences page for help with the spacing of the rows and columns in your output.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25