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 و چەند ڕاهێنانێکی قورستر: بۆ دۆزینەوەی ژمارە سەرەتاییەکان و شێوەی ئاڵۆزتر.

22 min read23 خولەک خوێندنەوە · 21 stepsهەنگاو

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= and print().
  • 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. دەتوانێت هەر لەخۆ بگرێت — تریش. ئەو لەناو تردا دەنووسرێت پێی دەگوترێت .

Definitionپێناسە

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.

و یەک یان چەند هەیە. ئەمە ئەو یاسایەیە کە هەموو ئەم وانەیە ڕوون دەکاتەوە: هەر جارێک یەک جار دەخولێتەوە، دووبارە دەست پێدەکاتەوە و لە سەرەتاوە دەڕوات. هەموو جارەکانی خۆی تەواو دەکات و تەنها ئەوسا دەچێتە هەنگاوی دواتر.

A waiter in a tea houseچایچییەک لە چایخانەدا

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 بەکار بهێنیت).

Quick checkپشکنینی خێراAn outer loop runs 3 times. On each of its rounds, the inner loop runs 4 times. How many times does the inner loop's body run in total? 3 جار دەخولێتەوە. لە هەر جارێکیدا 4 جار دەخولێتەوە. جەستەی بە گشتی چەند جار جێبەجێ دەبێت؟

Python For Loops - Python Tutorial for Absolute BeginnersProgramming with Mosh · Englishئینگلیزی · 855K viewsبینین · 14K likesلایک · Partبەشی 6:30–9:16 (ofلە 14:41)

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 ، دیسان بەشێکە لە جەستەی دەرەوە: بۆ هەر جارێکی دەرەوە یەک جار جێبەجێ دەبێت، دوای ئەوەی تەواو بوو.

Outer loop, inner loop and the rest of the outer bodyخولی دەرەوە، خولی ناوەوە و باقیی جەستەی دەرەوەnested.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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 line
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The outer loop: row takes 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 3×4=123 \times 4 = 12 times. end=" " keeps the pairs on one line.

جەستەی ناوەوە (⁦8⁩ ) 3×4=123 \times 4 = 12 جار جێبەجێ دەبێت. end=" " جووتەکان لەسەر یەک دێڕ دەهێڵێتەوە.

Lineدێڕی 4

Back to 4 spaces: this line belongs to the outer body. It runs 3 times and ends each line.

گەڕانەوە بۆ 4 : ئەم دێڕە سەر بە جەستەی دەرەوەیە. 3 جار جێبەجێ دەبێت و هەر دێڕێک تەواو دەکات.

Quick checkپشکنینی خێراIn nested.py, how many times does line 4 (print()) run?لە nested.pyـدا، دێڕی 4 (print()) چەند جار جێبەجێ دەبێت؟

The shape of a nested loopشێوەی خولێکی ناوەکی

The slide's templateقاڵبەکەی سلایدەکە

Quick checkپشکنینی خێراPython stops at line 2 of the slide's template with IndentationError: unexpected indent. Why there?Python لە دێڕی 2ی قاڵبەکەی سلایدەکەدا بە IndentationError: unexpected indent دەوەستێت. بۆچی لەوێ؟

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 ڕێگەی پێدەدات، بەڵام ئەوسا دەرەوە دەسڕێتەوە و نرخی خۆی تێدەخات. سەیر بکە:

One name for two loopsیەک ناو بۆ دوو خولsame_name.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(3):
    for i in range(2):
        print(i, end=" ")
    print("| after the inner loop, i is", i)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

Both loops use i.

هەردوو i بەکار دەهێنن.

Lineدێڕی 4

After the inner loop, i holds 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.

Quick checkپشکنینی خێراWhich pair of loop variables is the safest choice for a table?کام جووتە بۆ خشتەیەک باشترین هەڵبژاردنە؟

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 ئەستێرە وەردەگرێت.

