ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
Nested loops
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()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.Each row is one step: the line that just ran, and every name's value after it.
| Name | Value after line 1 |
|---|---|
| rowcounter | 0new |
| colcounter | not 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.
| Step | Line | rowcounter | colcounter | Printed |
|---|---|---|---|---|
| 1 | 1 | 0 | not made yet | |
| 2 | 2 | 0 | 0 | |
| 3 | 3 | 0 | 0 | 0 0 |
| 4 | 2 | 0 | 1 | |
| 5 | 3 | 0 | 1 | 0 1 |
| 6 | 2 | 0 | 2 | |
| 7 | 3 | 0 | 2 | 0 2 |
| 8 | 2 | 0 | 2 | |
| 9 | 1 | 1 | 2 | |
| 10 | 2 | 1 | 0 | |
| 11 | 3 | 1 | 0 | 1 0 |
| 12 | 2 | 1 | 1 | |
| 13 | 3 | 1 | 1 | 1 1 |
| 14 | 2 | 1 | 2 | |
| 15 | 3 | 1 | 2 | 1 2 |
| 16 | 2 | 1 | 2 | |
| 17 | 1 | 1 | 2 |
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)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.
Height: 4
*
**
***
****Your code
height = int(input("Height: "))
# One row per round of the outer loop; the inner loop prints that row's starsIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
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.
breakleaves 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.