PythonPython · Chapterبەش 7 · Lectureوانە 7.2

The for Loop and range()خولی for و range()

The for loop goes through every item of a sequence. Learn range(start, stop, step) with its rules and traps, trace the course's Exercise 1 and convert it to a while loop, solve Lab 1's text and dice tasks with the random module, and choose between for and while.

بە هەموو دانەیەکی دەڕوات. فێری range(start, stop, step) ببە لەگەڵ یاسا و تەڵەکانی، بەدوای ڕاهێنانی 1ی کۆرسەکەدا بچۆ و بیگۆڕە بۆ ، ئەرکەکانی دەق و زاری تاقیگەی 1 بە random چارەسەر بکە و لە نێوان for و whileدا هەڵبژێرە.

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

What you will learnچی فێر دەبیت

  • Write for loops over strings, lists, tuples and ranges, and say what the loop variable holds in each round.
  • Predict the numbers of any range(start, stop, step), including negative steps, empty ranges and errors.
  • Trace a loop with a trace table and convert between for and while without off-by-one errors.
  • Use import random and random.randint() and know why eval(input()) and curly quotes must be avoided.
  • Walk through a string by index with while, and build new strings, counts and series sums with accumulators.
  • بەسەر ، ، و rangeـدا بنووسیت و بڵێیت لە هەر چی هەڵدەگرێت.
  • ژمارەکانی هەر range(start, stop, step)ـێک پێشبینی بکەیت، لەوانەش هەنگاوی سالب، rangeی بەتاڵ و هەڵەکان.
  • بە بەدوای بچیت و لە نێوان for و whileدا بەبێ بیگۆڕیت.
  • import random و random.randint() بەکار بهێنیت و بزانیت بۆچی دەبێت خۆت لە eval(input()) و چەماوە بپارێزیت.
  • بە while بە بە بڕۆیت و بە نوێ، ژمارەکان و کۆی زنجیرەکان دروست بکەیت.

The for loopخولی for

Definitionپێناسە

A loop that repeats once for each value in a range or collection. In PHP, Java and C# it has three parts — start, condition and step: for ($i = 0; $i < 10; $i++); Python's for goes through the items of a sequence: for i in range(10):.

کە بۆ هەر نرخێکی مەودایەک یان کۆکراوەیەک جارێک دووبارە دەبێتەوە. لە PHP، Java و ⁦C#⁩ـدا سێ بەشی هەیە، سەرەتا، مەرج و هەنگاو: for ($i = 0; $i < 10; $i++)؛ forـی Python بەناو دانەکانی زنجیرەیەکدا دەڕوات: for i in range(10):.

A Python for loop goes through each value in a sequence, one after another. In every round the loop variable holds the next value, and the indented body runs once with it. When there are no values left, the loop ends.

بە هەموو نرخێکی دەڕوات، یەک لە دوای یەک. لە هەر نرخی دواتر هەڵدەگرێت و (دێڕە بەرەوناوچووەکان) یەکجار لەگەڵیدا جێبەجێ دەبێت. کاتێک هیچ نرخێک نەما، کۆتایی دێت.

Often you know exactly how many times the body must run — 10 times, once per letter. Then the loop variable works as a control variable that counts the rounds. A loop of this type is called a counter-controlled loop (or count-controlled loop).

زۆرجار ڕێک دەزانیت دەبێت چەند جار جێبەجێ ببێت — ⁦10⁩ جار، یان جارێک بۆ هەر پیتێک. ئەوکات وەک کۆنترۆڵ کار دەکات کە دەژمێرێت. ئەم جۆرە پێی دەوترێت .

The shape of a for loopشێوەی خولی forfor_syntax.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە
for var in sequence:
    # loop body
    statement(s)
Greeting everyone in a listسڵاوکردن لە هەموو کەسێکی ناو لیستێکfriends.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
friends = ["Aram", "Shilan", "Karwan"]
for name in friends:
    print("Hello,", name)
print("Done")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

name is the loop variable: "Aram", then "Shilan", then "Karwan".

name : "Aram"، پاشان "Shilan"، پاشان "Karwan".

Lineدێڕی 4

Not indented: runs once, after the loop.

بەرەوناو نەچووە: یەکجار دوای جێبەجێ دەبێت.

Calling the rollناوخوێندنەوە لە پۆلدا

A teacher calls the roll from the attendance list: the first name, then the next, down to the last one, and then stops. The teacher does not need to count the students first — the list decides how many rounds there are. A for loop over a sequence works the same way.

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

Where the analogy stops workingسنووری ئەم نموونەیە

A teacher may skip a name or stop early when the bell rings. A for loop visits every item in order unless you use break or continue (Controlling loops).

مامۆستا دەکرێت ناوێک جێبهێڵێت یان کاتێک زەنگ لێ دەدرێت زوو بوەستێت. سەردانی هەموو دانەیەک بە ڕیز دەکات، مەگەر break یان continue بەکار بهێنیت (کۆنترۆڵکردنی خولەکان).

Quick checkپشکنینی خێراIn for name in friends:, what is name in the second round?لە for name in friends:ـدا، name لە دووەمدا چییە؟

18- دوباره بوونه وه له ڕێگه ی فۆر For Loop In PythonMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 6.7K viewsبینین · 215 likesلایک

In Kurdish: repeating code with the for loop in Python.

بە کوردی: دووبارەکردنەوەی کۆد بە لە .

Kurdish (Sorani), whole video (12:33): part 18 of MR. Ant's Python course.

ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە ⁦(12:33)⁩: بەشی ⁦18⁩ لە کۆرسی ⁦MR. Ant⁩.

Sequences: strings, lists and tuplesزنجیرەکان: زنجیرەنووسەکان، لیستەکان و تاپڵەکان

A sequence holds several items of data stored one after the other, in a fixed order. In Python, strings, lists and tuples are sequence types (so is range, next screen). A for loop works on all of them: over a string it gives one character per round. (Lists are taught in Lists, tuples in Tuples, sets and dictionaries.)

چەند دانەیەک هەڵدەگرێت کە یەک لە دوای یەک بە ڕیزێکی جێگیر دانراون. لە ، و جۆرەکانی (rangeیش، لە شاشەی داهاتوودا). لەسەر هەموویان کار دەکات: بەسەر هەر دەدات. ( لە لیستەکان و لە تاپڵەکان، کۆمەڵەکان و فەرهەنگەکان دەخوێنرێن.)

A tuple and a stringتاپڵێک و زنجیرەنووسەیەکsequences.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for day in ("Saturday", "Sunday", "Monday"):
    print(day)
for letter in "LANA":
    print(letter)
