Nested Loops
You can put a loop inside another loop. This is called a nested loop. For each single run of the outer loop, the inner loop runs all the way through.
for row in range(1, 3):
for column in range(1, 4):
if column > 1:
print(" ", end="")
print(f"{row}-{column}", end="")
print()
Output:
1-1 1-2 1-3
2-1 2-2 2-3
The outer loop picks a row, first 1 and then 2. For each row, the inner loop goes through all three columns.
Normally, print starts a new line after its value. Writing end="" tells it to end with nothing instead, so the next print continues on the same line. That is how the three columns stay on one line. After the inner loop, the empty print() ends the row, so the next row starts on a new line.
The if column > 1 check prints a space before every column except the first. This way, no row ends with an extra space.
Nested loops can do a lot of work. If the outer loop runs 1,000 times and the inner loop runs 1,000 times, the code inside the inner loop runs one million times (1,000 x 1,000).
In the exercise, complete the inner loop to print a multiplication square.