A triangle of starsسێگۆشەیەک لە ئەستێرەstars.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# outer loop
for i in range(1, 6):
    # inner loop
    for j in range(i):
        print("*", end=" ")
    print()
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 0,1,…,i−10, 1, \ldots, i - 1: exactly i rounds.

range(i) ژمارەکانی 0,1,…,i−10, 1, \ldots, i - 1 دەدات: ڕێک 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() بەبێ هیچ شتێک لەناویدا تەنها دێڕەکە تەواو دەکات. لە جەستەی دەرەوەدایە، بۆیە بۆ هەر یەک جار جێبەجێ دەبێت.

Animation: the star program runs. The outer counter i steps from 1 to 5. In each step the inner counter j sweeps 0, 1, 2 … and one star appears for each j; then print() starts a new line. At the end the output shows a triangle of 1 to 5 stars, 15 stars in total.
Animation: the star program runs. The outer counter i steps from 1 to 5. In each step the inner counter j sweeps 0, 1, 2 … and one star appears for each j; then print() starts a new line. At the end the output shows a triangle of 1 to 5 stars, 15 stars in total.
The outer loop steps once; the inner loop sweeps across; then a new line. یەک هەنگاو دەنێت؛ بە تەواوی دەڕوات؛ پاشان دێڕێکی نوێ.CêIT
Quick checkپشکنینی خێراWhich line decides how many stars a row gets?کام دێڕ بڕیار دەدات چەند ئەستێرەی هەبێت؟

Solve any Star Pattern program in PythonSimply Coding · Englishئینگلیزی · 1.5M viewsبینین · 36K likesلایک · Partبەشی 3:13–8:51 (ofلە 18:44)

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ـەوە دێت.

Trace of stars.pyبەدواداچوونی stars.py
i (outer)i (دەرەوە)j takesj وەردەگرێتStars printedئەستێرەی چاپکراوThenپاشان
10*print() → new lineprint() ← دێڕی نوێ
20, 1* *new lineدێڕی نوێ
30, 1, 2* * *new lineدێڕی نوێ
40, 1, 2, 3* * * *new lineدێڕی نوێ
50, 1, 2, 3, 4* * * * *new line; range(1, 6) has no more valuesدێڕی نوێ؛ range(1, 6) نرخی تری نەماوە
Quick checkپشکنینی خێراYou change line 2 to for i in range(1, 8):. How many rows are printed, and how many stars are in the last row?دێڕی 2 دەگۆڕیت بۆ for i in range(1, 8):. چەند چاپ دەکرێت و دوایین چەند ئەستێرەی تێدایە؟

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 بنووسە، پاشان جارەکانی بژمێرە.

ExerciseڕاهێنانWhat does it print?چی چاپ دەکات؟

Write down exactly what this program prints.

ڕێک ئەوە بنووسە کە ئەم چاپی دەکات.

letters.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# 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وەڵامەکە پیشان بدە
Outputدەرئەنجام
O O O O O 
O O O O 
O O O 
O O 
O 
  • range(6, 1, -1) gives i = 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.
Quick checkپشکنینی خێراWhy does the inner loop use range(i - 1) and not range(i)?بۆچی range(i - 1) بەکار دەهێنێت نەک range(i)؟

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ـی ناوەوە بەناو دەڕوات.

Rows 1–3, columns 1–5ڕیزەکانی ⁦1–3⁩، ستوونەکانی ⁦1–5⁩table.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# 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 row
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() — لەگەڵ کەوانەکان — تەواو دەکات.

