Python: 12 Iteration – Nested Loops

Nested loops are loops inside other loops.

Note that the outer loop (first_word) is run first and then the code block encounters the second inner loop (second_word).

The inner loop is run three times and then moves onto the code below the indented for loop.

Then the code block for the outer loop finished and it gets the second item in the outer loop and runs the code block again.

This repeats until the all of the items have been iterated (looped) through.

first_word = ["Play", "Master", "Super"]
second_word = ["Station", "System", "Console"]

for x in first_word:
  print("Start of", x)
  for y in second_word:
    print(x, y)
  print("End of", x)