PythonPython · Chapterبەش 7 · Lectureوانە 7.4
Nested Loops and Patternsخولە ناوەکییەکان و کێشانی شێوە
A loop inside a loop: how the outer and inner loops work together, the correct shape and indentation, star and number patterns drawn row by row, multiplication tables with aligned columns, the Lab 1 pattern and table tasks, and harder challenges: prime numbers and more complex patterns.
لەناو : چۆن و ناوەوە پێکەوە کار دەکەن، شێوە و دروستەکەیان، شێوەی ئەستێرە و ژمارە کە بە دەکێشرێن، خشتەی لێکدان کە ڕێکن، ئەرکەکانی شێوە و خشتەی تاقیگەی 1 و چەند ڕاهێنانێکی قورستر: بۆ دۆزینەوەی ژمارە سەرەتاییەکان و شێوەی ئاڵۆزتر.
What you will learnچی فێر دەبیت
- Explain how the inner loop restarts for every round of the outer loop, and count how many times the inner body runs.
- Write correct nested loops in Python: colons, indentation and a separate variable for each loop.
- Print star, number and symbol patterns row by row with
end=andprint(). - Print a multiplication table whose columns line up, using f-string widths.
- ڕوونی بکەیتەوە چۆن بۆ هەر جارێکی لە سەرەتاوە دەست پێدەکاتەوە و بژمێریت جەستەی ناوەوە چەند جار جێبەجێ دەبێت.
- بە شێوەیەکی دروست لە Pythonـدا بنووسیت: دوو خاڵ، و جیا بۆ هەر .
- شێوەی ئەستێرە، ژمارە و هێما بە بە
end=وprint()چاپ بکەیت. - خشتەیەکی لێکدان چاپ بکەیت کە ڕێک بن، بە بەکارهێنانی پانیی f-string.
A loop inside a loopخولێک لەناو خولێکدا
You already know two loops: for and while. The body of a loop can hold any — and that includes another loop. A loop written inside another loop is a .
تا ئێستا دوو جۆر دەناسیت: for و while. دەتوانێت هەر لەخۆ بگرێت — تریش. ئەو لەناو تردا دەنووسرێت پێی دەگوترێت .
A loop written inside another loop. For each pass of the outer loop, the inner loop runs completely; this is how you go through and tables.
بریتییە لە کە لەناو خولێکی تردا نووسراوە. بۆ هەر خولانەوەیەکی خولی دەرەوە، خولی ناوەوە بە تەواوی جێبەجێ دەبێت؛ بەم شێوەیە و خشتەکان دەخوێنرێنەوە.
A nested loop has an outer loop and one or more inner loops. Here is the rule that explains every program in this lesson: each time the outer loop goes round once, the inner loop is entered again and starts from the beginning. It runs all of its rounds, and only then does the outer loop take its next step.
و یەک یان چەند هەیە. ئەمە ئەو یاسایەیە کە هەموو ئەم وانەیە ڕوون دەکاتەوە: هەر جارێک یەک جار دەخولێتەوە، دووبارە دەست پێدەکاتەوە و لە سەرەتاوە دەڕوات. هەموو جارەکانی خۆی تەواو دەکات و تەنها ئەوسا دەچێتە هەنگاوی دواتر.
Think of a waiter in a tea house. He goes from table to table — that is the outer loop. At each table he pours tea for every guest, one after another — that is the inner loop. Only when every guest at this table has a glass does he move to the next table, and there he starts again with the first guest.
چایچییەک لە چایخانەیەکدا بهێنە بەرچاوت. لە مێزێکەوە بۆ مێزێکی تر دەچێت — ئەمە . لەسەر هەر مێزێک بۆ هەموو میوانەکان یەک لە دوای یەک چا تێدەکات — ئەمە . تەنها کاتێک هەموو میوانەکانی ئەم مێزە ئیستیکانی چایان هەبوو، دەچێتە سەر مێزی دواتر و لەوێ دیسان لە یەکەم میوانەوە دەست پێدەکاتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A real waiter may leave a table half-served and come back later. A program never does that: the inner loop always finishes all its rounds before the outer loop moves on (unless you use break).
چایچیی ڕاستەقینە دەکرێت مێزێک بە نیوەچڵی جێبهێڵێت و دواتر بگەڕێتەوە. هەرگیز وا ناکات: هەمیشە هەموو جارەکانی تەواو دەکات پێش ئەوەی بڕواتە پێشەوە (مەگەر break بەکار بهێنیت).
break stops a loop early, and a for loop's else block runs only when the loop ends without break; then how nested loops run.
break زوو دەوەستێنێت و تەنها کاتێک جێبەجێ دەبێت کە بەبێ break کۆتایی بێت؛ پاشان چۆن کار دەکەن.
Plays 6:30-9:16, "Nested loops": for every round of the outer loop the inner loop runs completely.
لە 6:30 تا 9:16 پەخش دەبێت، بەشی Nested loops: بۆ هەر دەرەوە، ناوەوە بە تەواوی جێبەجێ دەبێت.
The shape of a nested loopشێوەی خولێکی ناوەکی
In Python the shape of a nested loop is shown by alone. The outer loop's header ends with a colon. Everything indented 4 spaces under it is the outer body — including the inner loop. The inner loop's own body is indented 8 spaces. A line that comes back to 4 spaces is again part of the outer body: it runs once per outer round, after the inner loop has finished.
لە Pythonـدا شێوەی تەنها بە دیار دەکرێت. سەردێڕی بە دوو خاڵ (:) کۆتایی دێت. هەر شتێک 4 لە ژێریدا بچێتە ناوەوە جەستەی دەرەوەیە — . جەستەی خۆی 8 دەچێتە ناوەوە. دێڕێک کە بگەڕێتەوە سەر 4 ، دیسان بەشێکە لە جەستەی دەرەوە: بۆ هەر جارێکی دەرەوە یەک جار جێبەجێ دەبێت، دوای ئەوەی تەواو بوو.
for row in range(1, 4): # outer loop: 3 rounds
for col in range(1, 5): # inner loop: 4 rounds each time
print(row, col, end=" ") # body of the inner loop
print() # rest of the outer body: end the line1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The outer loop:
rowtakes the values 1, 2, 3.:
rowنرخەکانی 1، 2، 3 وەردەگرێت.- Lineدێڕی 2
The inner loop starts again in every outer round:
col= 1, 2, 3, 4.لە هەر جارێکی دەرەوەدا لە سەرەتاوە دەست پێدەکاتەوە:
col= 1، 2، 3، 4.- Lineدێڕی 3
The inner body (8 spaces) runs times.
end=" "keeps the pairs on one line.جەستەی ناوەوە (8 ) جار جێبەجێ دەبێت.
end=" "جووتەکان لەسەر یەک دێڕ دەهێڵێتەوە.- Lineدێڕی 4
Back to 4 spaces: this line belongs to the outer body. It runs 3 times and ends each line.
گەڕانەوە بۆ 4 : ئەم دێڕە سەر بە جەستەی دەرەوەیە. 3 جار جێبەجێ دەبێت و هەر دێڕێک تەواو دەکات.
The shape of a nested loopشێوەی خولێکی ناوەکی
The slide's templateقاڵبەکەی سلایدەکە
The shape of a nested loopشێوەی خولێکی ناوەکی
Give each loop its own variableهەر خولێک گۆڕاوێکی خۆی هەبێت
The slide's template used the name element for both loops. Python allows this, but the inner loop then overwrites the outer loop's . Watch:
قاڵبەکەی سلایدەکە ناوی elementـی بۆ هەردوو بەکار هێنا. Python ڕێگەی پێدەدات، بەڵام ئەوسا دەرەوە دەسڕێتەوە و نرخی خۆی تێدەخات. سەیر بکە:
for i in range(3):
for i in range(2):
print(i, end=" ")
print("| after the inner loop, i is", i)0 1 | after the inner loop, i is 1 0 1 | after the inner loop, i is 1 0 1 | after the inner loop, i is 1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Both loops use
i.هەردوو
iبەکار دەهێنن.- Lineدێڕی 4
After the inner loop,
iholds the inner value 1 — the outer value (0, 1, 2) is lost.دوای ،
iنرخی ناوەوەی 1 هەڵدەگرێت — نرخی دەرەوە (0، 1، 2) ون بووە.
The outer loop still runs 3 times, because a for loop takes its next value from its own range, not from the variable. But any line after the inner loop sees the wrong value. Give every loop its own name: i and j, or better, names that say what they count, such as row and col.
هێشتا 3 جار دەخولێتەوە، چونکە for نرخی دواتری لە rangeـی خۆی وەردەگرێت، نەک لە . بەڵام هەر دێڕێک دوای نرخی هەڵە دەبینێت. ناوێکی جیاواز بدە بە هەر : i و j، یان باشتر ناوێک کە بڵێت چی دەژمێرێت، وەک row و col.
Drawing a triangle of starsکێشانی سێگۆشەیەک لە ئەستێرە
The slides' first example prints a right-angled triangle. Look at who controls what. The outer loop's i says which row we are on: 1 to 5. The inner loop runs range(i), so it goes round i times — row i gets i stars.
یەکەم نموونەی سلایدەکان سێگۆشەیەکی گۆشەوەستاو چاپ دەکات. سەیر بکە کێ چی کۆنترۆڵ دەکات. iـی دەڵێت لەسەر کام : 1 تا 5. range(i) بەکار دەهێنێت، بۆیە i جار دەخولێتەوە — i ژمارە i ئەستێرە وەردەگرێت.
# outer loop
for i in range(1, 6):
# inner loop
for j in range(i):
print("*", end=" ")
print()* * * * * * * * * * * * * * *
Every line also ends with a space, because each star is followed by one.
هەموو دێڕێک بە کۆتایی دێت، چونکە دوای هەر ئەستێرەیەک دێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
range(1, 6)gives 1, 2, 3, 4, 5 — the stop value 6 is not included.range(1, 6)ژمارەکانی 1، 2، 3، 4، 5 دەدات — نرخی وەستانی 6 نایەتە ناوەوە.- Lineدێڕی 4
range(i)gives : exactlyirounds.range(i)ژمارەکانی دەدات: ڕێکiجار.- Lineدێڕی 5
end=" "prints a space instead of a new line, so the stars stay on one line.end=" "لەبری دێڕی نوێ چاپ دەکات، بۆیە ئەستێرەکان لەسەر یەک دێڕ دەمێننەوە.- Lineدێڕی 6
print()with nothing inside only ends the line. It is in the outer body, so it runs once per row.print()بەبێ هیچ شتێک لەناویدا تەنها دێڕەکە تەواو دەکات. لە جەستەی دەرەوەدایە، بۆیە بۆ هەر یەک جار جێبەجێ دەبێت.