A grid of 3 rows and 5 columns. Above the columns, j = 1 to 5 with an arrow labelled ‘inner loop: j walks across the columns’. Left of the rows, i = 1 to 3 with a downward arrow labelled ‘outer loop: i picks the row’. Each cell shows i × j and its product; row i = 2 (2, 4, 6, 8, 10) is highlighted. print() stands at the end of every row. A note says: one round of i = one row; 3 rows × 5 columns = 15 products.خانەبەندییەک لە 3 ڕیز و 5 ستوون. لە سەرووی ستوونەکانەوە j = 1 تا 5 لەگەڵ تیرێک بە ناونیشانی «خولی ناوەوە: j بەناو ستوونەکاندا دەڕوات». لە لای چەپی ڕیزەکانەوە i = 1 تا 3 لەگەڵ تیرێکی بەرەو خوار بە ناونیشانی «خولی دەرەوە: i ڕیز هەڵدەبژێرێت». هەر خانەیەک i × j و ئەنجامەکەی پیشان دەدات؛ ڕیزی i = 2 (2، 4، 6، 8، 10) ڕەنگکراوە. print() لە کۆتایی هەموو ڕیزێکدایە. تێبینییەک دەڵێت: هەر جارێکی i = یەک ڕیز؛ 3 ڕیز و 5 ستوون = 15 ئەنجام.
i picks the row, j the column; the cell is i * j.i هەڵدەبژێرێت و j ؛ خانەکە i * jـە.CêIT
Quick checkپشکنینی خێراWhich number does table.py print in row 3, column 4?table.py لە 3 و 4ـدا چ ژمارەیەک چاپ دەکات؟

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ـدا بپشکنیت، تەنانەت لەسەر مۆبایل.

ExerciseڕاهێنانExercise (c): what is the output?ڕاهێنانی (ج): دەرخستە چییە؟

Work out the output of this program line by line.

ئەم دێڕ بە دێڕ دەربهێنە.

powers.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
i = 5
while i >= 1:
    num = 1
    for j in range(1, i + 1):
        print(num, end="xxx")
        num *= 2
    print()
    i -= 1
Hintئاماژە

end = "xxx" prints xxx instead of a new line. num *= 2 doubles num.

end = "xxx" لەبری دێڕی نوێ xxx چاپ دەکات. num *= 2 num دوو هێندە دەکات.

Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
1xxx2xxx4xxx8xxx16xxx
1xxx2xxx4xxx8xxx
1xxx2xxx4xxx
1xxx2xxx
1xxx
  • i starts at 5. The while loop runs while i >= 1, and i -= 1 lowers it at the end of each round: 5, 4, 3, 2, 1.
  • num = 1 is inside the outer loop, so every row starts again at 1.
  • The inner for runs range(1, i + 1) — i rounds. Each round prints num followed by xxx, then doubles num.
  • print() ends the row. When i reaches 0 the condition i >= 1 is 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 هەڵە دەبێت و دەوەستێت.
Trace of the first row (i = 5)بەدواداچوونی یەکەم (i = ⁦5⁩)
jjPrintedچاپکراوnum after num *= 2num دوای num *= 2
11xxx2
22xxx4
34xxx8
48xxx16
516xxx32
Quick checkپشکنینی خێراWhy does every row start with 1 again?بۆچی هەموو دیسان بە 1 دەست پێدەکات؟

Lab: two patternsتاقیگە: دوو شێوە

ExerciseڕاهێنانLab 1, Task 4: Pattern 1 and Pattern 2تاقیگەی 1، ئەرکی 4: شێوەی 1 و شێوەی 2

Write a Python program that prints Pattern 1 and then Pattern 2, exactly as shown.

Python بنووسە کە شێوەی 1 و پاشان شێوەی 2 ڕێک وەک خوارەوە چاپ بکات.

Pattern 1شێوەی 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Pattern 2شێوەی 2
<>
<> <>
<> <> <>
<> <> <> <>
<> <> <> <> <>
<> <> <> <>
<> <> <>
<> <>
<>
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وەڵامەکە پیشان بدە
patterns.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# 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()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-5

Pattern 1: r = 5 … 1. In row r, c takes the values 0,1,…,r−10, 1, \ldots, r - 1, so r - c prints r,r−1,…,1r, r - 1, \ldots, 1.

شێوەی 1: r = 5 … 1. لە rـدا c نرخەکانی 0,1,…,r−10, 1, \ldots, r - 1 وەردەگرێت، بۆیە r - c ژمارەکانی r,r−1,…,1r, r - 1, \ldots, 1 چاپ دەکات.

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 2 in the student's solutionشێوەی 2 لە چارەسەرەکەی قوتابییەکەداpattern2.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
#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 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 چاپ دەکات.

