A loop can contain one or more other loops: you can create a loop inside a loop.
This principle is known as nested loops. Nested loops go over two or more loops.
Programmers typically nest 2 or 3 levels deep. Anything higher than that is just confusing.
Related course: Complete Python Bootcamp: Go from zero to hero in Python 3
Lets do a simple example. We create two lists:
persons = [ "John", "Marissa", "Pete", "Dayton" ]
If we have a list of persons who like to eat at restaurants, can we make every one of them eat a certain restaurant?
This goes over both loops:
Try the exercises below
Given a tic-tac-toe board of 3x3, print every position
Create a program where every person meets the other
persons = [ “John”, “Marissa”, “Pete”, “Dayton” ]
If a normal for loop finishes in n steps O(n), how many steps has a nested loop?
After completing these continue with the next exercise.