Star patterns step by step: the outer loop counts rows, the inner loop prints one row's stars; a square, then a growing triangle.
شێوەی ئەستێرە هەنگاو بە هەنگاو: دەرەوە دەژمێرێت، ناوەوە ئەستێرەکانی یەک پیشان دەدات؛ چوارگۆشەیەک، پاشان سێگۆشەیەک کە بەرەو خوارەوە گەورە دەبێت.
Plays 3:13-8:51: the square and the increasing triangle; stops before the upside-down triangle so the exercise is not spoiled.
لە 3:13 تا 8:51 پەخش دەبێت: چوارگۆشەکە و سێگۆشە گەورەبووەکە؛ پێش سێگۆشە پێچەوانەکە ڕادەوەستێت تا وەڵامی ڕاهێنانەکە ئاشکرا نەبێت.
Drawing a triangle of starsکێشانی سێگۆشەیەک لە ئەستێرە
Tracing the triangleبەدواداچوونی سێگۆشەکە
A shows each round of the outer loop on one row. Notice that j is never printed here: it only counts. The number of stars comes from i.
هەر جارێکی لەسەر پیشان دەدات. سەرنج بدە کە j لێرەدا هەرگیز چاپ ناکرێت: تەنها دەژمێرێت. ژمارەی ئەستێرەکان لە iـەوە دێت.
i (outer)i (دەرەوە) | j takesj وەردەگرێت | Stars printedئەستێرەی چاپکراو | Thenپاشان |
|---|---|---|---|
| 1 | 0 | * | print() → new lineprint() ← دێڕی نوێ |
| 2 | 0, 1 | * * | new lineدێڕی نوێ |
| 3 | 0, 1, 2 | * * * | new lineدێڕی نوێ |
| 4 | 0, 1, 2, 3 | * * * * | new lineدێڕی نوێ |
| 5 | 0, 1, 2, 3, 4 | * * * * * | new line; range(1, 6) has no more valuesدێڕی نوێ؛ range(1, 6) نرخی تری نەماوە |
Exercise: an upside-down triangleڕاهێنان: سێگۆشەیەکی سەرەوخوار
The slide gives a second program with an empty output box. Work it out on paper before you open the answer: first list the values of i, then count the rounds of the inner loop.
سلایدەکە دووەم دەدات و شوێنی بەتاڵە. پێش ئەوەی وەڵامەکە بکەیتەوە لەسەر کاغەز کاری لەسەر بکە: سەرەتا نرخەکانی i بنووسە، پاشان جارەکانی بژمێرە.
Write down exactly what this program prints.
ڕێک ئەوە بنووسە کە ئەم چاپی دەکات.
# outer loop
for i in range(6, 1, -1):
# inner loop
for j in range(i - 1):
print("O", end=" ")
print()Hintئاماژە
range(6, 1, -1) counts down and stops before 1.
range(6, 1, -1) بەرەو خوار دەژمێرێت و پێش 1 دەوەستێت.
Show answerوەڵامەکە پیشان بدە
O O O O O O O O O O O O O O O
range(6, 1, -1)givesi= 6, 5, 4, 3, 2 (the stop value 1 is not included).- The inner loop runs
range(i - 1): 5, 4, 3, 2 and 1 rounds. - So there are 5 rows, from 5 letters O down to 1.
range(6, 1, -1)نرخەکانیi= 6، 5، 4، 3، 2 دەدات (نرخی وەستانی 1 نایەتە ناوەوە).-
range(i - 1)بەکار دەهێنێت: 5، 4، 3، 2 و 1 جار. - کەواتە 5 هەیە، لە 5 پیتی O بۆ 1.
A multiplication tableخشتەی لێکدان
The slides' next example prints a small multiplication table. This time the inner loop's variable is used as well: every cell is i * j. The outer i chooses the row and the inner j walks across the columns.
نموونەی دواتری سلایدەکان خشتەیەکی بچووکی لێکدان چاپ دەکات. ئەم جارە بەکار دێت: هەر خانەیەک i * jـە. iـی دەرەوە هەڵدەبژێرێت و jـی ناوەوە بەناو دەڕوات.
# outer loop: one row for each i
for i in range(1, 4):
# inner loop: one column for each j
for j in range(1, 6):
print(i * j, end=" ")
print() # with ( ): ends the row1 2 3 4 5 2 4 6 8 10 3 6 9 12 15
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
One row for each
i: 1, 2, 3.بۆ هەر
iـێک : 1، 2، 3.- Lineدێڕی 4-5
One column for each
j: 1 … 5. Two spaces follow each product.بۆ هەر
jـێک : 1 … 5. دوای هەر ئەنجامێک دوو دێت.- Lineدێڕی 6
print()— with parentheses — ends the row.print()— لەگەڵ کەوانەکان — تەواو دەکات.
i picks the row, j the column; the cell is i * j.i هەڵدەبژێرێت و j ؛ خانەکە i * jـە.CêITA multiplication tableخشتەی لێکدان
A bug on the slide: print without ( )باگێک لەسەر سلایدەکە: print بەبێ ( )
On the slide the last line is print with no parentheses. The slide shows three neat rows as the output, but if you run the code exactly as written, everything comes out on one line:
لەسەر سلایدەکە دوایین دێڕ printـە بەبێ کەوانە. سلایدەکە سێ ڕێک وەک پیشان دەدات، بەڵام ئەگەر کۆدەکە ڕێک وەک نووسراوە جێبەجێ بکەیت، هەموو شتێک لەسەر یەک دێڕ دەردەچێت:
# outer loop
for i in range(1, 4):
# nested loop
# to iterate from 1 to 5
for j in range(1, 6):
# print multiplication
print(i * j, end=' ')
print1 2 3 4 5 2 4 6 8 10 3 6 9 12 15
Mixing while and forتێکەڵکردنی while و for
The outer and the inner loop do not have to be the same kind. In this exercise from class, a while loop counts i down from 5 to 1, and a for loop inside it prints numbers that double each time. You can check answers like this in any online Python editor, even on a phone.
پێویست ناکات و ناوەوە لە هەمان جۆر بن. لەم ڕاهێنانەی پۆلدا، i لە 5ـەوە بەرەو 1 دەژمێرێت و لەناویدا ژمارەگەلێک چاپ دەکات کە هەر جارێک دوو هێندە دەبن. دەتوانیت وەڵامی ئەم جۆرە پرسیارانە لە هەر دەستکاریکەرێکی ئۆنلاینی Pythonـدا بپشکنیت، تەنانەت لەسەر مۆبایل.
Work out the output of this program line by line.
ئەم دێڕ بە دێڕ دەربهێنە.
i = 5
while i >= 1:
num = 1
for j in range(1, i + 1):
print(num, end="xxx")
num *= 2
print()
i -= 1Hintئاماژە
end = "xxx" prints xxx instead of a new line. num *= 2 doubles num.
end = "xxx" لەبری دێڕی نوێ xxx چاپ دەکات. num *= 2 num دوو هێندە دەکات.
Show answerوەڵامەکە پیشان بدە
1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx
istarts at 5. Thewhileloop runs whilei >= 1, andi -= 1lowers it at the end of each round: 5, 4, 3, 2, 1.num = 1is inside the outer loop, so every row starts again at 1.- The inner
forrunsrange(1, i + 1)—irounds. Each round printsnumfollowed by xxx, then doublesnum. print()ends the row. Whenireaches 0 the conditioni >= 1is false and the program stops.
iلە 5ـەوە دەست پێدەکات. تاi >= 1بێت کار دەکات وi -= 1لە کۆتایی هەر جارێکدا کەمی دەکاتەوە: 5، 4، 3، 2، 1.num = 1لەناو ، بۆیە هەموو دیسان بە 1 دەست پێدەکات.forـی ناوەوەrange(1, i + 1)بەکار دەهێنێت —iجار. هەر جارێکnumو دوای ئەو xxx چاپ دەکات، پاشانnumدوو هێندە دەکات.print()تەواو دەکات. کاتێکiدەگاتە 0،i >= 1هەڵە دەبێت و دەوەستێت.
jj | Printedچاپکراو | num after num *= 2num دوای num *= 2 |
|---|---|---|
| 1 | 1xxx | 2 |
| 2 | 2xxx | 4 |
| 3 | 4xxx | 8 |
| 4 | 8xxx | 16 |
| 5 | 16xxx | 32 |
Lab: two patternsتاقیگە: دوو شێوە
Write a Python program that prints Pattern 1 and then Pattern 2, exactly as shown.
Python بنووسە کە شێوەی 1 و پاشان شێوەی 2 ڕێک وەک خوارەوە چاپ بکات.
5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
<> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <>
Hintئاماژە
Pattern 1: row r counts down from r to 1. Pattern 2: one nested loop for the growing half and one for the shrinking half.
شێوەی 1: r لە rـەوە بەرەو 1 دەژمێرێت. شێوەی 2: بۆ نیوە گەورەبووەکە و یەکێکی تر بۆ نیوە بچووکبووەکە.
Show answerوەڵامەکە پیشان بدە
# Pattern 1
for r in range(5, 0, -1):
for c in range(r):
print(r - c, end=" ")
print()
print() # an empty line between the two patterns
# Pattern 2: rows grow from 1 to 5 ...
for r in range(1, 6):
for c in range(r):
print("<>", end=" ")
print()
# ... then shrink from 4 back to 1
for r in range(4, 0, -1):
for c in range(r):
print("<>", end=" ")
print()5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <>
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-5
Pattern 1:
r= 5 … 1. In rowr,ctakes the values , sor - cprints .شێوەی 1:
r= 5 … 1. لەrـداcنرخەکانی وەردەگرێت، بۆیەr - cژمارەکانی چاپ دەکات.- Lineدێڕی 10-13
Rows of 1 … 5 pairs
<>.1 … 5 جووت
<>.- Lineدێڕی 15-18
Then 4 … 1 pairs, so the longest row is not printed twice.
پاشان 4 … 1 جووت، بۆ ئەوەی درێژترین دوو جار چاپ نەکرێت.
Lab: two patternsتاقیگە: دوو شێوە
Where did the empty line come from?دێڕە بەتاڵەکە لە کوێوە هات؟
A student's solution printed Pattern 1 correctly. Pattern 2 also had the right shape — but with one extra empty line at the top:
چارەسەری قوتابییەک شێوەی 1ـی بە دروستی چاپ کرد. شێوەی 2ـیش شێوەی دروستی هەبوو — بەڵام یەک دێڕی بەتاڵی زیادە لە سەرەوە:
#Pattern: 2A
for r in range(0, 5):
for c in range(r):
print("<>", end=" ")
print()
for r in range(5, 0, -1):
for c in range(r):
print("<>", end=" ")
print()<> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <> <>
The first line of the output is empty.
یەکەم دێڕی بەتاڵە.
The cause is range(0, 5). Its first value is 0. For r = 0 the inner loop range(0) runs zero times, but print() in the outer body still runs — and prints an empty line. Change the first range to range(1, 5): then the rows are 1, 2, 3, 4 pairs, followed by 5, 4, 3, 2, 1 from the second loop.
هۆکارەکە range(0, 5)ـە. یەکەم نرخی 0ـە. بۆ r = 0، range(0) سفر جار دەخولێتەوە، بەڵام print()ـی ناو جەستەی دەرەوە هەر جێبەجێ دەبێت — و دێڕێکی بەتاڵ چاپ دەکات. یەکەم range بگۆڕە بۆ range(1, 5): ئەوسا 1، 2، 3، 4 جووتیان دەبێت و دوای ئەوان 5، 4، 3، 2، 1 چاپ دەکات.
Lab: the 5–9 multiplication tableتاقیگە: خشتەی لێکدانی 5–9
Write a Python program that prints this multiplication table.
Python بنووسە کە ئەم خشتەی لێکدانە چاپ بکات.
* | 5 6 7 8 9 ----------------------- 5 | 25 30 35 40 45 6 | 30 36 42 48 54 7 | 35 42 49 56 63 8 | 40 48 56 64 72 9 | 45 54 63 72 81
Hintئاماژە
Print the header row and the dashed line first. Then one nested loop: r for the rows, c for the columns.
سەرەتا سەرەوە و هێڵە پچڕپچڕەکە چاپ بکە. پاشان : r بۆ و c بۆ .
Show answerوەڵامەکە پیشان بدە
# header row: the column numbers
print("* |", end="")
for c in range(5, 10):
print(f"{c:4}", end="")
print()
print("-" * 23)
# one row for each r
for r in range(5, 10):
print(r, "|", end="")
for c in range(5, 10):
print(f"{r * c:4}", end="") # every number 4 characters wide
print()* | 5 6 7 8 9 ----------------------- 5 | 25 30 35 40 45 6 | 30 36 42 48 54 7 | 35 42 49 56 63 8 | 40 48 56 64 72 9 | 45 54 63 72 81
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-5
The header row:
* |and then the column numbers 5 … 9, each 4 characters wide.print()ends the line.سەرەوە:
* |و پاشان ژمارەی 5 … 9، هەریەکەیان 4 پان.print()دێڕەکە تەواو دەکات.- Lineدێڕی 6
"-" * 23repeats the dash 23 times."-" * 23هێڵی کورت 23 جار دووبارە دەکاتەوە.- Lineدێڕی 9-13
For each row
r: the row number and|, then the five productsr * c, each 4 characters wide.بۆ هەر
r: ژمارەی و|، پاشان پێنج ئەنجامیr * c، هەریەکەیان 4 پان.

