Chapter 3.6 - Nested for loops

3.6 - Nested for loops


Try to predict what the code below will print. Then enter the code and see if you are correct.

# What does this print? Why? for i in range(3): print("a") for j in range(3): print("b")

This next block of code is almost identical to the one above. The second for loop has been indented one tab stop so that it is now nested inside of the first for loop. This changes how the code runs significantly. Try it and see.

# What does this print? Why? for i in range(3): print("a") for j in range(3): print("b") print("Done")


Now try to create a program that will output

Nested Loops Example
1
a
a
a
a
2
a
a
a
a
3
a
a
a

Here you can view a solution to the problem.