Quick checkپشکنینی خێراHow many times does the body of for c in range(0): run?جەستەی for c in range(0): چەند جار جێبەجێ دەبێت؟

Lab: the 5–9 multiplication tableتاقیگە: خشتەی لێکدانی ⁦5–9⁩

ExerciseڕاهێنانLab 1, Task 5تاقیگەی 1، ئەرکی 5

Write a Python program that prints this multiplication table.

Python بنووسە کە ئەم خشتەی لێکدانە چاپ بکات.

Expected outputدەرخستەکەی چاوەڕوانکراو
* |   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وەڵامەکە پیشان بدە
table_5_9.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# 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()
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

"-" * 23 repeats the dash 23 times.

"-" * 23 هێڵی کورت ⁦23⁩ جار دووبارە دەکاتەوە.

Lineدێڕی 9-13

For each row r: the row number and |, then the five products r * c, each 4 characters wide.

بۆ هەر r: ژمارەی و |، پاشان پێنج ئەنجامی r * c، هەریەکەیان 4 پان.

Animation: the Task 5 program on the left, its output on the right. First the header row * |   5   6   7   8   9 appears, then the dashed line. Then the outer loop's r takes the values 5, 6, 7, 8, 9: each time the highlighted lines 6–10 run and one new row of five products appears, all in neat 4-character columns.
Each round of r adds one row; f"{…:4}" keeps the columns straight.هەر جارێکی r زیاد دەکات؛ f"{…:4}" ڕێک دەهێڵێتەوە.CêIT

Lab: 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:

چارەسەری قوتابییەک هەموو ئەنجامە دروستەکان چاپ دەکات، بەڵام سەرەوە ژمارەی و هێڵە پچڕپچڕەکەی تێدا نییە و ڕێک نین:

The student's solutionچارەسەرەکەی قوتابییەکەtask5_student.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
#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()

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⁩ بە تەواوی ڕوون دەکاتەوە.

Every number 4 characters wideهەموو ژمارەیەک 4 نووسە پانwidth.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(f"[{7:4}]")
print(f"[{42:4}]")
print(f"[{108:4}]")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-3

The brackets show the width: 3, 2 and 1 spaces are added on the left.

کەوانە چوارگۆشەکان پانییەکە پیشان دەدەن: 3، 2 و 1 لە لای چەپ زیاد دەکرێن.

Quick checkپشکنینی خێراWhat does f"[{42:4}]" give?f"[{42:4}]" چی دەدات؟

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 n×mn \times m times. table.py printed 3×5=153 \times 5 = 15 products; a full 10×1010 \times 10 table needs 100.

باشە بزانیت چەند کار دەکات. ئەگەر هەمیشە هەمان ژمارە جار بخولێتەوە، لێکیان بدە: بە n جار و بە m جار، جەستەی ناوەوە n×mn \times m جار جێبەجێ دەکەن. table.py 3×5=153 \times 5 = 15 ئەنجامی چاپ کرد؛ خشتەیەکی تەواوی 10×1010 \times 10 پێویستی بە ⁦100⁩ ئەنجام هەیە.

In the triangle, the inner loop runs 1, then 2, then 3 … times. Add them up:

لە سێگۆشەکەدا 1، پاشان 2، پاشان 3 … جار دەخولێتەوە. کۆیان بکەرەوە:

1+2+3+⋯+n=n(n+1)2n=5:  5⋅62=151 + 2 + 3 + \cdots + n = \frac{n(n+1)}{2} \qquad n = 5:\; \frac{5 \cdot 6}{2} = 15

For 5 rows: 15 stars — the same 15 the animation showed.

بۆ 5 : ⁦15⁩ ئەستێرە — هەمان ئەو ⁦15⁩ـەی ئەنیمەیشنەکە پیشانی دا.

Let the program count for youبا بەرنامەکە بۆت بژمێرێتcount_runs.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

A : one more for every run of the inner body.

