PythonPython · Chapterبەش 5 · Lectureوانە 5.1

Making Decisions: if, elif, elseبڕیاردان: if، elif و else

Let a program choose: True and False, the six comparison operators, if, if … else and if … elif … else, blocks made by indentation, why the order of the tests matters, and the classic mistakes — with the grade example from the course's Python-and-Java report, cleaned up.

با هەڵبژێرێت: True و False، شەش ، if، if … else و if … elif … else، بلۆک کە بە دروست دەبێت، بۆچی ڕیزبەندیی تاقیکردنەوەکان گرنگە و هەڵە کلاسیکییەکان — لەگەڵ نموونەی نمرە لە ڕاپۆرتی Python و Javaـی کۆرسەکە، پاککراوە.

17 min read16 خولەک خوێندنەوە · 21 stepsهەنگاو

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

  • Write conditions with the six comparison operators and predict their True/False value.
  • Use if, if … else and if … elif … else with correctly indented blocks.
  • Order elif tests so that every case is reachable, and choose between elif and separate ifs.
  • Recognise and fix = vs ==, a missing colon, bad indentation and text-versus-number comparisons.
  • بە شەش بنووسیت و True/Falseـیان پێشبینی بکەیت.
  • if، if … else و if … elif … else لەگەڵ بلۆکی دروست چووەتە ناوەوە بەکار بهێنیت.
  • تاقیکردنەوەکانی elif بە شێوەیەک ڕیز بکەیت کە هەموو حاڵەتێک بگەیەنرێت و لە نێوان elif و ifـی جیادا هەڵبژێریت.
  • = بەرامبەر ==، دوو خاڵی ون، هەڵە و بەراوردی دەق لەگەڵ ژمارە بناسیتەوە و چاکیان بکەیت.

Programs that make decisionsبەرنامەکان کە بڕیار دەدەن

So far every program ran all its lines, from top to bottom, every time. Real programs must decide: some code should run only when a condition is met. A shop gives a discount only to students; a game says “You win!” only when the score is high enough.

تا ئێستا هەموو هەموو دێڕەکانی، لە سەرەوە بۆ خوارەوە، هەموو جارێک جێبەجێ دەکرد. ڕاستەقینە دەبێت بڕیار بدەن: هەندێک کۆد تەنها کاتێک جێبەجێ دەبێت کە هاتبێتە دی. دوکانێک تەنها داشکاندن بە قوتابیان دەدات؛ یارییەک تەنها کاتێک دەڵێت «بردتەوە!» کە خاڵەکان بەس بن.

A condition is a question with only two possible answers: true or false. The program does one thing when the answer is true and, if you want, something else when it is false. In Python these decisions are written with if, elif and else.

پرسیارێکە تەنها دوو وەڵامی دەکرێت: ڕاست یان هەڵە. کاتێک وەڵامەکە ڕاست بێت کارێک دەکات و، ئەگەر بتەوێت، کاتێک هەڵە بێت کارێکی تر. لە Pythonـدا ئەم بڕیارانە بە if، elif و else دەنووسرێن.

Definitionپێناسە

A statement that runs different code depending on a . Most languages have if, if…else and an else-if chain; many also have switch (Python 3.10 and later has match).

کە بەپێی کۆدی جیاواز جێبەجێ دەکات. زۆربەی زمانەکان if، if…else و زنجیرەیەکی ⁦else-if⁩ـیان هەیە؛ زۆریان switchـیشیان هەیە (Python لە وەشانی ⁦3.10⁩ـەوە matchـی هەیە).

Quick checkپشکنینی خێراHow many possible answers does a condition have? چەند وەڵامی دەکرێت هەیە؟

Programs that make decisionsبەرنامەکان کە بڕیار دەدەن

Two roads, one choiceدوو ڕێگا، یەک هەڵبژاردن

A taxi at a junctionتەکسییەک لە چوارڕێیانێکدا

A taxi driver reaches a junction. If the road to the bazaar is open, he drives straight on; otherwise he turns right. He takes one road, never both, and after the junction the journey continues. An if … else works the same way: the program checks the condition, takes one branch, and then carries on with the next line after the decision.

شۆفێرێکی تەکسی دەگاتە چوارڕێیانێک. ئەگەر ڕێگای بازاڕ کراوە بێت ڕاست دەڕوات؛ ئەگەرنا بەلای ڕاستدا دەسووڕێتەوە. یەک ڕێگا دەگرێت، هەرگیز هەردووکیان نا و دوای چوارڕێیانەکە گەشتەکە بەردەوام دەبێت. if … else بە هەمان شێوە کار دەکات: دەپشکنێت، یەک لق دەگرێت و پاشان لەگەڵ دێڕی دوای بڕیارەکە بەردەوام دەبێت.

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

A driver can change his mind halfway. A program cannot: once the condition is checked, the branch is fixed until the program reaches that if again.

شۆفێر دەتوانێت لە نیوەی ڕێگادا بیر بگۆڕێت. ناتوانێت: کاتێک پشکنرا، لقەکە جێگیرە تا دووبارە دەگاتەوە ئەو ifـە.

On paper, a decision is the diamond of a flowchart (Algorithms, pseudocode and flowcharts). In this lesson you turn those diamonds into Python.

لەسەر کاغەز، بڕیار ئەڵماسییەکەی (ئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت). لەم وانەیەدا ئەو ئەڵماسییانە دەکەیت بە Python.

Quick checkپشکنینی خێراIn a flowchart, which shape stands for a decision such as if?لە ، کام شێوە بڕیارێکی وەک if دەنوێنێت؟

