Python - 17

Python - 17: Nested Loops

A nested loop is a loop inside another loop.

Your challenge: Use nested loops to build a small grid, then use another pair of loops to create multiplication results.

✦ Python Editor
▶ Output

Outer and inner loops

The outer loop runs first. Each time it repeats, the entire inner loop runs from beginning to end.

In the grid example, the outer loop creates the rows and the inner loop prints the columns.

Keep output on the same line

Normally print() moves to a new line. Using end=" " keeps each symbol on the same line.

The empty print() then starts the next row.

Multiply inside a nested loop

You can combine values from both loops: number * multiplier

Try it: Change the grid to 4 rows and 4 columns.

Good Python habit: Keep nested loops clearly indented so it is easy to see which loop each line belongs to.

👀 Show Example Solution
for row in range(3):

    for column in range(5):
        print("*", end=" ")

    print()

for number in range(1, 4):

    for multiplier in range(1, 4):
        print(number * multiplier)

    print("---")

The inner loop completes all of its repetitions every time the outer loop runs once.