Animation of for letter in "LANA": print(letter). The four letters L, A, N, A sit in boxes with indexes 0 to 3. A pointer labelled letter moves from box to box; the variable box shows "L", "A", "N", "A" in turn, and the output panel gains one letter per round. After the fourth round no letters are left and the loop ends.
Animation of for letter in "LANA": print(letter). The four letters L, A, N, A sit in boxes with indexes 0 to 3. A pointer labelled letter moves from box to box; the variable box shows "L", "A", "N", "A" in turn, and the output panel gains one letter per round. After the fourth round no letters are left and the loop ends.
One round per character: the string decides how many rounds there are.یەک بۆ هەر : دیاری دەکات چەند هەیە.CêIT

Looping over the letters of a word is a classic first exercise; you will do it with your own name in Lab task 1-2. A for loop can also sit inside another for loop, for example to draw a triangle of * row by row — that is taught in Nested loops.

سووڕانەوە بەسەر پیتەکانی وشەیەکدا ڕاهێنانێکی کلاسیکیی سەرەتاییە؛ لە ئەرکی تاقیگەی ⁦1-2⁩ بە ناوی خۆت دەیکەیت. دەکرێت لە ناو تریشدا بێت، بۆ نموونە بۆ کێشانی سێگۆشەیەک لە * ڕیز بە ڕیز — ئەمە لە خولە ناوەکییەکان فێر دەکرێت.

Quick checkپشکنینی خێراHow many lines does for ch in "Hi!": print(ch) print?for ch in "Hi!": print(ch) چەند دێڕ چاپ دەکات؟

Sequences: strings, lists and tuplesزنجیرەکان: زنجیرەنووسەکان، لیستەکان و تاپڵەکان

The same walk with while and an indexهەمان ڕۆیشتن بە while و ئیندێکسێک

A for loop hides the indexes. You can also walk through a string by its indexes with a while loop: start at i = 0, read word[i], add 1, and stop when i reaches len(word) — the last index is len(word) - 1. That needs three extra parts (start, test, step), so for a plain walk for is simpler. The index version helps when you also need the position, or want to move by more than one character.

دەشارێتەوە. هەروەها دەتوانیت بە ، بە ، بە بڕۆیت: لە i = 0ەوە دەست پێ بکە، word[i] بخوێنەرەوە، 1 زیاد بکە و کاتێک i گەیشتە len(word) بوەستە — دوایین len(word) - 1ـە. ئەمە سێ بەشی زیادەی پێویستە (سەرەتا، پشکنین، هەنگاو)، بۆیە بۆ ڕۆیشتنێکی سادە for ئاسانترە. وەشانی بەسوودە کاتێک شوێنەکەشت پێویستە، یان دەتەوێت لە یەک زیاتر باز بدەیت.

for over the letters, while over the indexesfor بەسەر پیتەکاندا، while بەسەر ئیندێکسەکانداPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "LANA"
for letter in word:
    print(letter, end=" ")
print()
i = 0
while i < len(word):
    print(i, word[i])
    i += 1
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

for: no index, no counter.

for: نە ، نە .

Lineدێڕی 5-8

while: start, test and step are written by you.

while: سەرەتا، پشکنین و هەنگاو خۆت دەیاننووسیت.

Quick checkپشکنینی خێراIn while i < len(word):, why < and not <=?لە while i < len(word):ـدا، بۆچی < نەک <=؟

Counting with range()ژماردن بە range()

To repeat something a number of times you need a sequence of numbers. The function range(a, b) produces the whole numbers a, a+1, …, b−2, b−1a,\ a+1,\ \dots,\ b-2,\ b-1: it starts at a and stops before b. In maths words, these are the whole numbers of the half-open interval [a,b)[a, b) — see Intervals.

بۆ دووبارەکردنەوەی شتێک چەند جارێک پێویستت بە لە ژمارە هەیە. range(a, b) a, a+1, …, b−2, b−1a,\ a+1,\ \dots,\ b-2,\ b-1 دەدات: لە aەوە دەست پێدەکات و پێش b دەوەستێت. بە زمانی بیرکاری، ئەمانە [a,b)[a, b)ن — بڕوانە ماوەکان.

range(a,b)  →  a, a+1, …, b−1(the integers n with a≤n<b)\texttt{range}(a, b) \;\rightarrow\; a,\ a+1,\ \dots,\ b-1 \qquad (\text{the integers } n \text{ with } a \le n < b)

For example range(2, 6) gives 2, 3, 4, 5: four numbers, because 6−2=46 - 2 = 4.

بۆ نموونە range(2, 6) 2، 3، 4 و 5 دەدات: چوار ژمارە، چونکە 6−2=46 - 2 = 4.

range() takes between 1 and 3 whole-number arguments:

range() لە 1 تا 3 وەردەگرێت کە :

range(stop), range(start, stop), range(start, stop, step)⁦range(stop)⁩، ⁦range(start, stop)⁩ و ⁦range(start, stop, step)⁩
ArgumentMeaningواتاIf you leave it outئەگەر نەینووسیت
startThe number the sequence begins withئەو ژمارەیەی پێی دەست پێدەکاتit is 00ـە
stopCounted up to but not includedتا ئەوێ دەژمێردرێت بەڵام خۆی تێدا نییەalways requiredهەمیشە پێویستە
stepHow much to add each time; a negative step counts downهەر جارێک چەند زیاد بکرێت؛ هەنگاوی سالب بەرەو خوار دەژمێرێتit is 11ـە
The slide's first exampleیەکەم نموونەی سلایدەکەrange_2_6.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(2,6):
   print(n)
Quick checkپشکنینی خێراWhich call gives exactly the numbers 1, 2, 3, 4, 5?کام بانگکردن ڕێک ژمارەکانی 1، 2، 3، 4 و 5 دەدات؟

for Loop with range() Function in PythonNeso Academy · Englishئینگلیزی · 152K viewsبینین · 2.9K likesلایک

What range(stop), range(start, stop) and range(start, stop, step) produce, and how a for loop walks through them.

range(stop)، range(start, stop) و range(start, stop, step) چی دروست دەکەن و چۆن یەک بە یەک بەسەریاندا دەڕوات.

Whole video (11:55). Chapters: 0:41 range(), 3:26 for loop, 8:00 start and stop, 10:22 points to remember.

هەموو ڤیدیۆکە ⁦(11:55)⁩. بەشەکان: ⁦0:41⁩ range()، ⁦3:26⁩ ، ⁦8:00⁩ start و stop، ⁦10:22⁩ خاڵە گرنگەکان.

Counting with range()ژماردن بە range()

Try it: what does range() give?تاقیی بکەرەوە: range() چی دەدات؟

Exerciseڕاهێنانrange with one argumentrange بە یەک ئارگیومێنت

What does this loop print?