: بۆ هەر جێبەجێبوونێکی جەستەی ناوەوە یەکێک زیاد دەکات.

Quick checkپشکنینی خێراAn outer for loop uses range(10) and its inner loop also uses range(10). How many times does the inner body run? range(10) بەکار دەهێنێت و range(10). جەستەی ناوەوە چەند جار جێبەجێ دەبێت؟

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

Exercisesڕاهێنانەکان

ExerciseڕاهێنانA triangle of numbersسێگۆشەیەک لە ژمارە

Write a program that prints this triangle of numbers.

بنووسە کە ئەم سێگۆشە ژمارەییە چاپ بکات.

Expected outputدەرخستەکەی چاوەڕوانکراو
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وەڵامەکە پیشان بدە
numbers.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(1, 5):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
ExerciseڕاهێنانA rectangle of bricksلاکێشەیەک لە خشت

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وەڵامەکە پیشان بدە
bricks.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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 بگۆڕە و دیسان جێبەجێی بکە.

ExerciseڕاهێنانA pyramidهەرەمێک

Print a pyramid of height 4 whose rows have 1, 3, 5 and 7 stars, centred like this.

هەرەمێک بە بەرزیی 4 چاپ بکە کە 1، 3، 5 و 7 ئەستێرەیان هەبێت و وەک ئەمە لە ناوەڕاستدا بن.

Expected outputدەرخستەکەی چاوەڕوانکراو
   *
  ***
 *****
*******
Hintئاماژە

Each row needs two inner loops: first height - i spaces, then 2 * i - 1 stars.

هەر پێویستی بە دوو هەیە: سەرەتا height - i ، پاشان 2 * i - 1 ئەستێرە.

Show answerوەڵامەکە پیشان بدە
pyramid.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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()
ExerciseڕاهێنانFind the bugباگەکە بدۆزەوە

This program should print a triangle of 1, 2 and 3 stars. Python refuses to run it. Why? Fix it.

ئەم دەبێت سێگۆشەیەک لە 1، 2 و 3 ئەستێرە چاپ بکات. Python ڕازی نابێت جێبەجێی بکات. بۆچی؟ چاکی بکەرەوە.

bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(1, 4)
    for j in range(i):
        print("*", end=" ")
    print()
Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
  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): و سێگۆشەکە چاپ دەکات.

Quick checkپشکنینی خێراThe outer loop is for a in range(2):, the inner loop is for b in range(3): and the inner body is print(a, b). How many lines are printed? for a in range(2):ـە، for b in range(3):ـە و جەستەی ناوەوە print(a, b)ـە. چەند دێڕ چاپ دەکرێت؟

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 تاقی دەکاتەوە. تاقیکردنەوەی هەموو دابەشکەرێک خاوترین ڕێگای دروستە؛ بەراوردکردنی ئەلگۆریزمەکان ڕێگای خێراتر پیشان دەدات.

ExerciseڕاهێنانPrint the primes from 2 to 50ژمارە سەرەتاییەکانی 2 تا ⁦50⁩ چاپ بکە

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 دابەش دەکات.

Expected outputدەرخستەکەی چاوەڕوانکراو
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وەڵامەکە پیشان بدە
primes_2_50.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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()
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. break ends only the inner loop — the outer loop goes on with the next n.

دابەشکەرەکان تاقی دەکاتەوە. break تەنها کۆتایی پێدێنێت — بە nـی دواتر بەردەوام دەبێت.

Lineدێڕی 5-6

This else belongs to the inner for (same indentation). It runs only if no divisor was found.

ئەم elseـە هی forـی ناوەوەیە (هەمان ). تەنها کاتێک جێبەجێ دەبێت کە هیچ دابەشکەرێک نەدۆزرابێتەوە.

  • n = 9: d = 2 does not divide 9, d = 3 does → break, and 9 is not printed.
  • n = 7: none of 2 … 6 divides 7, the inner loop ends normally, and its else prints 7.
  • For n = 2 the inner range range(2, 2) is empty, so the else runs 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ـی ناوەکی هەمان شتی پیشان دا.

