Code

Loops Inside Loops

You can put a loop inside another loop. For every single round of the outer loop, the inner loop runs all the way through.

for row in range(1, 3):
    for col in range(1, 4):
        print(row, col)

This prints 2 × 3 = 6 lines: 1 1, 1 2, 1 3, 2 1, 2 2, 2 3.

Staying on one line

Normally print jumps to a new line after it prints. Pass end=" " to print a space instead, and call an empty print() when you do want a new line:

for n in range(3):
    print(n, end=" ")
print()   # now move to the next line
# 0 1 2

Assignment

Print a 4 × 4 times table for the Guild's apprentices:

1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
  1. The outer loop already goes through row = 1 to 4.
  2. Add an inner loop over col = 1 to size that prints row * col followed by a space (end=" ").
  3. Keep the print() at the end of each row.