ئەم چی چاپ دەکات؟

range_4.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(4):
    print(n)
Show answerوەڵامەکە پیشان بدە
range_4.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(4):
    print(n)

Only stop is given, so start is 0 and step is 1: four numbers, 0 to 3.

تەنها stop دراوە، بۆیە start 0ـە و step 1: چوار ژمارە، 0 تا 3.

Exerciseڕاهێنانrange with a steprange بە هەنگاو

What does this loop print?

ئەم چی چاپ دەکات؟

range_2_9_3.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(2,9,3):
    print(n)
Show answerوەڵامەکە پیشان بدە
range_2_9_3.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(2,9,3):
    print(n)

Start at 2 and add 3: 2, 5, 8. The next number, 11, is not below 9, so the sequence stops.

لە 2ەوە دەست پێ بکە و 3 زیاد بکە: 2، 5، 8. ژمارەی دواتر، ⁦11⁩، لە 9 کەمتر نییە، بۆیە دەوەستێت.

The animation shows the same rule with range(2, 10, 3): each jump adds the step, and the sequence stops as soon as the next number is not below stop.

ئەنیمەیشنەکە هەمان یاسا بە range(2, 10, 3) پیشان دەدات: هەر بازدانێک هەنگاوەکە زیاد دەکات و هەر کاتێک ژمارەی دواتر لە stop کەمتر نەبێت دەوەستێت.

Animation of range(2, 10, 3) on a number line from 0 to 12. The stop value 10 is marked with an open circle. A dot appears at 2 (start), then a +3 jump lands on 5, then on 8. The next +3 jump would land on 11, which is not below 10, so it is crossed out and the sequence stops. The result is [2, 5, 8].
Animation of range(2, 10, 3) on a number line from 0 to 12. The stop value 10 is marked with an open circle. A dot appears at 2 (start), then a +3 jump lands on 5, then on 8. The next +3 jump would land on 11, which is not below 10, so it is crossed out and the sequence stops. The result is [2, 5, 8].
Start, add the step, stop before stop.دەست پێ بکە، هەنگاوەکە زیاد بکە، پێش stop بوەستە.CêIT
Quick checkپشکنینی خێراDo range(2, 9, 3) and range(2, 10, 3) give the same numbers?ئایا range(2, 9, 3) و range(2, 10, 3) هەمان ژمارەکان دەدەن؟

Counting with range()ژماردن بە range()

Four patterns of range()چوار شێوازی range()

range() examples from the courseنموونەکانی range() لە کۆرسەکەوە
LoopValues of iنرخەکانی iNoteتێبینی
for i in range(6):0, 1, 2, 3, 4, 5The loop runs 6 times. 6 جار جێبەجێ دەبێت.
for i in range(10, 16):10, 11, 12, 13, 14, 15The ending value is never included.نرخی کۆتایی هەرگیز تێدا نییە.
for i in range(0, 9, 2):0, 2, 4, 6, 8The third argument is the step. سێیەم هەنگاوەکەیە.
for i in range(5, 0, -1):5, 4, 3, 2, 1A negative step counts down.هەنگاوی سالب بەرەو خوار دەژمێرێت.
Checking all four rowsپشکنینی هەر چوار ڕیزەکەrange_table.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(list(range(6)))
print(list(range(10, 16)))
print(list(range(0, 9, 2)))
print(list(range(5, 0, -1)))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-4

list() turns a range into a list so that print shows all its numbers.

list() rangeـێک دەکات بە بۆ ئەوەی print هەموو ژمارەکانی پیشان بدات.

Quick checkپشکنینی خێراHow many times does for i in range(10, 16): run?for i in range(10, 16): چەند جار جێبەجێ دەبێت؟

Counting with range()ژماردن بە range()

Counting down, empty ranges and step 0ژماردن بەرەو خوار، rangeی بەتاڵ و هەنگاوی 0

With a negative step, range counts down and stops before it reaches stop from above: range(10, 0, -2) gives 10, 8, 6, 4, 2. If start is already past stop in the direction of the step, the range is simply empty and the loop body never runs. A step of 0 would never move, so Python refuses it with a ValueError.

بە هەنگاوی سالب، range بەرەو خوار دەژمێرێت و پێش گەیشتن بە stop لە سەرەوە دەوەستێت: range(10, 0, -2) ⁦10⁩، 8، 6، 4 و 2 دەدات. ئەگەر start بە ئاڕاستەی هەنگاوەکە پێشتر لە stop تێپەڕیبێت، rangeەکە تەنها بەتاڵە و هەرگیز جێبەجێ نابێت. هەنگاوی 0 هەرگیز ناجووڵێت، بۆیە بە ValueError ڕەتی دەکاتەوە.

count_down.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(10, 0, -2):
    print(n, end=" ")
print()
print(list(range(5, 1)))
print(list(range(1, 5, -1)))
print(list(range(-3, 3)))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-5

Both ranges are empty: you cannot count up from 5 to 1, or down from 1 to 5.

هەردوو rangeەکە بەتاڵن: ناتوانیت لە 5ەوە بەرەو سەر بۆ 1 بژمێریت، یان لە 1ەوە بەرەو خوار بۆ 5.

Lineدێڕی 6

Negative numbers are fine as start or stop.

وەک start یان stop کێشەیان نییە.

step_zero.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(1, 10, 0):
    print(n)
Quick checkپشکنینی خێراWhat does list(range(3, 0, -1)) give?list(range(3, 0, -1)) چی دەدات؟

Counting with range()ژماردن بە range()

A range is not a listrange لیست نییە

A range does not store all its numbers. It stores only start, stop and step and works out each number when the loop asks for it, so range(1000000) needs no more memory than range(10). That is why print(range(10)) shows range(0, 10) and not the numbers; list() makes a real list when you want to see them. A range still supports len(), in and indexing.

range هەموو ژمارەکانی هەڵناگرێت. تەنها start، stop و step هەڵدەگرێت و هەر ژمارەیەک کاتێک داوای دەکات دەیدۆزێتەوە، بۆیە range(1000000) لە range(10) زیاتری ناوێت. لەبەر ئەمە print(range(10)) range(0, 10) پیشان دەدات نەک ژمارەکان؛ list() ڕاستەقینە دروست دەکات کاتێک دەتەوێت ببینیت. range هێشتا len()، in و پشتگیری دەکات.

range_object.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
numbers = range(10)
print(numbers)
print(list(numbers))
print(len(range(0, 1000000, 5)))
print(7 in range(0, 20, 7), 8 in range(0, 20, 7))
print(range(0, 20, 2)[3])
print(sum(range(1, 101)))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

200,000 numbers, but nothing is stored: len() is worked out from start, stop and step.