Quick checkپشکنینی خێراIn primes_2_50.py, how many times does the inner loop's body run for n = 9?لە primes_2_50.pyـدا، بۆ n = 9 جەستەی چەند جار جێبەجێ دەبێت؟

Challenge: all the primes from 2 to 50ڕاهێنانی قورستر: هەموو ژمارە سەرەتاییەکانی 2 تا ⁦50⁩

Challenge: two harder patternsڕاهێنانی قورستر: دوو شێوەی ئاڵۆزتر

ExerciseڕاهێنانA number pyramidهەرەمێکی ژمارەیی

Print this pyramid of height 5. Each row counts down to 1 and back up again, so both halves are the same.

ئەم هەرەمە بە بەرزیی 5 چاپ بکە. هەر بەرەو 1 دەژمێرێت و دیسان بەرەو سەرەوە دەگەڕێتەوە، بۆیە هەردوو نیوەکەی وەک یەکن.

Expected outputدەرخستەکەی چاوەڕوانکراو
    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وەڵامەکە پیشان بدە
number_pyramid.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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()
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⁩ چاپ دەکات.

ExerciseڕاهێنانA hollow diamondئەڵماسێکی بۆش

Print a diamond of stars that is empty inside: only the edge is drawn.

ئەڵماسێک لە ئەستێرە چاپ بکە کە ناوەکەی بەتاڵە: تەنها لێوارەکەی دەکێشرێت.

Expected outputدەرخستەکەی چاوەڕوانکراو
   *
  * *
 *   *
*     *
 *   *
  * *
   *
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وەڵامەکە پیشان بدە
hollow_diamond.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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 - 1 places. Place 0 and place 2 * row - 2 get 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ـەوە دەست پێدەکات، بۆیە پانترین تەنها یەک جار چاپ دەکرێت.

Quick checkپشکنینی خێراIn the number pyramid with height = 5, how many digits does the last row have?لە هەرەمە ژمارەییەکەدا بە height = 5، دوایین چەند ژمارەی تێدایە؟

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/j or row/col.
  • For patterns: the outer loop makes the rows, the inner loop the items of one row; end= keeps items on one line and print() ends the row.
  • print without parentheses does nothing in Python 3.
  • f"{x:4}" gives every number the same width, so table columns line up.
  • A rectangle of n×mn \times m runs the inner body n×mn \times m times; a triangle of nn rows runs it 1+2+⋯+n1 + 2 + \cdots + n times.
  • A break inside the inner loop ends only the inner loop; with for … else you can find prime numbers.
  • لەناو خولێکی تردا. بۆ هەر جارێکی ، لە سەرەتاوە و بە تەواوی جێبەجێ دەبێت.
  • ناوەکیبوونەکە پیشان دەدات: 4 ، جەستەکەی 8 و باقیی جەستەی دەرەوە دیسان 4.
  • تایبەت بدە بە هەر : i/j یان row/col.
  • بۆ شێوەکان: دروست دەکات و دانەکانی یەک ؛ end= دانەکان لەسەر یەک دێڕ دەهێڵێتەوە و print() تەواو دەکات.
  • print بەبێ کەوانە لە ⁦Python 3⁩ـدا هیچ ناکات.
  • f"{x:4}" هەمان پانی دەدات بە هەموو ژمارەیەک، بۆیە خشتە ڕێک دەبن.
  • لاکێشەیەکی n×mn \times m جەستەی ناوەوە n×mn \times m جار جێبەجێ دەکات؛ سێگۆشەیەک بە nn 1+2+⋯+n1 + 2 + \cdots + n جار.
  • break لەناو تەنها کۆتایی پێدێنێت؛ بە for … else دەتوانیت ژمارە سەرەتاییەکان بدۆزیتەوە.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Nested loops in Python are easy ➿Bro Code · Englishئینگلیزی · 600K viewsبینین · 13K likesلایک

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Defining and Calling Functionsناساندن و بانگکردنی کردارەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.