Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

Nested loops

3 min read

A loop's body can hold any code, including another loop. That's how you work with anything that has rows and columns: a seating plan, a times table, a grid of pixels. By the end of this lesson you'll be able to follow a loop inside a loop step by step, and you'll see why they can get slow.

Rows and columns

Remember end from lesson 0.3? With end="", print doesn't start a new line after what it prints. Predict what this draws.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for row in range(3):
    for col in range(4):
        print("*", end="")
    print()
Pick the output

Python prints

****
****
****

The outer loop runs 3 times, once per row. Each time, the inner loop prints 4 stars on the same line, then print() ends the line. 3 rows of 4.

The outer loop makes rows; the inner loop fills each row. The empty print() belongs to the outer loop, not the inner one: it's indented under for row, so it runs once per row, after the inner loop has finished. Indentation decides which loop a line belongs to, and Python is strict about it:

The inner loop starts again

The key idea: for every round of the outer loop, the inner loop runs completely, from its start. Step through it and watch col go back to 0 when row changes.

Two counters

Press Next to run one line at a time and watch each name change.

for row in range(2):
for col in range(3):
print(row, col)
Names and their values after line 1
NameValue after line 1
rowcounter0new
colnot made yet

Output so far

Step 1 of 17

Step 1 of 17. Line 1: for row in range(2):. row is now 0, the counter.

row changes slowly, col quickly, like the hour and minute hands of a clock. Every pair of values appears exactly once.

ጥያቄ

The outer loop is for row in range(4): and the inner loop is for col in range(5):. How many times does a print inside the inner loop run?

A times table

Use the row and column numbers themselves. With :>3 from lesson 1.4, every number takes three characters, so the columns line up:

for row in range(1, 10):
    for col in range(1, 10):
        print(f"{row * col:>3}", end="")
    print()

In the editor, press Escape then Tab to move on.

Try :>2 to see why the width matters, then put it back.

break leaves one loop

break leaves only the loop it's directly inside. Predict:

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for row in range(3):
    for col in range(3):
        if col == 1:
            break
        print(row, col)
Pick the output

Python prints

0 0
1 0
2 0

When col is 1, break ends the inner loop. The outer loop carries on to its next round, and the inner loop starts again from 0. So each row prints only its first pair.

If you expected only 0 0, you pictured break leaving both loops. It doesn't: it ends the inner loop, and the outer one goes on.

Why nested loops get slow

The rounds multiply. A 10 × 10 grid is 100 rounds, which is nothing. But comparing each of 1,000 students with every student is 1,000 × 1,000 = a million rounds, and 10,000 students is a hundred million. When a program gets slow, a loop inside a loop is the first place to look.

መልመጃ

A triangle of stars

Ask for a height and print a right triangle that many rows tall: 1 star on the first row, 2 on the second, and so on. The inner loop's range() can use the outer loop's number.

Output
Height: 4
*
**
***
****

Your code

height = int(input("Height: "))
# One row per round of the outer loop; the inner loop prints that row's stars

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

height = int(input("Height: "))
for row in range(1, height + 1):
    for star in range(row):
        print("*", end="")
    print()

You could also write the inner loop as print("*" * row), using * on text from lesson 1.3. Both are fine; the nested version shows the idea.

ዋና ዋና ነጥቦች

  • A loop can contain another loop. For every round of the outer loop, the inner loop runs completely, from its start.
  • Indentation decides which loop a line belongs to.
  • The rounds multiply: 4 outer × 5 inner = 20.
  • break leaves only the loop it's directly inside.
  • Nested loops over large amounts of data get slow fast: 1,000 × 1,000 is a million rounds.