⁦200,000⁩ ژمارە، بەڵام هیچیان هەڵنەگیراون: len() لە start، stop و step دەدۆزرێتەوە.

Lineدێڕی 7

sum() adds up all the numbers 1 to 100.

sum() هەموو ژمارەکانی 1 تا ⁦100⁩ کۆ دەکاتەوە.

Quick checkپشکنینی خێراWhat does print(range(3)) show?print(range(3)) چی پیشان دەدات؟

Exercise: why does this program fail?ڕاهێنان: بۆچی ئەم بەرنامەیە سەرناکەوێت؟

ExerciseڕاهێنانFour times Python?چوار جار Python؟

The slide asks for the output of this loop. Copy it exactly as it is and run it. What happens, and why?

سلایدەکە داوای ئەم دەکات. ڕێک وەک خۆی کۆپیی بکە و جێبەجێی بکە. چی ڕوودەدات و بۆچی؟

Exactly as on the slideڕێک وەک سلایدەکەcurly.pyTEXTType in a terminalلە تێرمیناڵدا بینووسە
for n in range(4):
   print(“Python” )
Show answerوەڵامەکە پیشان بدە
What Python saysPython چی دەڵێتcurly.pyTEXTType in a terminalلە تێرمیناڵدا بینووسە
for n in range(4):
   print(“Python” )

The quotes are curly quotes, “ and ”, made by a word processor or a slide program. Python only accepts straight quotes " and ', so it reports a syntax error before running anything. With straight quotes the loop prints Python four times:

چەماوەن، “ و ”، کە پرۆگرامی نووسین یان سلاید دروستیان دەکات. تەنها ڕاست " و ' قبووڵ دەکات، بۆیە پێش جێبەجێکردنی هەر شتێک ڕادەگەیەنێت. بە ڕاست چوار جار Python چاپ دەکات:

straight.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(4):
    print("Python")
Quick checkپشکنینی خێراWhich line is valid Python?کام دێڕ دروستە؟

Counting from 1 to 10 with forژماردن لە 1 تا ⁦10⁩ بە for

The course report prints Example 1 to Example 10 with a for loop. Compare it with the while version in Counting from 1 to 10: range(1, 11) sets the start, the test and the step for you.

ڕاپۆرتی کۆرسەکە بە Example 1 تا Example 10 چاپ دەکات. بەراوردی بکە لەگەڵ وەشانی while لە ژماردن لە 1 تا ⁦10⁩: range(1, 11) سەرەتا، پشکنین و هەنگاوەکە بۆت دادەنێت.

From the reportلە ڕاپۆرتەکەوەreport_for.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(1, 11):
  print("Example", i)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

range(1, 11): 1 to 10, because 11 is not included.

range(1, 11): 1 تا ⁦10⁩، چونکە ⁦11⁩ تێدا نییە.

Lineدێڕی 2

2-space indentation works, but PEP 8 asks for 4.

2 کار دەکات، بەڵام ⁦PEP 8⁩ داوای 4 دەکات.

The same count with while and forهەمان ژماردن بە while و for
Partبەشwhilefor
Start valueنرخی سەرەتاi = 1 (before the loop)i = 1 (پێش )range(1, …)
Testپشکنینwhile i <= 10:range(…, 11)
Stepهەنگاوi += 1 (in the body)i += 1 (لە ناو )range (+1)
Lines of loop codeدێڕەکانی کۆدی 42
Quick checkپشکنینی خێراWhich for loop prints the same numbers as i = 1 / while i <= 10: / print(i) / i += 1?کام هەمان ژمارەکانی i = 1 / while i <= 10: / print(i) / i += 1 چاپ دەکات؟

Exercise 1: trace a for loopڕاهێنانی 1: بەدوای خولێکی forـدا بچۆ

ExerciseڕاهێنانWhat is the output?دەرخستەکە چییە؟

The user types 2, 3, 4, 5 and 0 (one number per line). What does the program print? Make a trace table before you run it.

2، 3، 4، 5 و 0 دەنووسێت (هەر ژمارەیەک لە دێڕێکدا). چی چاپ دەکات؟ پێش جێبەجێکردن دروست بکە.

exercise1.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
for count in range(5):
    number = int(input("Enter an integer: "))
    total += number