True and FalseTrue و False

Python has a data type for the two answers: bool (Boolean), with exactly two values, True and False. Write them with a capital letter and no quotes. Every condition is an expression whose value is True or False, and you can store that value in a variable like any other.

Python هەیە بۆ دوو وەڵامەکە: bool ()، بە ڕێک دوو ، True و False. بە پیتی گەورە و بەبێ بیاننووسە. هەموو کە True یان Falseـە و دەتوانیت ئەو وەک هەر تر لە هەڵبگریت.

Conditions are values tooمەرجەکانیش نرخنbool.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(5 > 3)
print(5 == 3)
print(type(True))
is_big = 10 > 5
print(is_big)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The type of True is bool.

جۆری True بریتییە لە bool.

Lineدێڕی 4-5

10 > 5 is worked out first; is_big stores the answer True.

سەرەتا 10 > 5 هەژمار دەکرێت؛ is_big وەڵامی True هەڵدەگرێت.

Quick checkپشکنینی خێراWhich of these is a bool value in Python?کامیان boolـە لە Pythonـدا؟

Asking questions: comparison operatorsپرسیار کردن: کارپێکەرەکانی بەراوردکردن

Most conditions compare two values. Python has six comparison operators, and each one gives True or False:

زۆربەی دوو بەراورد دەکەن. لە Pythonـدا شەش هەیە و هەر یەکەیان True یان False دەدات:

The six comparison operatorsشەش
OperatorQuestionپرسیارExample with a = 7نموونە بە a = 7Resultئەنجام
==equal to?یەکسانە بە؟a == 7True
!=not equal to?یەکسان نییە بە؟a != 7False
<less than?کەمترە لە؟a < 10True
>greater than?زیاترە لە؟a > 10False
<=less than or equal to?کەمترە یان یەکسانە بە؟a <= 7True
>=greater than or equal to?زیاترە یان یەکسانە بە؟a >= 8False
Comparisons give True or Falseبەراوردکردن True یان False دەداتcompare.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = 7
print(a == 7)
print(a != 7)
print(a < 10)
print(a > 10)
print(a <= 7)
print(a >= 8)
print("apple" == "Apple")
print("5" == 5)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 7

Strings are compared letter by letter, and capital letters are different from small ones: "apple" is not "Apple".

پیت بە پیت بەراورد دەکرێن و پیتی گەورە جیاوازە لە بچووک: "apple" یەکسانی "Apple" نییە.

Lineدێڕی 8

The text "5" is never equal to the number 5. Remember this when you compare user input.

دەقی "5" هەرگیز یەکسانی ژمارەی 5 نییە. ئەمە لە بیر بێت کاتێک بەراورد دەکەیت.

Strings can also be compared with < and >: Python uses alphabetical order by character codes. That is explained in Working with strings.

دەتوانرێن بە < و > بەراورد بکرێن: Python ڕیزبەندیی ئەلفوبێیی بەپێی کۆدی بەکار دەهێنێت. ئەمە لە کارکردن لەگەڵ زنجیرەنووسەکان ڕوون کراوەتەوە.

Quick checkپشکنینی خێراage = 18. What is age >= 18?age = 18. age >= 18 چییە؟

Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif StatementsCorey Schafer · Englishئینگلیزی · 842K viewsبینین · 20K likesلایک · Partبەشی 1:05–2:54 (ofلە 16:28)

The comparison operators ==, !=, <, >, <= and >= in Python, and which values Python treats as False in a condition.

==، !=، <، >، <= و >= لە ، هەروەها کام لە بە False دادەنرێت.

Plays 1:05-2:54: the six comparison operators, each giving True or False.

لە ⁦1:05⁩ تا ⁦2:54⁩ پەخش دەبێت: هەر شەش ، کە هەر یەکێکیان True یان False دەداتەوە.

The if statementڕستەی if

An if statement runs a block of code only when its condition is True. When the condition is False, Python skips the block and continues with the first line after it.

تەنها کاتێک جێبەجێ دەکات کە True بێت. کاتێک False بێت، Python بلۆکەکە بەجێ دەهێڵێت و لەگەڵ یەکەم دێڕی دوای ئەو بەردەوام دەبێت.

A heat warningئاگادارکردنەوەی گەرماif.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
temperature = int(input("Temperature today: "))
if temperature > 30:
    print("It is hot.")
    print("Drink plenty of water.")