r adds one row; f"{…:4}" keeps the columns straight.هەر جارێکی r زیاد دەکات؛ f"{…:4}" ڕێک دەهێڵێتەوە.CêITLab: the 5–9 multiplication tableتاقیگە: خشتەی لێکدانی 5–9
Lining up the columnsڕێکخستنی ستوونەکان
A student's solution prints all the right products, but the header has no column numbers and no dashed line, and the columns do not line up:
چارەسەری قوتابییەک هەموو ئەنجامە دروستەکان چاپ دەکات، بەڵام سەرەوە ژمارەی و هێڵە پچڕپچڕەکەی تێدا نییە و ڕێک نین:
#Task 5
for r in range(5, 10):
if r == 5:
print("* |")
for c in range(5, 10):
if c == 5:
print(r, "|", end=" ")
print(r * c, end=" ")
print()* | 5 | 25 30 35 40 45 6 | 30 36 42 48 54 7 | 35 42 49 56 63 8 | 40 48 56 64 72 9 | 45 54 63 72 81
print(x, end=" ") puts one space after each number, but the numbers have different widths — 5 is one character, 25 is two. The cure is to give every number the same width. Inside an , {x:4} means "at least 4 characters": a short number is pushed to the right and filled with spaces on the left. This :4 is a ; Input and Output explains f-strings in full.
print(x, end=" ") دوای هەر ژمارەیەک یەک دادەنێت، بەڵام ژمارەکان پانییان جیاوازە — 5 یەک و 25 دوو . چارەسەرەکە ئەوەیە کە هەموو ژمارەیەک هەمان پانیی هەبێت. لەناو ، {x:4} واتە «لانیکەم 4 »: ژمارەیەکی کورت بەرەو ڕاست پاڵ دەنرێت و لای چەپی بە پڕ دەکرێتەوە. ئەم :4ـە ؛ نێوخستە و دەرخستە f-string بە تەواوی ڕوون دەکاتەوە.
print(f"[{7:4}]")
print(f"[{42:4}]")
print(f"[{108:4}]")[ 7] [ 42] [ 108]
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-3
The brackets show the width: 3, 2 and 1 spaces are added on the left.
کەوانە چوارگۆشەکان پانییەکە پیشان دەدەن: 3، 2 و 1 لە لای چەپ زیاد دەکرێن.
How many times does the inner body run?جەستەی ناوەوە چەند جار جێبەجێ دەبێت؟
It helps to know how much work a nested loop does. If the inner loop always runs the same number of times, multiply: an outer loop of n rounds and an inner loop of m rounds run the inner body times. table.py printed products; a full table needs 100.
باشە بزانیت چەند کار دەکات. ئەگەر هەمیشە هەمان ژمارە جار بخولێتەوە، لێکیان بدە: بە n جار و بە m جار، جەستەی ناوەوە جار جێبەجێ دەکەن. table.py ئەنجامی چاپ کرد؛ خشتەیەکی تەواوی پێویستی بە 100 ئەنجام هەیە.
In the triangle, the inner loop runs 1, then 2, then 3 … times. Add them up:
لە سێگۆشەکەدا 1، پاشان 2، پاشان 3 … جار دەخولێتەوە. کۆیان بکەرەوە:
For 5 rows: 15 stars — the same 15 the animation showed.
بۆ 5 : 15 ئەستێرە — هەمان ئەو 15ـەی ئەنیمەیشنەکە پیشانی دا.
count = 0
for i in range(1, 6):
for j in range(i):
count += 1 # one more run of the inner body
print("The inner body ran", count, "times")The inner body ran 15 times
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
A : one more for every run of the inner body.
: بۆ هەر جێبەجێبوونێکی جەستەی ناوەوە یەکێک زیاد دەکات.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What you seeئەوەی دەیبینیت | Fixچارەسەر |
|---|---|---|
print instead of print() after the inner loopprint لەبری print() دوای | Everything on one line, no errorهەموو شتێک لەسەر یەک دێڕ، بێ | Write print() with parentheses.print() لەگەڵ کەوانە بنووسە. |
print() indented 8 spaces (inside the inner loop)print() بە 8 (لەناو ) | Every star on its own lineهەر ئەستێرەیەک لەسەر دێڕی خۆی | Indent it 4 spaces: it belongs to the outer body.بە 4 بینووسە: سەر بە جەستەی دەرەوەیە. |
| The same variable name in both loopsهەمان ناوی لە هەردوو | The outer value is lost after the inner loopنرخی دەرەوە دوای ون دەبێت | Use i/j or row/col.i/j یان row/col بەکار بهێنە. |
No colon after for … or while …نەبوونی دوو خاڵ دوای for … یان while … | SyntaxError: expected ':'SyntaxError: expected ':' | End every loop header with :.هەموو سەردێڕێکی بە : کۆتایی پێبهێنە. |
A range that starts at 0 for a count of itemsrangeـێک کە بۆ ژماردنی دانەکان لە 0ـەوە دەست پێدەکات | An extra empty lineدێڕێکی بەتاڵی زیادە | Start at 1: range(1, n).لە 1ـەوە دەست پێبکە: range(1, n). |
| Forgetting that the stop value is excludedلەبیرکردنی ئەوەی نرخی وەستان نایەتە ناوەوە | One row or column too fewیەک یان کەمتر | range(5, 10) is 5 … 9; use range(5, 11) for 5 … 10.range(5, 10) واتە 5 … 9؛ بۆ 5 … 10 range(5, 11) بەکار بهێنە. |
| Numbers of different widthsژمارەی پانیی جیاواز | Crooked columns ڕێک نین | Use f"{x:4}".f"{x:4}" بەکار بهێنە. |
print() in the wrong placeprint() لە شوێنی هەڵەداwrong_place.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەfor i in range(1, 4):
for j in range(i):
print("*", end=" ")
print() # wrong place: inside the inner loop* * * * * *
Six stars, each on its own line, instead of a triangle of 1, 2 and 3 stars.
شەش ئەستێرە، هەریەکەیان لەسەر دێڕی خۆی، لەبری سێگۆشەیەک لە 1، 2 و 3 ئەستێرە.
Exercisesڕاهێنانەکان
Write a program that prints this triangle of numbers.
بنووسە کە ئەم سێگۆشە ژمارەییە چاپ بکات.
1 1 2 1 2 3 1 2 3 4
Hintئاماژە
Like the star triangle, but print j instead of a star — and let j start at 1.
وەک سێگۆشەی ئەستێرەکان، بەڵام لەبری ئەستێرە j چاپ بکە — و با j لە 1ـەوە دەست پێبکات.
Show answerوەڵامەکە پیشان بدە
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end=" ")
print()1 1 2 1 2 3 1 2 3 4
Print a wall of # bricks that is 3 rows high and 6 bricks wide. Store the sizes in the variables rows and columns, so that you can change them easily.
دیوارێک لە خشتی # چاپ بکە کە 3 بەرز و 6 خشت پان بێت. قەبارەکان لە rows و columnsـدا هەڵبگرە، بۆ ئەوەی بە ئاسانی بیانگۆڕیت.
Show answerوەڵامەکە پیشان بدە
rows = 3
columns = 6
for r in range(rows):
for c in range(columns):
print("#", end="")
print()###### ###### ######
end="" prints nothing after each brick, so they touch. Change rows and columns and run it again.
end="" دوای هەر خشتێک هیچ چاپ ناکات، بۆیە بە یەکەوە دەلکێن. rows و columns بگۆڕە و دیسان جێبەجێی بکە.
Print a pyramid of height 4 whose rows have 1, 3, 5 and 7 stars, centred like this.
هەرەمێک بە بەرزیی 4 چاپ بکە کە 1، 3، 5 و 7 ئەستێرەیان هەبێت و وەک ئەمە لە ناوەڕاستدا بن.
* *** ***** *******
Hintئاماژە
Each row needs two inner loops: first height - i spaces, then 2 * i - 1 stars.
هەر پێویستی بە دوو هەیە: سەرەتا height - i ، پاشان 2 * i - 1 ئەستێرە.
Show answerوەڵامەکە پیشان بدە
height = 4
for i in range(1, height + 1):
for s in range(height - i): # spaces before the stars
print(" ", end="")
for k in range(2 * i - 1): # 1, 3, 5, 7 stars
print("*", end="")
print()* *** ***** *******
This program should print a triangle of 1, 2 and 3 stars. Python refuses to run it. Why? Fix it.
ئەم دەبێت سێگۆشەیەک لە 1، 2 و 3 ئەستێرە چاپ بکات. Python ڕازی نابێت جێبەجێی بکات. بۆچی؟ چاکی بکەرەوە.
for i in range(1, 4)
for j in range(i):
print("*", end=" ")
print()Show answerوەڵامەکە پیشان بدە
File "bug.py", line 1
for i in range(1, 4)
^
SyntaxError: expected ':'The outer loop's header has no colon. Write for i in range(1, 4): and the program prints the triangle.
سەردێڕی دوو خاڵی (:) نییە. بنووسە for i in range(1, 4): و سێگۆشەکە چاپ دەکات.
Challenge: all the primes from 2 to 50ڕاهێنانی قورستر: هەموو ژمارە سەرەتاییەکانی 2 تا 50
A prime number is a whole number above 1 that only 1 and the number itself divide exactly: 2, 3, 5, 7, 11 … A nested loop can find all of them: the outer loop picks each number n, and the inner loop tries every possible divisor d from 2 to n - 1. Trying every divisor is the slowest correct method; Comparing algorithms shows faster ones.
ژمارەی سەرەتایی لە سەرووی 1 کە تەنها 1 و خودی خۆی بە تەواوی دابەشی دەکەن: 2، 3، 5، 7، 11 … دەتوانێت هەموویان بدۆزێتەوە: هەر ژمارەیەکی n هەڵدەبژێرێت و هەموو دابەشکەرێکی ئەگەری d لە 2ـەوە تا n - 1 تاقی دەکاتەوە. تاقیکردنەوەی هەموو دابەشکەرێک خاوترین ڕێگای دروستە؛ بەراوردکردنی ئەلگۆریزمەکان ڕێگای خێراتر پیشان دەدات.
Print every prime number from 2 to 50 on one line. Use n % d == 0 to test whether d divides n.
هەموو ژمارەیەکی سەرەتایی لە 2 تا 50 لەسەر یەک دێڕ چاپ بکە. بە n % d == 0 بپشکنە ئایا d بە تەواوی n دابەش دەکات.
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Hintئاماژە
Use for … else from Controlling Loops: break as soon as a divisor is found; the else of the inner loop runs only when no break happened, so that is where you print n.
for … else لە کۆنترۆڵکردنی خولەکان بەکار بهێنە: هەر کە دابەشکەرێک دۆزرایەوە break بکە؛ elseـی تەنها کاتێک جێبەجێ دەبێت کە هیچ breakـێک ڕووی نەدابێت، بۆیە لەوێدا n چاپ بکە.
Show answerوەڵامەکە پیشان بدە
for n in range(2, 51):
for d in range(2, n):
if n % d == 0:
break # a divisor: n is not prime
else:
print(n, end=" ") # no divisor found: n is prime
print()2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The outer loop: every candidate number from 2 to 50.
: هەموو ژمارەیەک لە 2 تا 50، کە دەکرێت سەرەتایی بێت.
- Lineدێڕی 2-4
The inner loop tries the divisors.
breakends only the inner loop — the outer loop goes on with the nextn.دابەشکەرەکان تاقی دەکاتەوە.
breakتەنها کۆتایی پێدێنێت — بەnـی دواتر بەردەوام دەبێت.- Lineدێڕی 5-6
This
elsebelongs to the innerfor(same indentation). It runs only if no divisor was found.ئەم
elseـە هیforـی ناوەوەیە (هەمان ). تەنها کاتێک جێبەجێ دەبێت کە هیچ دابەشکەرێک نەدۆزرابێتەوە.
n = 9:d = 2does not divide 9,d = 3does →break, and 9 is not printed.n = 7: none of 2 … 6 divides 7, the inner loop ends normally, and itselseprints 7.- For
n = 2the inner rangerange(2, 2)is empty, so theelseruns at once: 2 is prime.
n = 9:d = 2ژمارە 9 دابەش ناکات، بەڵامd = 3دابەشی دەکات ←breakو 9 چاپ ناکرێت.n = 7: هیچ یەکێک لە 2 … 6 ژمارە 7 دابەش ناکات، بە شێوەی ئاسایی تەواو دەبێت وelseـەکەی 7 چاپ دەکات.- بۆ
n = 2،range(2, 2)بەتاڵە، بۆیەelseیەکسەر جێبەجێ دەبێت: 2 ژمارەیەکی سەرەتاییە.
You only need to try divisors up to the square root of n: if 49 had a divisor above 7, it would also have one below 7. Faster programs use this idea.
پێویست ناکات دابەشکەرەکان لە n زیاتر تاقی بکەیتەوە: ئەگەر 49 دابەشکەرێکی لە سەرووی 7 هەبوایە، دابەشکەرێکی لە خوار 7ـیشی دەبوو. خێراتر ئەم بیرۆکەیە بەکار دەهێنن.
Remember: break inside a nested loop stops only the loop it is written in — the nested break challenge showed the same thing.
لەبیرت بێت: break لەناو تەنها ئەو ڕادەگرێت کە تێیدا نووسراوە — ڕاهێنانی breakـی ناوەکی هەمان شتی پیشان دا.
Challenge: all the primes from 2 to 50ڕاهێنانی قورستر: هەموو ژمارە سەرەتاییەکانی 2 تا 50
Challenge: two harder patternsڕاهێنانی قورستر: دوو شێوەی ئاڵۆزتر
Print this pyramid of height 5. Each row counts down to 1 and back up again, so both halves are the same.
ئەم هەرەمە بە بەرزیی 5 چاپ بکە. هەر بەرەو 1 دەژمێرێت و دیسان بەرەو سەرەوە دەگەڕێتەوە، بۆیە هەردوو نیوەکەی وەک یەکن.
1 212 32123 4321234 543212345
Hintئاماژە
Row row needs three inner loops: height - row spaces, then row down to 1, then 2 up to row.
row پێویستی بە سێ هەیە: height - row ، پاشان لە rowـەوە بۆ 1، پاشان لە 2ـەوە بۆ row.
Show answerوەڵامەکە پیشان بدە
height = 5
for row in range(1, height + 1):
for s in range(height - row): # spaces before the numbers
print(" ", end="")
for k in range(row, 0, -1): # row, row - 1, ..., 1
print(k, end="")
for k in range(2, row + 1): # 2, 3, ..., row
print(k, end="")
print()1 212 32123 4321234 543212345
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
Spaces push the row to the middle: 4, 3, 2, 1, 0 spaces.
پاڵ دەنێنە ناوەڕاست: 4، 3، 2، 1، 0 .
- Lineدێڕی 5-8
Down, then up: for row 3 this prints 3 2 1 and then 2 3.
بەرەو خوار، پاشان بەرەو سەرەوە: بۆ 3 ئەمە 3 2 1 و پاشان 2 3 چاپ دەکات.
Print a diamond of stars that is empty inside: only the edge is drawn.
ئەڵماسێک لە ئەستێرە چاپ بکە کە ناوەکەی بەتاڵە: تەنها لێوارەکەی دەکێشرێت.
* * * * * * * * * * * *
Hintئاماژە
First draw a full diamond: a pyramid, then the same pyramid upside down without its longest row. Then print a star only at the first and the last place of each row, and a space everywhere else.
سەرەتا ئەڵماسێکی پڕ بکێشە: هەرەمێک، پاشان هەمان هەرەم سەرەوخوار بەبێ درێژترین . پاشان تەنها لە یەکەم و دوایین شوێنی هەر ئەستێرە چاپ بکە و لە شوێنەکانی تر .
Show answerوەڵامەکە پیشان بدە
n = 4
# top half: rows 1 .. n
for row in range(1, n + 1):
for s in range(n - row):
print(" ", end="")
for k in range(2 * row - 1):
if k == 0 or k == 2 * row - 2: # first or last place in the row
print("*", end="")
else:
print(" ", end="")
print()
# bottom half: rows n - 1 .. 1
for row in range(n - 1, 0, -1):
for s in range(n - row):
print(" ", end="")
for k in range(2 * row - 1):
if k == 0 or k == 2 * row - 2:
print("*", end="")
else:
print(" ", end="")
print()* * * * * * * * * * * *
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6-10
A row has
2 * row - 1places. Place 0 and place2 * row - 2get a star; the places between get a space.2 * row - 1شوێنی هەیە. شوێنی 0 و شوێنی2 * row - 2ئەستێرە وەردەگرن؛ شوێنەکانی نێوانیان .- Lineدێڕی 13
The bottom half starts at
n - 1, so the widest row is printed only once.نیوەی خوارەوە لە
n - 1ـەوە دەست پێدەکات، بۆیە پانترین تەنها یەک جار چاپ دەکرێت.
Summaryپوختە
- A nested loop is a loop inside another loop. For every round of the outer loop, the inner loop runs completely, starting from the beginning.
- Indentation shows the nesting: inner loop 4 spaces in, its body 8 spaces, the rest of the outer body back at 4.
- Give each loop its own variable:
i/jorrow/col. - For patterns: the outer loop makes the rows, the inner loop the items of one row;
end=keeps items on one line andprint()ends the row. printwithout parentheses does nothing in Python 3.f"{x:4}"gives every number the same width, so table columns line up.- A rectangle of runs the inner body times; a triangle of rows runs it times.
- A
breakinside the inner loop ends only the inner loop; withfor … elseyou can find prime numbers.
- لەناو خولێکی تردا. بۆ هەر جارێکی ، لە سەرەتاوە و بە تەواوی جێبەجێ دەبێت.
- ناوەکیبوونەکە پیشان دەدات: 4 ، جەستەکەی 8 و باقیی جەستەی دەرەوە دیسان 4.
- تایبەت بدە بە هەر :
i/jیانrow/col. - بۆ شێوەکان: دروست دەکات و دانەکانی یەک ؛
end=دانەکان لەسەر یەک دێڕ دەهێڵێتەوە وprint()تەواو دەکات. printبەبێ کەوانە لە Python 3ـدا هیچ ناکات.f"{x:4}"هەمان پانی دەدات بە هەموو ژمارەیەک، بۆیە خشتە ڕێک دەبن.- لاکێشەیەکی جەستەی ناوەوە جار جێبەجێ دەکات؛ سێگۆشەیەک بە جار.
breakلەناو تەنها کۆتایی پێدێنێت؛ بەfor … elseدەتوانیت ژمارە سەرەتاییەکان بدۆزیتەوە.
What happens to the inner loop each time the outer loop goes round?هەر جارێک خولی دەرەوە دەخولێتەوە، چی بەسەر خولی ناوەوە دێت؟
It starts again from the beginning and runs all its rounds.
لە سەرەتاوە دەست پێدەکاتەوە و هەموو جارەکانی تەواو دەکات.
Where must print() stand to end each row of a pattern?print() دەبێت لە کوێ بێت بۆ ئەوەی هەر ڕیزێکی شێوەیەک تەواو بکات؟
In the outer body, after the inner loop (indented 4 spaces).
لە جەستەی دەرەوەدا، دوای (بە 4 ).
Why did the student's Pattern 2 start with an empty line?بۆچی شێوەی 2ـی قوتابییەکە بە دێڕێکی بەتاڵ دەستی پێکرد؟
range(0, 5) starts with 0: the inner loop ran zero times, but print() still ran.
range(0, 5) بە 0 دەست پێدەکات: سفر جار خولایەوە، بەڵام print() هەر جێبەجێ بوو.
Next: Defining and Calling Functions — and when a 2-D list needs a nested loop, see Two-Dimensional Lists.
دواتر: ناساندن و بانگکردنی کردارەکان — و کاتێک پێویستی بە هەیە، بڕوانە لیستە دووڕەهەندەکان.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A loop inside a loop: the outer loop makes rows, the inner loop prints symbols across, and print() ends each row.
لەناو : دەرەوە دروست دەکات، ناوەوە هێماکان لە تەنیشت یەکەوە پیشان دەدات و print() هەر کۆتایی پێدەهێنێت.
Whole video (5:34): a rectangle of symbols with rows and columns typed by the user.
هەموو ڤیدیۆکە (5:34): لاکێشەیەک لە هێما، کە ژمارەی و دەنووسێت.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Defining and Calling Functionsناساندن و بانگکردنی کردارەکان
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 4.2–4.3: for statements and the range() function
- Python tutorial 7.1: Fancier output formatting (f-strings, field widths)
- Harvard CS50P, lecture 2 notes: Loops (the Mario nested-loop example)
- Liang, Introduction to Programming Using Python — table of contents (chapter 5, Loops)
- PEP 8 — Style Guide for Python Code (indentation, names)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.