print("sum is", total)
print("count is", count)
Show answerوەڵامەکە پیشان بدە
Trace table (the course group's homework answer) (وەڵامی ئەرکی ماڵەوەی گرووپەکەی کۆرسەکە)
countnumbertotal
022
135
249
3514
4014
exercise1.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
for count in range(5):
    number = int(input("Enter an integer: "))
    total += number
print("sum is", total)
print("count is", count)

range(5) gives count the values 0 to 4, so after the loop count keeps its last value, 4. The total is 2+3+4+5+0=142 + 3 + 4 + 5 + 0 = 14.

range(5) نرخەکانی 0 تا 4 دەداتە count، بۆیە دوای count دوایین نرخی خۆی دەهێڵێتەوە، 4. کۆکە 2+3+4+5+0=142 + 3 + 4 + 5 + 0 = 14ە.

The safe ways to read numbers, and what to do when the user types letters, are in Input and output and Handling errors.

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

Quick checkپشکنینی خێراWhy does the program print count is 4 and not count is 5?بۆچی count is 4 چاپ دەکات نەک count is 5؟

Exercise 1: trace a for loopڕاهێنانی 1: بەدوای خولێکی forـدا بچۆ

Exercise 1b: the same program with whileڕاهێنانی 1ب: هەمان بەرنامە بە while

ExerciseڕاهێنانConvert the for loop to a while loopخولی for بگۆڕە بۆ خولی while

Rewrite Exercise 1 with a while loop. Does your version print exactly the same two lines?

ڕاهێنانی 1 بە بنووسەرەوە. ئایا وەشانەکەت ڕێک هەمان دوو دێڕ چاپ دەکات؟

Show answerوەڵامەکە پیشان بدە

The course group's homework answer (written by hand; here typed as Python, with the capital letters of Print, Count and Sum made small):

وەڵامی ئەرکی ماڵەوەی گرووپەکەی کۆرسەکە (بە دەست نووسرابوو؛ لێرەدا وەک نووسراوە و پیتە گەورەکانی Print، Count و Sum کراون بە بچووک):

group_while.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 0
num = 0
sum = 0
while count < 5:
    num = eval(input("Enter an integer: "))
    sum += num
    count += 1
print("Sum is ", sum)
print("Count is ", count)

It is a good conversion, but it prints Count is 5, not 4. In the for loop the last value given to count is 4. The while loop adds 1 once more at the end of the fifth round, and that 5 is what makes count < 5 False. To print exactly the same lines as the for version, print count - 1 (and use int() and total):

گۆڕینێکی باشە، بەڵام Count is 5 چاپ دەکات نەک 4. لە دوایین نرخ کە دەدرێتە count 4ـە. لە کۆتایی پێنجەمدا جارێکی تر 1 زیاد دەکات و ئەو 5ـە وا دەکات count < 5 False بێت. بۆ ئەوەی ڕێک هەمان دێڕەکانی وەشانی for چاپ بکات، count - 1 چاپ بکە (و int() و total بەکار بهێنە):

exercise1_while.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
count = 0
while count < 5:
    number = int(input("Enter an integer: "))
    total += number
    count += 1
print("sum is", total)
print("count is", count - 1)
Quick checkپشکنینی خێراAfter count = 0 / while count < 5: / count += 1, what is count?دوای count = 0 / while count < 5: / count += 1، count چەندە؟

Lab 1: loops over text, and diceتاقیگەی 1: خولەکان بەسەر دەقدا و زار

The first lab sheet of the loops chapter practises for loops. Tasks 1-1, 1-2 and 3 are below; tasks 2-1 and 2-2 use continue and are in Controlling loops, and tasks 4 and 5 are in Nested loops.

یەکەم پەڕەی تاقیگەی بەشی ڕاهێنان دەکات. ئەرکەکانی ⁦1-1⁩، ⁦1-2⁩ و 3 لە خوارەوەن؛ ئەرکەکانی ⁦2-1⁩ و ⁦2-2⁩ continue بەکار دەهێنن و لە کۆنترۆڵکردنی خولەکاندان و ئەرکەکانی 4 و 5 لە خولە ناوەکییەکاندان.

Lab 1: loops over text, and diceتاقیگەی 1: خولەکان بەسەر دەقدا و زار

Task 1-1: the same text five times on one lineئەرکی ⁦1-1⁩: هەمان دەق پێنج جار لە یەک دێڕدا

ExerciseڕاهێنانTask 1-1ئەرکی ⁦1-1⁩

Use a for loop to print the text Python 5 times in one line. (The lab sheet printed the class's name, “Stage 1”; any text works the same way.)

بەکار بهێنە بۆ چاپکردنی دەقی Python 5 جار لە یەک دێڕدا. (پەڕەی تاقیگەکە ناوی پۆلەکەی چاپ دەکرد، «⁦Stage 1⁩»؛ هەر دەقێک بە هەمان شێوە کار دەکات.)

Hintئاماژە

print(..., end="") does not start a new line.

print(..., end="") دێڕێکی نوێ دەست پێ ناکات.

Show answerوەڵامەکە پیشان بدە
task_1_1.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(5):
    print(" Python ", end="")
print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

end="" replaces the new line that print normally adds.

end="" جێگەی ئەو دێڕە نوێیە دەگرێتەوە کە print بە ئاسایی زیادی دەکات.

Lineدێڕی 3

One empty print() ends the line after the loop.

یەک print()ـی بەتاڵ دێڕەکە دوای کۆتایی پێ دەهێنێت.

Quick checkپشکنینی خێراWhat would change without the last line print()?بەبێ دوایین دێڕی print() چی دەگۆڕێت؟

Lab 1: loops over text, and diceتاقیگەی 1: خولەکان بەسەر دەقدا و زار

Task 1-2: loop over a nameئەرکی ⁦1-2⁩: خول بەسەر ناوێکدا

ExerciseڕاهێنانTask 1-2ئەرکی ⁦1-2⁩

Repeat the previous task, but this time let the user enter their name, and use the name as the sequence of your loop: print each letter followed by the text.

ئەرکی پێشوو دووبارە بکەرەوە، بەڵام ئەمجارە با ناوی خۆی بنووسێت و ناوەکە وەک بەکار بهێنە: هەر پیتێک و دوای ئەو دەقەکە چاپ بکە.

Expected outputدەرخستەی چاوەڕوانکراو
Your name is :    Lana
L  Python
a  Python
n  Python
a  Python

The expected output on the lab sheet, with a neutral name and our text.

چاوەڕوانکراوی پەڕەی تاقیگەکە، بە ناوێکی بێلایەن و دەقەکەی ئێمە.

Show answerوەڵامەکە پیشان بدە
task_1_2.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
name = input("Your name is :    ")
for letter in name:
    print(letter, " Python")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

input() returns the name as a string — a sequence of letters.

input() ناوەکە وەک دەگەڕێنێتەوە — لە پیت.

Lineدێڕی 3

print puts one space between its two values, and the text itself starts with a space: that makes two spaces.

print دەخاتە نێوان دوو نرخەکەیەوە و خودی دەقەکەش بە دەست پێدەکات: ئەمە دەکاتە دوو .

The course owner solved it on a phone with the name written into the code (here a neutral name):

خاوەنی کۆرسەکە لەسەر مۆبایل چارەسەری کرد و ناوەکەی ڕاستەوخۆ لە کۆدەکەدا نووسی (لێرەدا ناوێکی بێلایەن):

Owner's solutionچارەسەری خاوەنی کۆرسەکەtask_1_2_owner.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
name = "Lana"
for a in name:
    print(a, "Python")
Quick checkپشکنینی خێراname = input() and the user types Hana. How many times does for letter in name: run?name = input() و Hana دەنووسێت. for letter in name: چەند جار جێبەجێ دەبێت؟

Lab 1: loops over text, and diceتاقیگەی 1: خولەکان بەسەر دەقدا و زار

Task 3: rolling two diceئەرکی 3: هەڵدانی دوو زار

Task 3 asks for something new: chance. Python's standard library — the modules that come with every Python — has a random module for it. Write import random once at the top of the program; then random.randint(a, b) gives a random number from a to b. Both ends are included, so random.randint(1, 6) can give 6 — the opposite of range(1, 6), which stops at 5. (More about import in Modules.)

ئەرکی 3 داوای شتێکی نوێ دەکات: هەڵکەوت. — ئەو لەگەڵ هەموو دامەزراندنێکی دێن — randomـی بۆ ئەمە هەیە. یەکجار لە سەرەوەی import random بنووسە؛ پاشان random.randint(a, b) لە a تا b دەدات. هەردوو سەرەکە تێدان، بۆیە random.randint(1, 6) دەتوانێت 6 بدات — پێچەوانەی range(1, 6) کە لە 5دا دەوەستێت. (زیاتر دەربارەی import لە مۆدیوڵەکان.)

ExerciseڕاهێنانTask 3ئەرکی 3

Write a Python program that simulates tossing a pair of dice 10 times.

بنووسە کە ⁦10⁩ جار هەڵدانی جووتێک زار لاسایی بکاتەوە.

Show answerوەڵامەکە پیشان بدە

The course owner's solution prints two dice per line, ten lines. random.seed(7) is added here only so that this test run can be repeated; without it every run is different.

چارەسەری خاوەنی کۆرسەکە لە هەر دێڕێکدا دوو زار چاپ دەکات، دە دێڕ. random.seed(7) تەنها لێرەدا زیاد کراوە بۆ ئەوەی ئەم جێبەجێکردنە تاقیکارییە دووبارە بکرێتەوە؛ بەبێ ئەو هەموو جێبەجێکردنێک جیاوازە.

Owner's solution (+ a seed for testing)چارەسەری خاوەنی کۆرسەکە (⁦+⁩ تۆوی هەڕەمەکی بۆ تاقیکردنەوە)dice_owner.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
import random

random.seed(7)  # only so that this test run can be repeated

for a in range(10):
    print(random.randint(1, 6), random.randint(1, 6))

A clearer version names the dice and numbers the rolls with an f-string:

وەشانێکی ڕوونتر ناو لە زارەکان دەنێت و هەڵدانەکان بە ژمارە دەکات:

dice.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
import random

random.seed(2026)  # remove this line to get new rolls every time
for roll in range(1, 11):
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    print(f"Roll {roll}: {die1} and {die2}")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

range(1, 11) numbers the rolls 1 to 10.

range(1, 11) هەڵدانەکان لە 1 تا ⁦10⁩ ژمارە دەکات.

Lineدێڕی 5-6

randint(1, 6) includes 6, like a real die.

randint(1, 6) 6یش دەگرێتەوە، وەک زارێکی ڕاستەقینە.

Quick checkپشکنینی خێراWhich expression can give the number 6?کام دەتوانێت ژمارە 6 بدات؟

for or while?for یان while؟

Both loops can do the same job (Exercise 1 showed one program written both ways), but each fits some tasks better.

هەردوو دەتوانن هەمان کار بکەن (ڕاهێنانی 1 یەک بە هەردوو شێوە پیشان دا)، بەڵام هەر یەکێکیان بۆ هەندێک ئەرک باشترە.

Choosing a loopهەڵبژاردنی
SituationدۆخBetter choiceهەڵبژاردەی باشترWhyبۆچی
Repeat 10 times; count from 1 to 100⁦10⁩ جار دووبارە بکەرەوە؛ لە 1 تا ⁦100⁩ بژمێرەfor … in range(…)start, test and step in one line; no forgotten += 1سەرەتا، پشکنین و هەنگاو لە یەک دێڕدا؛ += 1ی لەبیرکراو نییە
Go through every letter or every list itemبە هەموو پیتێک یان هەموو دانەیەکی بڕۆfor … in sequencethe sequence decides the number of rounds ژمارەی دیاری دەکات
Until the user types 0; until the input is validتا 0 دەنووسێت؛ تا دروست دەبێتwhilethe number of rounds is not known in advanceژمارەی پێشتر نازانرێت
Until a value reaches a limit (digits, doubling)تا نرخێک دەگاتە سنوورێک (، دووقاتکردنەوە)whilethe stop depends on a calculationوەستان بەندە بە ژمێرکارییەک
Quick checkپشکنینی خێرا"Print each word of a sentence on its own line." Which loop is simplest?«هەر وشەیەکی ڕستەیەک لە دێڕێکی جیادا چاپ بکە.» کام سادەترە؟

for Loop vs. while Loop in PythonNeso Academy · Englishئینگلیزی · 87K viewsبینین · 1.4K likesلایک

When to choose a for loop (known number of rounds) and when a while loop (repeat until a condition changes).

کەی هەڵبژێریت (ژمارەی زانراوە) و کەی (دووبارەکردنەوە تا دەگۆڕێت).

Whole video (8:45). Chapters: 0:37 why loops, 2:39 number of rounds, 5:30 infinite loops.

هەموو ڤیدیۆکە ⁦(8:45)⁩. بەشەکان: ⁦0:37⁩ بۆچی ، ⁦2:39⁩ ژمارەی ، ⁦5:30⁩ .

Common mistakesهەڵە باوەکان

for-loop mistakesهەڵەکانی
MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
for n in range(1, 10):Prints 1 to 9, not 1 to 10 (off-by-one)1 تا 9 چاپ دەکات نەک 1 تا ⁦10⁩ ()range(1, 11)
range(2.5)TypeError: range needs integersTypeError: range پێویستی بە هەیەrange(int(x)) / range(2)
range(1, 10, 0)ValueError: the step may not be 0ValueError: هەنگاو نابێت 0 بێتa step of 1 or moreهەنگاوی 1 یان زیاتر
for i in range(5)SyntaxError: missing colonSyntaxError: دوو خاڵ نییەfor i in range(5):
print(“Hi”)SyntaxError: curly quotesSyntaxError: چەماوەprint("Hi")
sum = 0Hides the built-in sum()sum()ی ئامادە دەشارێتەوەtotal = 0
Changing the loop variable inside the bodyگۆڕینی لە ناو No effect on the next round: for gives the next value anywayهیچ کاریگەرییەکی لەسەر دواتر نییە: for بە هەر حاڵ نرخی دواتر دەداتUse a while loop if you need to control the counterئەگەر پێویستت بە کۆنترۆڵکردنی هەیە بەکار بهێنە
random.randint(1, 6) vs range(1, 6)randint includes 6; range does notrandint 6 دەگرێتەوە؛ range ناRemember: randint = both endsلەبیرت بێت: randint = هەردوو سەر
Changing the loop variable does not change the next roundگۆڕینی گۆڕاوی خول خولانەوەی دواتر ناگۆڕێتloop_variable.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for i in range(3):
    print(i)
    i = 100
print("after the loop:", i)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The next round overwrites i with the next value from range(3).

دواتر i بە نرخی دواتری range(3) دەگۆڕێت.

Lineدێڕی 4

After the loop i keeps the last value it was given (here 100). The loop variable still exists after the loop in Python.

دوای i دوایین نرخی خۆی دەهێڵێتەوە (لێرەدا ⁦100⁩). لە دوای هەر دەمێنێت.

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

ExerciseڕاهێنانMultiples of 7چەندجارەکانی 7

Print 7, 14, 21, …, 70 on one line using only range() (no if).

تەنها بە range() (بەبێ if) 7، ⁦14⁩، ⁦21⁩، …، ⁦70⁩ لە یەک دێڕدا چاپ بکە.

Hintئاماژە

Use the step.

هەنگاوەکە بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
sevens.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(7, 71, 7):
    print(n, end=" ")
print()
ExerciseڕاهێنانCountdownژماردنی پێچەوانە

Print 10 9 8 … 1 and then Go! on the same line.

⁦10 9 8⁩ … 1 و پاشان Go! لە هەمان دێڕدا چاپ بکە.

Show answerوەڵامەکە پیشان بدە
countdown_for.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(10, 0, -1):
    print(n, end=" ")
print("Go!")
ExerciseڕاهێنانSum with forکۆکردنەوە بە for

Add up the numbers 1 to 100 with a for loop and an accumulator. Check your answer against the while version in Counters and accumulators.

ژمارەکانی 1 تا ⁦100⁩ بە و کۆ بکەرەوە. وەڵامەکەت لەگەڵ وەشانی while لە ژمێرەرەکان و گۆڕاوەکانی کۆکردنەوە بەراورد بکە.

Show answerوەڵامەکە پیشان بدە
sum_for.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
for n in range(1, 101):
    total += n
print(total)
ExerciseڕاهێنانCount the vowelsپیتە بزوێنەکان بژمێرە

Ask for a word and count its vowels (a, e, i, o, u), capital or small.

داوای وشەیەک بکە و پیتە بزوێنەکانی (a، e، i، o، u) بژمێرە، گەورە بن یان بچووک.

Hintئاماژە

letter in "aeiou" tests one letter; word.lower() handles capitals.

letter in "aeiou" یەک پیت دەپشکنێت؛ word.lower() پیتە گەورەکان چارەسەر دەکات.

Show answerوەڵامەکە پیشان بدە
vowels.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = input("A word: ")
vowels = 0
for letter in word.lower():
    if letter in "aeiou":
        vowels += 1
print("Vowels:", vowels)
ExerciseڕاهێنانReverse a word with a loopوشەیەک بە خول هەڵبگەڕێنەوە

Without slicing, build the reversed word with a for loop: "Erbil" → "librE".

بەبێ ، بە وشە هەڵگەڕاوەکە دروست بکە: "Erbil" ← "librE".

Hintئاماژە

Start with result = "" and put each letter in front: result = letter + result.

بە result = "" دەست پێ بکە و هەر پیتێک بخەرە پێشەوە: result = letter + result.

Show answerوەڵامەکە پیشان بدە
reverse_loop.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "Erbil"
result = ""
for letter in word:
    result = letter + result
print(result)

result is a string accumulator: it starts empty and grows by one letter each round. (With slicing, word[::-1] does the same in one step.)

result : بەتاڵ دەست پێدەکات و هەر یەک پیت گەورەتر دەبێت. (بە ، word[::-1] هەمان کار بە یەک هەنگاو دەکات.)

ExerciseڕاهێنانIndex and letterئیندێکس و پیت

Print every letter of "LANA" with its index: 0 L, 1 A, …

هەموو پیتێکی "LANA" لەگەڵ چاپ بکە: 0 L، 1 A، …

Show answerوەڵامەکە پیشان بدە
index_letter.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = "LANA"
for i in range(len(word)):
    print(i, word[i])
for i, letter in enumerate(word):
    print(i, letter)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

range(len(word)) gives the indexes 0 to 3.

range(len(word)) 0 تا 3 دەدات.

Lineدێڕی 4-5

Extra: enumerate() gives index and item together — the usual Python way.

زیادە: enumerate() و دانە پێکەوە دەدات — ڕێگە ئاساییەکەی .

More exercises: text and number seriesڕاهێنانی زیاتر: دەق و زنجیرەی ژمارە

These exercises use a for loop with an accumulator: a string that grows letter by letter, counters, or a running total.

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

ExerciseڕاهێنانHide the vowelsپیتە بزوێنەکان بشارەوە

Ask for a text and print it with every vowel (a, e, i, o, u, capital or small) replaced by *. Strings are immutable, so build a new string.

داوای دەقێک بکە و چاپی بکە بەڵام هەموو پیتێکی بزوێن (a، e، i، o، u، گەورە یان بچووک) بکە بە *. ، بۆیە نوێ دروست بکە.

Hintئاماژە

result += "*" or result += letter in every round.

لە هەموو result += "*" یان result += letter.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
text = input("Text: ")
result = ""
for letter in text:
    if letter.lower() in "aeiou":
        result += "*"
    else:
        result += letter
print(result)
print(text)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

A string accumulator starts empty.

بەتاڵ دەست پێدەکات.

Lineدێڕی 9

The original text is unchanged.

دەقە ڕەسەنەکە نەگۆڕاوە.

ExerciseڕاهێنانText statisticsئاماری دەق

Ask for a text. Count its letters, digits, spaces and other characters in one loop, and print the number of characters and words too.

داوای دەقێک بکە. لە یەک پیتەکان، ، و تری بژمێرە و ژمارەی هەموو و وشەکانیش چاپ بکە.

Hintئاماژە

Use isalpha(), isdigit() and isspace() from the is…() methods.

isalpha()، isdigit() و isspace() لە میتۆدەکانی is…() بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
text = input("Text: ")
letters = 0
digits = 0
spaces = 0
others = 0
for ch in text:
    if ch.isalpha():
        letters += 1
    elif ch.isdigit():
        digits += 1
    elif ch.isspace():
        spaces += 1
    else:
        others += 1
print("Characters:", len(text))
print("Letters:", letters, "Digits:", digits, "Spaces:", spaces, "Others:", others)
print("Words:", len(text.split()))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 7-14

Each character goes into exactly one group: elif stops at the first test that is true.

هەر تەنها دەچێتە یەک گرووپەوە: elif لە یەکەم پشکنینی ڕاستدا دەوەستێت.

Lineدێڕی 17

split() with no argument gives the words.

split() بەبێ وشەکان دەدات.

ExerciseڕاهێنانSum of the digits in a textکۆی تاکژمارەکانی ناو دەقێک

Ask for a text such as Room 12, floor 3 and add up every digit in it (1+2+3=61 + 2 + 3 = 6).

داوای دەقێکی وەک Room 12, floor 3 بکە و هەموو ناوی کۆ بکەرەوە (1+2+3=61 + 2 + 3 = 6).

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
text = input("Text: ")
total = 0
for ch in text:
    if ch.isdigit():
        total += int(ch)
print("Sum of the digits:", total)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

ch is a one-character string such as "2": int() turns it into the number 2.

ch یەک وەک "2": int() دەیکات بە ژمارە 2.

The 12 counts as 1 and 2, because the loop sees one character at a time. Compare the number version with % 10 in The while loop.

⁦12⁩ وەک 1 و 2 دەژمێردرێت، چونکە هەر جارێک یەک دەبینێت. بەراوردی بکە لەگەڵ وەشانی ژمارەیی بە % 10 لە خولی while.

ExerciseڕاهێنانAlternating signsسالب و موجەب بە نۆرە

Print the first n numbers of the sequence −5, 10, −15, 20, …-5,\ 10,\ -15,\ 20,\ \dots on one line: the size grows by 5 and the sign flips every time.

یەکەم n ژمارەی زنجیرەی −5, 10, −15, 20, …-5,\ 10,\ -15,\ 20,\ \dots لە یەک دێڕدا چاپ بکە: قەبارەی ژمارەکان هەر جارێک 5 زیاد دەکات و نیشانەکەیان هەموو جارێک دەگۆڕێت.

Hintئاماژە

Test i % 2, or multiply by (-1) ** i.

i % 2 بپشکنە، یان لێکی بدە لە (-1) ** i.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("How many terms? "))
for i in range(1, n + 1):
    term = 5 * i
    if i % 2 == 1:
        term = -term
    print(term, end=" ")
print()
for i in range(1, n + 1):
    print((-1) ** i * 5 * i, end=" ")
print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-5

Odd positions (1, 3, 5, …) are made negative.

شوێنە تاکەکان (⁦1⁩، 3، 5، …) دەکرێن بە سالب.

Lineدێڕی 9

The same with a power: (−1)i(-1)^i is −1-1 for odd i and 1 for even i.

هەمان شت بە : (−1)i(-1)^i بۆ iی تاک −1-1ە و بۆ iی جووت 1.

ExerciseڕاهێنانA series sumکۆی زنجیرەیەک

Ask for n and add up 1+18+127+⋯+1n31 + \frac{1}{8} + \frac{1}{27} + \cdots + \frac{1}{n^3}, that is ∑k=1n1k3\sum_{k=1}^{n} \frac{1}{k^3}. Print the sum, and the sum with round(total, 4) (4 digits after the decimal point).

داوای n بکە و 1+18+127+⋯+1n31 + \frac{1}{8} + \frac{1}{27} + \cdots + \frac{1}{n^3} کۆ بکەرەوە، واتە ∑k=1n1k3\sum_{k=1}^{n} \frac{1}{k^3}. کۆکە و کۆکە بە round(total, 4) چاپ بکە (⁦4⁩ دوای ).

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("n: "))
total = 0.0
for k in range(1, n + 1):
    total += 1 / k ** 3