print("Have a nice day!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

if, then the condition, then a colon :.

if، پاشان ، پاشان دوو خاڵ :.

Lineدێڕی 3-4

The block: every line indented by 4 spaces belongs to the if. Both lines run, or neither does.

بلۆکەکە: هەموو دێڕێک کە بە 4 بۆشایی چووەتە ناوەوە سەر بە ifـە. یان هەردوو دێڕەکە جێبەجێ دەبن یان هیچیان.

Lineدێڕی 5

Not indented, so it is outside the if: it runs every time.

بۆشایی سەرەتای نییە، بۆیە لە دەرەوەی ifـە: هەموو جارێک جێبەجێ دەبێت.

  1. if — a keyword: it starts the decision.
  2. the condition — any expression that gives True or False. No brackets are needed around it.
  3. : — the colon ends the header line. Forgetting it is a syntax error.
  4. the block — one or more lines, all indented by the same amount (4 spaces is the standard).
  1. if — : بڕیارەکە دەست پێ دەکات.
  2. — هەر کە True یان False بدات. پێویست بە کەوانە نییە لە دەوری.
  3. : — دوو خاڵەکە دێڕی سەرەتا تەواو دەکات. لەبیرکردنی .
  4. بلۆکەکە — یەک یان چەند دێڕێک، هەموویان بە هەمان ئەندازە چوونەتە ناوەوە (⁦4⁩ بۆشایی ستانداردە).
Quick checkپشکنینی خێراtemperature is 25. Which lines of the heat-warning program run?temperature بریتییە لە ⁦25⁩. کام دێڕەکانی ئاگادارکردنەوەی گەرما جێبەجێ دەبن؟

14- ئه گه ره كان له پایثۆندا If Statements In PythonMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 7.5K viewsبینین · 267 likesلایک

In Kurdish: how an if statement works in Python, with elif and else.

بە کوردی: لە چۆن کار دەکات، لەگەڵ elif و else.

Kurdish (Sorani), whole video (9:27): part 14 of MR. Ant's Python course.

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

The if statementڕستەی if

Blocks are made by indentationبلۆک بە بۆشایی سەرەتای دێڕ دروست دەبێت

Many languages put a block between curly braces { }. Python uses indentation instead: the spaces at the start of a line are part of the syntax. PEP 8, Python's style guide, asks for 4 spaces per level; editors such as IDLE and VS Code indent the next line for you when you press Enter after a colon, and the Tab key inserts 4 spaces.

زۆر زمان بلۆک دەخەنە نێوان کەوانەی { }. Python لەبری ئەوە بەکار دەهێنێت: بۆشاییەکانی سەرەتای دێڕ بەشێکن لە . ⁦PEP 8⁩، ڕێبەری شێوازی Python، بۆ هەر ئاستێک 4 بۆشایی داوا دەکات؛ دەستکاریکەرەکانی وەک IDLE و ⁦VS Code⁩ کاتێک دوای دوو خاڵ Enter دادەگریت خۆیان دێڕی دواتر دەبەنە ناوەوە و Tab چوار دادەنێت.

No indentation after the colonهیچ بۆشایی سەرەتای دێڕ نییە دوای دوو خاڵەکەindent_error.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
temperature = 35
if temperature > 30:
print("It is hot.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The block of an if must be indented. Python names the problem and the line of the if.

بلۆکی if دەبێت بچێتە ناوەوە. Python کێشەکە و دێڕی ifـەکە ناو دەبات.

Indentation that changes inside a blockبۆشایی سەرەتای دێڕ کە لەناو بلۆکدا دەگۆڕێتindent_mixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
temperature = 35
if temperature > 30:
    print("It is hot.")
      print("Drink plenty of water.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Two extra spaces: Python does not know which block this line belongs to.

دوو بۆشایی زیادە: Python نازانێت ئەم دێڕە سەر بە کام بلۆکە.

Quick checkپشکنینی خێراHow does Python know which lines belong to an if?Python چۆن دەزانێت کام دێڕانە سەر بە ifـن؟

Two paths: if … elseدوو ڕێگا: if … else

Often you want one thing when the condition is True and something else when it is False. Add else: with its own block. Exactly one of the two blocks runs, every time.

زۆرجار دەتەوێت کاتێک True بێت کارێک بکرێت و کاتێک False بێت کارێکی تر. else: لەگەڵ بلۆکی خۆی زیاد بکە. هەموو جارێک ڕێک یەکێک لە دوو بلۆکەکە جێبەجێ دەبێت.

Flowchart: Start, then an input box n = int(input()), then a decision diamond asking n % 2 == 0. The True arrow leads to print "even", the False arrow leads to print "odd". Both arrows meet again before End.هێڵکاریی ڕەوت: دەستپێک، پاشان چوارگۆشەی نێوخستە n = int(input())، پاشان ئەڵماسیی بڕیار کە دەپرسێت n % 2 == 0. تیری True دەچێت بۆ چاپکردنی "even" و تیری False بۆ چاپکردنی "odd". هەردوو تیرەکە پێش کۆتایی دووبارە یەک دەگرنەوە.
An if … else is a diamond with two exits that meet again.if … else ئەڵماسییەکە بە دوو دەرچە کە دووبارە یەک دەگرنەوە.CêIT
Even or odd?جووت یان تاک؟even_odd.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("Enter a whole number: "))
if n % 2 == 0:
    print(n, "is even.")
else:
    print(n, "is odd.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

n % 2 is the remainder after dividing by 2 (Operators); 0 means even.

n % 2 دوای دابەشکردن بەسەر 2دا (کارپێکەرەکان)؛ 0 واتە جووت.

Lineدێڕی 4

else has no condition: it simply catches every other case.

else هیچ نییە: تەنها هەموو حاڵەتەکانی تر دەگرێت.

Old enough to vote?تەمەنت بەسە بۆ دەنگدان؟vote.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = int(input("Your age: "))
if age >= 18:
    print("You can vote.")
else:
    print("You can vote in", 18 - age, "years.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

The else block can calculate too: 18−15=318 - 15 = 3 years to wait.

بلۆکی elseـیش دەتوانێت هەژمار بکات: 18−15=318 - 15 = 3 ساڵ چاوەڕوانی.

Quick checkپشکنینی خێراIn an if … else, how many of the two blocks run?لە if … elseـدا، چەند لە دوو بلۆکەکە جێبەجێ دەبن؟

More than two paths: elifزیاتر لە دوو ڕێگا: elif

When there are more than two cases, add elif (short for else if) between if and else. Python tests the conditions from top to bottom and runs the block of the first one that is True; the rest are skipped. If none is True, the else block runs (and else is optional).

کاتێک زیاتر لە دوو حاڵەت هەیە، elif (کورتکراوەی ⁦else if⁩) لە نێوان if و else زیاد بکە. Python لە سەرەوە بۆ خوارەوە تاقی دەکاتەوە و بلۆکی یەکەم کە True بێت جێبەجێ دەکات؛ ئەوانی تر بەجێ دەهێڵرێن. ئەگەر هیچیان True نەبوو، بلۆکی else جێبەجێ دەبێت (else ئارەزوومەندانەیە).

Definitionپێناسە

A part between if and else that tests another condition when the ones before it were false. PHP writes elseif, Java and C# else if, Python elif; there can be many, and only the first true one runs.

بەشێکە لە نێوان if و else کە کاتێک مەرجەکانی پێشووی هەڵە بوون مەرجێکی تر دەپشکنێت. PHP دەنووسێت elseif، Java و ⁦C#⁩ else if و Python elif؛ دەکرێت زۆریان هەبێت و تەنها یەکەمین ڕاستیان جێبەجێ دەبێت.

Here is the grade example from the course's report Python and Java, exactly as written there, with its output:

ئەمە نموونەی نمرەیە لە ڕاپۆرتی ⁦Python and Java⁩ی کۆرسەکە، ڕێک وەک لەوێدا نووسراوە، لەگەڵ :

The report's versionوەشانی ڕاپۆرتەکەPYTHONType in a terminalلە تێرمیناڵدا بینووسە
num = 80;
if (num >= 80):
  print("Excelent!")
elif (num >= 50):
  print("Good.")
else:
  print("Failed!")
Quick checkپشکنینی خێراThe report's program prints the right answer. Why does this course still rewrite it? ڕاپۆرتەکە وەڵامی ڕاست چاپ دەکات. بۆچی ئەم کۆرسە هێشتا دووبارە دەینووسێتەوە؟

More than two paths: elifزیاتر لە دوو ڕێگا: elif

The grade decision, step by stepبڕیاری نمرە، هەنگاو بە هەنگاو

The grade decision, cleaned upبڕیاری نمرە، پاککراوەgrade.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
num = int(input("Mark: "))
if num >= 80:
    print("Excellent!")
elif num >= 50:
    print("Good.")
else:
    print("Failed!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

80 or more: the first condition is True, so “Excellent!” — and Python skips the rest.

⁦80⁩ یان زیاتر: یەکەم Trueـە، بۆیە «⁦Excellent!⁩» — و Python ئەوانی تر بەجێ دەهێڵێت.

Lineدێڕی 4

Reached only when the mark is below 80. So this branch means 50 to 79.

تەنها کاتێک دەگاتە ئێرە کە نمرەکە لە ⁦80⁩ کەمتر بێت. بۆیە ئەم لقە واتە ⁦50⁩ بۆ ⁦79⁩.

Lineدێڕی 6

Everything else: below 50.

هەموو ئەوانی تر: کەمتر لە ⁦50⁩.

Animation of the grade flowchart. For num = 80 the first diamond, num >= 80, answers True and the path goes to Excellent!. For num = 65 the first diamond answers False, the second, num >= 50, answers True and the path goes to Good.. For num = 30 both diamonds answer False and the path goes to Failed!.
Animation of the grade flowchart. For num = 80 the first diamond, num >= 80, answers True and the path goes to Excellent!. For num = 65 the first diamond answers False, the second, num >= 50, answers True and the path goes to Good.. For num = 30 both diamonds answer False and the path goes to Failed!.
One mark, one path: the first True condition decides.یەک نمرە، یەک ڕێگا: یەکەم True بڕیار دەدات.CêIT
Quick checkپشکنینی خێراWith the clean grade program, what is printed for the mark 50?بە پاککراوەکەی نمرە، بۆ نمرەی ⁦50⁩ چی چاپ دەکرێت؟

More than two paths: elifزیاتر لە دوو ڕێگا: elif

The order of the conditions mattersڕیزبەندیی مەرجەکان گرنگە

Because only the first True branch runs, put the most demanding condition first. Here the two tests are swapped:

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

Swapped conditionsمەرجە شوێنگۆڕاوەکانwrong_order.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
num = 80
if num >= 50:
    print("Good.")
elif num >= 80:
    print("Excellent!")
else:
    print("Failed!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

80 >= 50 is True, so this branch wins and “Excellent!” can never be printed: the elif below it is unreachable for every mark of 50 or more.

80 >= 50 Trueـە، بۆیە ئەم لقە دەباتەوە و «⁦Excellent!⁩» هەرگیز چاپ ناکرێت: elifـی خوارەوە بۆ هەموو نمرەیەکی ⁦50⁩ یان زیاتر ناگەیەنرێت.

This is a logic error: no message, just a wrong answer. With >= tests, go from the highest limit down to the lowest (or from the lowest < limit up).

ئەمە : هیچ پەیامێک نییە، تەنها وەڵامێکی هەڵە. لەگەڵ تاقیکردنەوەی >=، لە بەرزترین سنوورەوە بۆ نزمترین بڕۆ (یان لە نزمترین سنووری <ـەوە بەرەو سەرەوە).

Quick checkپشکنینی خێراWhy does the swapped program print Good. for 80?بۆچی شوێنگۆڕاوەکە بۆ ⁦80⁩ Good. چاپ دەکات؟

More than two paths: elifزیاتر لە دوو ڕێگا: elif

elif or separate ifs?elif یان چەند ifـی جیا؟

An elif chain is one decision with several exits. Several separate if statements are several decisions, and more than one can run:

زنجیرەیەکی elif یەک بڕیارە بە چەند دەرچەیەک. چەند جیا چەند بڕیارێکن و دەکرێت زیاتر لە یەکێکیان جێبەجێ ببن:

Two separate decisionsدوو بڕیاری جیاseparate_ifs.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
num = 80
if num >= 80:
    print("Excellent!")
if num >= 50:
    print("Good.")
else:
    print("Failed!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

First decision: 80 >= 80 is True, so “Excellent!”.

بڕیاری یەکەم: 80 >= 80 Trueـە، بۆیە «⁦Excellent!⁩».

Lineدێڕی 4-7

A new, separate if: 80 >= 50 is also True, so “Good.” is printed as well.

ifـێکی نوێ و جیا: 80 >= 50ـیش Trueـە، بۆیە «Good.»ـیش چاپ دەکرێت.

Use elif when the cases exclude each other (a mark has one grade). Use separate ifs when each check is independent (a student can get a prize for maths and a prize for attendance). An elif chain also stops testing after the first match, so it does less work.

کاتێک حاڵەتەکان یەکتری دوور دەخەنەوە elif بەکار بهێنە (هەر نمرەیەک یەک پلەی هەیە). کاتێک هەر پشکنینێک سەربەخۆیە ifـی جیا بەکار بهێنە (قوتابییەک دەتوانێت خەڵاتی بیرکاری و خەڵاتی ئامادەبوون وەربگرێت). هەروەها زنجیرەی elif دوای یەکەم گونجان تاقیکردنەوە ڕادەگرێت، بۆیە کاری کەمتر دەکات.

Quick checkپشکنینی خێراA program must print exactly one weather word for a temperature. Which do you use? دەبێت بۆ پلەیەکی گەرمی ڕێک یەک وشەی کەشوهەوا چاپ بکات. کامیان بەکار دەهێنیت؟

More than two paths: elifزیاتر لە دوو ڕێگا: elif

Sorting a number into rangesپۆلێنکردنی ژمارەیەک بەپێی سنوورەکان

A common use of elif is to put a number into a range. Each elif only needs the upper limit, because the lower limit is already guaranteed by the tests above it:

بەکارهێنانێکی باوی elif ئەوەیە ژمارەیەک بخاتە نێو یەکێک لە چەند بەشێک، وەک «⁦10⁩ بۆ ⁦49⁩». هەر elifـێک تەنها سنووری سەرەوەی پێویستە، چونکە سنووری خوارەوە پێشتر تاقیکردنەوەکانی سەرەوە گەرەنتییان کردووە:

Compare with 10, 50 and 100بەراورد لەگەڵ ⁦10⁩، ⁦50⁩ و ⁦100⁩ranges.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("Enter a number: "))
if n < 10:
    print(n, "is less than 10.")
elif n < 50:
    print(n, "is from 10 to 49.")
elif n < 100:
    print(n, "is from 50 to 99.")
else:
    print(n, "is 100 or more.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

We only get here when n is not less than 10, so n < 50 means 10 to 49.

تەنها کاتێک دەگەینە ئێرە کە n لە ⁦10⁩ کەمتر نەبێت، بۆیە n < 50 واتە ⁦10⁩ بۆ ⁦49⁩.

Quick checkپشکنینی خێراIn this program, which message do you get for 99?لەم ، بۆ ⁦99⁩ کام پەیام وەردەگریت؟

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

Four mistakes cause most of the trouble with if. The first three stop the program before it starts; the last one gives a wrong answer silently.

چوار هەڵە زۆربەی گرفتەکانی if دروست دەکەن. سێ دانەی یەکەم پێش دەستپێکردن ڕادەگرن؛ دوایینیان بێدەنگ وەڵامێکی هەڵە دەدات.

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

= instead of === لەبری ==

Assignment where a comparison belongsدانانی نرخ لە شوێنی بەراوردداassign_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 5
if x = 5:
    print("five")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Python even suggests the fix: ==.

Python تەنانەت چارەسەرەکەش پێشنیار دەکات: ==.

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

A missing colon, or a condition after elseدوو خاڵی ون، یان مەرج دوای else

No colon after the conditionدوو خاڵ نییە دوای مەرجەکەcolon_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 5
if x == 5
    print("five")
else cannot have a conditionelse ناتوانێت مەرجی هەبێتelse_cond_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 5
if x > 10:
    print("big")
else x <= 10:
    print("small")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

else means “in every other case”. For another test, write elif x <= 10: — or simply else:.

else واتە «لە هەموو حاڵەتەکانی تردا». بۆ تاقیکردنەوەیەکی تر بنووسە elif x <= 10: — یان تەنها else:.

Quick checkپشکنینی خێراWhich of these lines is correct Python?کام لەم دێڕانە Pythonـی دروستە؟

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

Comparing text with a numberبەراوردکردنی دەق لەگەڵ ژمارە

The right answer is “wrong”وەڵامە ڕاستەکە «هەڵە»یەinput_compare_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
answer = input("2 + 2 = ")
if answer == 4:
    print("Correct!")
else:
    print("Wrong!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

answer is the string "4", and "4" == 4 is False. No error message — just a wrong result.

answer "4"ـە و "4" == 4 Falseـە. هیچ نییە — تەنها ئەنجامێکی هەڵە.

Convert firstسەرەتا بیگۆڕەinput_compare_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
answer = int(input("2 + 2 = "))
if answer == 4:
    print("Correct!")
else:
    print("Wrong!")
Quick checkپشکنینی خێراanswer = input() and the user types 4. What is answer == 4?answer = input() و 4 دەنووسێت. answer == 4 چییە؟

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

More things to avoidشتی تر کە دەبێت خۆتی لێ بپارێزیت

MistakeهەڵەBetterباشتر
if is_raining == True:if is_raining == True:if is_raining: — the variable is already True or False (PEP 8).if is_raining: — خۆی True یان Falseـە ⁦(PEP 8)⁩.
if (num >= 80): and num = 80; copied from Java or Cif (num >= 80): و num = 80; کە لە Java یان Cـەوە هاتوونif num >= 80: and num = 80 — Python needs neither the brackets nor the ;.if num >= 80: و num = 80 — Python نە پێویستی بە کەوانەکانە و نە بە ;.
Lower limit tested first in a >= chainسنووری خوارەوە یەکەم جار تاقی کراوەتەوە لە زنجیرەیەکی >=ـداTest the highest limit first (see The order matters).سەرەتا بەرزترین سنوور تاقی بکەرەوە (بڕوانە ڕیزبەندی گرنگە).
Separate ifs where the cases exclude each otherifـی جیا کاتێک حاڵەتەکان یەکتری دوور دەخەنەوەOne if … elif … else chain.یەک زنجیرەی if … elif … else.
Writing Else or IFنووسینی Else یان IFKeywords are small letters: if, elif, else. بە پیتی بچووکن: if، elif، else.

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

ExerciseڕاهێنانPositive, negative or zeroژمارەی موجەب، ژمارەی سالب یان سفر

Read a whole number and print Positive, Negative or Zero.

بخوێنەوە و Positive، Negative یان Zero چاپ بکە.

Show answerوەڵامەکە پیشان بدە
ex_sign.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("Enter a number: "))
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")
ExerciseڕاهێنانThe larger of two numbersگەورەترینی دوو ژمارە

Read two whole numbers and print the larger one. If they are equal, say so.

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

Show answerوەڵامەکە پیشان بدە
ex_larger.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = int(input("First number: "))
b = int(input("Second number: "))
if a > b:
    print("The larger number is", a)
elif b > a:
    print("The larger number is", b)
else:
    print("The numbers are equal.")
ExerciseڕاهێنانTicket pricesنرخی بلیت

A park charges nothing for children under 6, 1,000 dinars for ages 6 to 17, and 2,000 dinars for everyone older. Read an age and print the price.

باخچەیەک بۆ منداڵی خوار 6 ساڵ هیچ وەرناگرێت، بۆ تەمەنی 6 بۆ ⁦17⁩ ساڵ ⁦1,000⁩ دینار و بۆ هەموو کەسێکی گەورەتر ⁦2,000⁩ دینار. تەمەنێک بخوێنەوە و نرخەکە چاپ بکە.

Hintئاماژە

Start with the smallest limit and use < in each test.

بە بچووکترین سنوور دەست پێ بکە و لە هەر تاقیکردنەوەیەکدا < بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
ex_ticket.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = int(input("Age: "))
if age < 6:
    print("Free entry")
elif age < 18:
    print("Child ticket: 1,000 dinars")
else:
    print("Adult ticket: 2,000 dinars")
ExerciseڕاهێنانPredict the outputدەرخستە پێشبینی بکە

Without running it, write down what this program prints. Careful: how many decisions are there?

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

PYTHON
x = 15
if x > 10:
    print("A")
if x > 20:
    print("B")
elif x > 12:
    print("C")
else:
    print("D")
print("E")
Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 15
if x > 10:
    print("A")
if x > 20:
    print("B")
elif x > 12:
    print("C")
else:
    print("D")
print("E")

There are two decisions. The first if prints A (15 > 10). The second is a chain: 15 > 20 is False, 15 > 12 is True, so C. E is outside both and always prints.

دوو بڕیار هەیە. یەکەم if A چاپ دەکات (15 > 10). دووەمیان زنجیرەیەکە: 15 > 20 Falseـە، 15 > 12 Trueـە، بۆیە ⁦C. E⁩ لە دەرەوەی هەردووکیانە و هەمیشە چاپ دەکرێت.

ExerciseڕاهێنانA grading scaleپێوەرێکی نمرەدان

Use this scale: 90–100 Excellent, 80–89 Very good, 70–79 Good, 60–69 Fair, 50–59 Pass, below 50 Fail. Read a mark from 0 to 100 and print its word.

ئەم پێوەرە بەکار بهێنە: ⁦90–100 Excellent⁩، ⁦80–89 Very good⁩، ⁦70–79 Good⁩، ⁦60–69 Fair⁩، ⁦50–59 Pass⁩، خوار ⁦50 Fail⁩. نمرەیەک لە 0 بۆ ⁦100⁩ بخوێنەوە و وشەکەی چاپ بکە.

Show answerوەڵامەکە پیشان بدە
ex_scale.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
mark = int(input("Mark (0-100): "))
if mark >= 90:
    print("Excellent")
elif mark >= 80:
    print("Very good")
elif mark >= 70:
    print("Good")
elif mark >= 60:
    print("Fair")
elif mark >= 50:
    print("Pass")
else:
    print("Fail")

Lab 2 asks for the same kind of decision inside a function; you will write that in Return values.

تاقیگەی 2 هەمان جۆری بڕیار لەناو داوا دەکات؛ ئەوە لە نرخە گەڕاوەکان دەنووسیت.

Quick checkپشکنینی خێراWhat does this print? x = 5 / if x > 3: / print("big") / else: / print("small") / print("done")ئەمە چی چاپ دەکات؟ x = 5 / if x > 3: / print("big") / else: / print("small") / print("done")

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

Exercises: a calculator and a menuڕاهێنان: حاسیبەیەک و مێنیوێک

ExerciseڕاهێنانA calculator that asks for the operatorحاسیبەیەک کە کارپێکەر دەپرسێت

Write a small calculator. Read two numbers and an operator (+, -, * or /) typed by the user, and use if … elif … else to print the result. If the user tries to divide by zero, print a message instead of crashing. Any other operator gets the message Unknown operator. (The calculator in Input and output printed all four results; this one decides.)

بچووک بنووسە. دوو ژمارە و (+، -، * یان /) کە دەینووسێت بخوێنەوە و بە if … elif … else ئەنجامەکە چاپ بکە. ئەگەر هەوڵی دابەشکردن بەسەر سفردا بدات، لەبری ئەوەی بکەوێت پەیامێک چاپ بکە. هەر تر پەیامی Unknown operator وەردەگرێت. ( نێوخستە و دەرخستە هەر چوار ئەنجامەکەی چاپ دەکرد؛ ئەمەیان بڕیار دەدات.)

Hintئاماژە

Compare the text: op == "+". Inside the / branch you need a second if for the zero check — a block may contain any statement, even another if (more in Nested if).

دەقەکە بەراورد بکە: op == "+". لەناو لقی /ـدا ifـێکی دووەمت بۆ پشکنینی سفر پێویستە — بلۆک دەتوانێت هەر لەخۆ بگرێت، تەنانەت ifـێکی تریش (زیاتر لە ڕستەی ifـی ناوەکی).

Show answerوەڵامەکە پیشان بدە
PYTHON
a = float(input("First number: "))
op = input("Operator (+, -, * or /): ")
b = float(input("Second number: "))

if op == "+":
    print("Result:", a + b)
elif op == "-":
    print("Result:", a - b)
elif op == "*":
    print("Result:", a * b)
elif op == "/":
    if b == 0:
        print("You cannot divide by zero.")
    else:
        print("Result:", a / b)
else:
    print("Unknown operator:", op)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11-15

The zero check sits inside the / branch, so it runs only for division.

پشکنینی سفر لەناو لقی /ـدایە، بۆیە تەنها بۆ دابەشکردن جێبەجێ دەبێت.

Lineدێڕی 16-17

else catches every operator the program does not know.

else هەموو ئەو دەگرێت کە نایانناسێت.

ExerciseڕاهێنانA menu for marksمێنیوێک بۆ نمرەکان

Write a menu program. Show three numbered options — 1 enter five marks, 2 show the grade table, 3 quit — and read the user's choice. For 1, read five marks out of 100 and print the total (out of 500), the percentage with one decimal place and the grade from the table below. For 2, print the table; for 3, say goodbye; for anything else, ask for 1, 2 or 3.

بنووسە کە لە سێ هەڵبژاردەی ژمارەدار پیشان بدات — 1 نووسینی پێنج نمرە، 2 پیشاندانی خشتەی پلەکان، 3 دەرچوون — پاشان هەڵبژاردنی بخوێنەوە. بۆ 1، پێنج نمرە لە ⁦100⁩ بخوێنەوە و کۆ (لە ⁦500⁩)، بە یەک و پلەکە بەپێی خشتەکەی خوارەوە چاپ بکە. بۆ 2 خشتەکە چاپ بکە؛ بۆ 3 ماڵئاوایی بکە؛ بۆ هەر شتێکی تر داوای 1، 2 یان 3 بکە.

The grade tableخشتەی پلەکان
PercentageGradeپلە
above 85 %سەروو ⁦85⁩٪A
75 to 85 %⁦75⁩ بۆ ⁦85⁩٪B
50 to below 75 %⁦50⁩ بۆ کەمتر لە ⁦75⁩٪C
30 to below 50 %⁦30⁩ بۆ کەمتر لە ⁦50⁩٪D
below 30 %خوار ⁦30⁩٪re-sit the examدووبارە تاقیکردنەوە
Hintئاماژە

input() gives text, so compare the choice with "1", not 1. The grade is an if … elif … else chain inside the block for "1", from the highest limit down.

input() دەق دەدات، بۆیە هەڵبژاردنەکە لەگەڵ "1" بەراورد بکە، نەک 1. پلەکە زنجیرەیەکی if … elif … elseـە لەناو بلۆکی "1"ـدا، لە بەرزترین سنوورەوە بەرەو خوار.

Show answerوەڵامەکە پیشان بدە
PYTHON
print("1. Enter five marks")
print("2. Show the grade table")
print("3. Quit")
choice = input("Choose 1, 2 or 3: ")

if choice == "1":
    m1 = int(input("Mark 1: "))
    m2 = int(input("Mark 2: "))
    m3 = int(input("Mark 3: "))
    m4 = int(input("Mark 4: "))
    m5 = int(input("Mark 5: "))
    total = m1 + m2 + m3 + m4 + m5
    percentage = total / 500 * 100
    if percentage > 85:
        grade = "A"
    elif percentage >= 75:
        grade = "B"
    elif percentage >= 50:
        grade = "C"
    elif percentage >= 30:
        grade = "D"
    else:
        grade = "Re-sit"
    print("Total:", total, "out of 500")
    print(f"Percentage: {percentage:.1f}%")
    print("Grade:", grade)
elif choice == "2":
    print("A: above 85%  B: 75-85%  C: 50-74%  D: 30-49%  below 30%: re-sit")
elif choice == "3":
    print("Goodbye!")
else:
    print("Please choose 1, 2 or 3.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 13

391500×100=78.2\frac{391}{500} \times 100 = 78.2.

391500×100=78.2\frac{391}{500} \times 100 = 78.2.

Lineدێڕی 14-23

The grade chain goes from the highest limit down, so each elif only needs its lower limit.

زنجیرەی پلەکان لە بەرزترین سنوورەوە بەرەو خوار دەڕوات، بۆیە هەر elifـێک تەنها سنووری خوارەوەی خۆی پێویستە.

A real menu comes back after each choice until the user picks Quit. When you know the while loop, put the menu inside while choice != "3": (start with choice = ""):

ڕاستەقینە دوای هەر هەڵبژاردنێک دەگەڕێتەوە تا دەرچوون هەڵدەبژێرێت. کاتێک خولی while دەناسیت، بخەرە ناو while choice != "3": (بە choice = "" دەست پێ بکە):

Later: the same menu in a loopدواتر: هەمان مێنیو لەناو خولێکداPYTHON
choice = ""
while choice != "3":
    print("1. Enter five marks")
    print("2. Show the grade table")
    print("3. Quit")
    choice = input("Choose 1, 2 or 3: ")

    if choice == "1":
        m1 = int(input("Mark 1: "))
        m2 = int(input("Mark 2: "))
        m3 = int(input("Mark 3: "))
        m4 = int(input("Mark 4: "))
        m5 = int(input("Mark 5: "))
        total = m1 + m2 + m3 + m4 + m5
        percentage = total / 500 * 100
        if percentage > 85:
            grade = "A"
        elif percentage >= 75:
            grade = "B"
        elif percentage >= 50:
            grade = "C"
        elif percentage >= 30:
            grade = "D"
        else:
            grade = "Re-sit"
        print("Total:", total, "out of 500")
        print(f"Percentage: {percentage:.1f}%")
        print("Grade:", grade)
    elif choice == "2":
        print("A: above 85%  B: 75-85%  C: 50-74%  D: 30-49%  below 30%: re-sit")
    elif choice == "3":
        print("Goodbye!")
    else:
        print("Please choose 1, 2 or 3.")
Quick checkپشکنینی خێراWhy does the menu program test choice == "1" with quotes, and not choice == 1?بۆچی choice == "1" بە تاقی دەکاتەوە، نەک choice == 1؟

Summaryپوختە

  • A condition is an expression whose value is True or False (type bool).
  • Comparison operators: ==, !=, <, >, <=, >=. = stores, == compares.
  • if condition: + an indented block runs only when the condition is True.
  • else: catches every other case; exactly one of if/else runs.
  • elif adds more cases; Python runs the first True branch only, so the order of the tests matters.
  • Blocks are made by indentation (4 spaces). Python needs no brackets around conditions and no ;.
  • کە True یان Falseـە (جۆری bool).
  • : ==، !=، <، >، <=، >=. = هەڵدەگرێت و == بەراورد دەکات.
  • if condition: + بلۆکێکی چووەتە ناوەوە تەنها کاتێک جێبەجێ دەبێت کە True بێت.
  • else: هەموو حاڵەتەکانی تر دەگرێت؛ ڕێک یەکێک لە if/else جێبەجێ دەبێت.
  • elif حاڵەتی زیاتر زیاد دەکات؛ Python تەنها یەکەم لقی True جێبەجێ دەکات، بۆیە ڕیزبەندیی تاقیکردنەوەکان گرنگە.
  • بلۆک بە دروست دەبێت (⁦4⁩ بۆشایی). Python پێویستی بە کەوانە لە دەوری و ; نییە.
Review questionsپرسیارەکانی پێداچوونەوە
What is the difference between = and ==?جیاوازیی نێوان = و == چییە؟

= assigns a value to a variable; == asks whether two values are equal and gives True or False.

= دەدات بە ؛ == دەپرسێت ئایا دوو یەکسانن و True یان False دەدات.

How many blocks of an if … elif … elif … else chain can run?چەند بلۆکی زنجیرەیەکی if … elif … elif … else دەکرێت جێبەجێ ببن؟

Exactly one: the first whose condition is True, or the else.

ڕێک یەک: یەکەمیان کە True بێت، یان else.

Why does input() == 4 never become True?بۆچی input() == 4 هەرگیز نابێتە True؟

input() gives a string, and a string is never equal to a number. Use int(input()) == 4.

input() دەدات و هەرگیز یەکسانی ژمارە نییە. int(input()) == 4 بەکار بهێنە.

How do you test whether n is even?چۆن تاقی دەکەیتەوە کە n جووتە؟

if n % 2 == 0:

if n % 2 == 0:

Videoڤیدیۆ

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

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

If statements in Python are easy (if, elif, else) 🤔Bro Code · Englishئینگلیزی · 437K viewsبینین · 9K likesلایک

How if, else and elif choose one path, with short age and yes/no examples, two exercises and a Boolean variable as the condition.

if، else و elif چۆن یەک ڕێگە هەڵدەبژێرن: نموونەی کورتی تەمەن و بەڵێ/نەخێر، دوو ڕاهێنان و وەک .

Whole video (8:20). Chapters: 0:00 if, 1:27 else, 2:19 elif, 4:22 and 5:45 exercises, 6:37 a bool variable as the condition.

هەموو ڤیدیۆکە ⁦(8:20)⁩. بەشەکان: ⁦0:00⁩ if، ⁦1:27⁩ else، ⁦2:19⁩ elif، ⁦4:22⁩ و ⁦5:45⁩ ڕاهێنان، ⁦6:37⁩ bool وەک .

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

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

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

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

Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی

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

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