print("Sum:", total)
print("Rounded:", round(total, 4))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

A float accumulator: the terms are fractions.

: ژمارەکانی زنجیرەکە کەرتن.

Lineدێڕی 4

** comes before /, so this is 1k3\frac{1}{k^3}.

** پێش / دێت، بۆیە ئەمە 1k3\frac{1}{k^3}ـە.

Check by hand: 1+0.125+0.0370…+0.015625=1.1776…1 + 0.125 + 0.0370\ldots + 0.015625 = 1.1776\ldots. With n = 1000 the sum is about 1.20206: it grows more and more slowly. A loop that adds terms is exactly a sigma — see A loop is a sigma.

بە دەست بیپشکنە: 1+0.125+0.0370…+0.015625=1.1776…1 + 0.125 + 0.0370\ldots + 0.015625 = 1.1776\ldots. بە n=1000n = 1000 کۆکە نزیکەی ⁦1.20206⁩ـە: هەتا دێت هێواشتر گەورە دەبێت. کە ژمارەکانی زنجیرەیەک کۆ دەکاتەوە ڕێک سیگمایەکە — بڕوانە خولێک سیگمایەکە.

Summaryپوختە

  • for var in sequence: runs the body once for each item; strings, lists, tuples and ranges are sequences.
  • A while loop with an index (i < len(s)) can walk a string too; accumulators build new strings, counts and series sums.
  • range(start, stop, step): start defaults to 0, step to 1, and stop is never included — the integers of [start, stop)[\mathit{start},\ \mathit{stop}).
  • Arguments must be integers; a negative step counts down; step 0 is a ValueError; a range is not a list.
  • After for count in range(5), count is 4; after the equivalent while, it is 5 — check counters when converting loops.
  • Use straight quotes, int(input()) instead of eval(input()), and total instead of sum.
  • import random and random.randint(a, b) give random numbers with both ends included.
  • for var in sequence: بۆ هەر دانەیەک جارێک جێبەجێ دەکات؛ ، ، و range .
  • بە (i < len(s)) دەتوانێت بە بڕوات؛ نوێ، ژمارەکان و کۆی زنجیرەکان دروست دەکەن.
  • range(start, stop, step): start بە ئاسایی 0ـە، ⁦step 1⁩ و stop هەرگیز تێدا نییە — [start, stop)[\mathit{start},\ \mathit{stop}).
  • دەبێت بن؛ هەنگاوی سالب بەرەو خوار دەژمێرێت؛ هەنگاوی 0 ValueErrorـە؛ range نییە.
  • دوای for count in range(5)، count 4ـە؛ دوای هەمان بە while، 5ـە — کاتی گۆڕینی بپشکنە.
  • ڕاست، int(input()) لەبری eval(input()) و total لەبری sum بەکار بهێنە.
  • import random و random.randint(a, b) دەدەن کە هەردوو سەریان تێدایە.
Review questionsپرسیارەکانی پێداچوونەوە
What does range(3, 8) give?range(3, 8) چی دەدات؟

3, 4, 5, 6, 7.

3، 4، 5، 6، 7.

How do you count down from 10 to 1?چۆن لە ⁦10⁩ەوە بەرەو خوار بۆ 1 دەژمێریت؟

for n in range(10, 0, -1):

for n in range(10, 0, -1):

Why does print(“Hi”) fail?بۆچی print(“Hi”) سەرناکەوێت؟

Curly quotes are not Python quotes: it is a syntax error.

چەماوە نین: .

Can random.randint(1, 6) return 6?ئایا random.randint(1, 6) دەتوانێت 6 بگەڕێنێتەوە؟

Yes. Both ends are included.

بەڵێ. هەردوو سەر تێدان.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Learn Python for loops in 5 minutes! 🔁Bro Code · Englishئینگلیزی · 1.1M viewsبینین · 21K likesلایک · Partبەشی 0:00–3:26 (ofلە 5:05)

for loops with range(): counting forwards and backwards, using a step, and looping over the characters of a string.

بە range(): ژماردن بەرەو پێشەوە و بەرەو دواوە، بەکارهێنانی هەنگاو و تێپەڕبوون بەسەر .

Plays 0:00-3:26: range() forwards, backwards, with a step, and a loop over a string; stops before continue and break (next lesson).

لە ⁦0:00⁩ تا ⁦3:26⁩ پەخش دەبێت: range() بەرەو پێشەوە، بەرەو دواوە، بە هەنگاو و بەسەر ؛ پێش continue و break ڕادەوەستێت (وانەی داهاتوو).

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

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

Controlling Loops: break and continueکۆنترۆڵکردنی خولەکان: break و continue

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

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