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ـی کۆرسەکە، پاککراوە.
What you will learnچی فێر دەبیت
- Write conditions with the six comparison operators and predict their True/False value.
- Use
if,if … elseandif … elif … elsewith correctly indented blocks. - Order
eliftests so that every case is reachable, and choose betweenelifand separateifs. - 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 دەنووسرێن.
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ـی هەیە).
Programs that make decisionsبەرنامەکان کە بڕیار دەدەن
Two roads, one choiceدوو ڕێگا، یەک هەڵبژاردن
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.
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ـە و دەتوانیت ئەو وەک هەر تر لە هەڵبگریت.
print(5 > 3)
print(5 == 3)
print(type(True))
is_big = 10 > 5
print(is_big)True False <class 'bool'> True
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The type of
Trueisbool.جۆری
Trueبریتییە لەbool.- Lineدێڕی 4-5
10 > 5is worked out first;is_bigstores the answerTrue.سەرەتا
10 > 5هەژمار دەکرێت؛is_bigوەڵامیTrueهەڵدەگرێت.
Asking questions: comparison operatorsپرسیار کردن: کارپێکەرەکانی بەراوردکردن
Most conditions compare two values. Python has six comparison operators, and each one gives True or False:
زۆربەی دوو بەراورد دەکەن. لە Pythonـدا شەش هەیە و هەر یەکەیان True یان False دەدات:
| Operator | Questionپرسیار | Example with a = 7نموونە بە a = 7 | Resultئەنجام |
|---|---|---|---|
== | equal to?یەکسانە بە؟ | a == 7 | True |
!= | not equal to?یەکسان نییە بە؟ | a != 7 | False |
< | less than?کەمترە لە؟ | a < 10 | True |
> | greater than?زیاترە لە؟ | a > 10 | False |
<= | less than or equal to?کەمترە یان یەکسانە بە؟ | a <= 7 | True |
>= | greater than or equal to?زیاترە یان یەکسانە بە؟ | a >= 8 | 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)True False True False True False False False
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 number5. 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 ڕیزبەندیی ئەلفوبێیی بەپێی کۆدی بەکار دەهێنێت. ئەمە لە کارکردن لەگەڵ زنجیرەنووسەکان ڕوون کراوەتەوە.
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 بلۆکەکە بەجێ دەهێڵێت و لەگەڵ یەکەم دێڕی دوای ئەو بەردەوام دەبێت.
temperature = int(input("Temperature today: "))
if temperature > 30:
print("It is hot.")
print("Drink plenty of water.")
print("Have a nice day!")Temperature today: 35 It is hot. Drink plenty of water. Have a nice day! Temperature today: 20 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ـە: هەموو جارێک جێبەجێ دەبێت.
if— a keyword: it starts the decision.- the condition — any expression that gives
TrueorFalse. No brackets are needed around it. :— the colon ends the header line. Forgetting it is a syntax error.- the block — one or more lines, all indented by the same amount (4 spaces is the standard).
if— : بڕیارەکە دەست پێ دەکات.- — هەر کە
TrueیانFalseبدات. پێویست بە کەوانە نییە لە دەوری. :— دوو خاڵەکە دێڕی سەرەتا تەواو دەکات. لەبیرکردنی .- بلۆکەکە — یەک یان چەند دێڕێک، هەموویان بە هەمان ئەندازە چوونەتە ناوەوە (4 بۆشایی ستانداردە).
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 چوار دادەنێت.
temperature = 35
if temperature > 30:
print("It is hot.") File "indent_error.py", line 3
print("It is hot.")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The block of an
ifmust be indented. Python names the problem and the line of theif.بلۆکی
ifدەبێت بچێتە ناوەوە. Python کێشەکە و دێڕیifـەکە ناو دەبات.
temperature = 35
if temperature > 30:
print("It is hot.")
print("Drink plenty of water.") File "indent_mixed.py", line 4
print("Drink plenty of water.")
IndentationError: unexpected indentExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Two extra spaces: Python does not know which block this line belongs to.
دوو بۆشایی زیادە: Python نازانێت ئەم دێڕە سەر بە کام بلۆکە.
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: لەگەڵ بلۆکی خۆی زیاد بکە. هەموو جارێک ڕێک یەکێک لە دوو بلۆکەکە جێبەجێ دەبێت.
if … else is a diamond with two exits that meet again.if … else ئەڵماسییەکە بە دوو دەرچە کە دووبارە یەک دەگرنەوە.CêITn = int(input("Enter a whole number: "))
if n % 2 == 0:
print(n, "is even.")
else:
print(n, "is odd.")Enter a whole number: 14 14 is even. Enter a whole number: 7 7 is odd.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
n % 2is the remainder after dividing by 2 (Operators); 0 means even.n % 2دوای دابەشکردن بەسەر 2دا (کارپێکەرەکان)؛ 0 واتە جووت.- Lineدێڕی 4
elsehas no condition: it simply catches every other case.elseهیچ نییە: تەنها هەموو حاڵەتەکانی تر دەگرێت.
age = int(input("Your age: "))
if age >= 18:
print("You can vote.")
else:
print("You can vote in", 18 - age, "years.")Your age: 21 You can vote. Your age: 15 You can vote in 3 years.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
The
elseblock can calculate too: years to wait.بلۆکی
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 ئارەزوومەندانەیە).
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ی کۆرسەکە، ڕێک وەک لەوێدا نووسراوە، لەگەڵ :
num = 80;
if (num >= 80):
print("Excelent!")
elif (num >= 50):
print("Good.")
else:
print("Failed!")Excelent!
More than two paths: elifزیاتر لە دوو ڕێگا: elif
The grade decision, step by stepبڕیاری نمرە، هەنگاو بە هەنگاو
num = int(input("Mark: "))
if num >= 80:
print("Excellent!")
elif num >= 50:
print("Good.")
else:
print("Failed!")Mark: 80 Excellent! Mark: 65 Good. Mark: 30 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.


True بڕیار دەدات.CêITMore 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 جێبەجێ دەبێت، سەختترین لە پێشەوە دابنێ. لێرەدا دوو تاقیکردنەوەکە شوێنیان گۆڕدراوە:
num = 80
if num >= 50:
print("Good.")
elif num >= 80:
print("Excellent!")
else:
print("Failed!")Good.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
80 >= 50isTrue, so this branch wins and “Excellent!” can never be printed: theelifbelow it is unreachable for every mark of 50 or more.80 >= 50Trueـە، بۆیە ئەم لقە دەباتەوە و «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).
ئەمە : هیچ پەیامێک نییە، تەنها وەڵامێکی هەڵە. لەگەڵ تاقیکردنەوەی >=، لە بەرزترین سنوورەوە بۆ نزمترین بڕۆ (یان لە نزمترین سنووری <ـەوە بەرەو سەرەوە).
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 یەک بڕیارە بە چەند دەرچەیەک. چەند جیا چەند بڕیارێکن و دەکرێت زیاتر لە یەکێکیان جێبەجێ ببن:
num = 80
if num >= 80:
print("Excellent!")
if num >= 50:
print("Good.")
else:
print("Failed!")Excellent! Good.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
First decision:
80 >= 80isTrue, so “Excellent!”.بڕیاری یەکەم:
80 >= 80Trueـە، بۆیە «Excellent!».- Lineدێڕی 4-7
A new, separate
if:80 >= 50is alsoTrue, 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 دوای یەکەم گونجان تاقیکردنەوە ڕادەگرێت، بۆیە کاری کەمتر دەکات.
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ـێک تەنها سنووری سەرەوەی پێویستە، چونکە سنووری خوارەوە پێشتر تاقیکردنەوەکانی سەرەوە گەرەنتییان کردووە:
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.")Enter a number: 7 7 is less than 10. Enter a number: 50 50 is from 50 to 99. Enter a number: 100 100 is 100 or more.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
We only get here when n is not less than 10, so
n < 50means 10 to 49.تەنها کاتێک دەگەینە ئێرە کە n لە 10 کەمتر نەبێت، بۆیە
n < 50واتە 10 بۆ 49.
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 === لەبری ==
x = 5
if x = 5:
print("five") File "assign_bug.py", line 2
if x = 5:
^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Python even suggests the fix:
==.Python تەنانەت چارەسەرەکەش پێشنیار دەکات:
==.
Common mistakesهەڵە باوەکان
A missing colon, or a condition after elseدوو خاڵی ون، یان مەرج دوای else
x = 5
if x == 5
print("five") File "colon_bug.py", line 2
if x == 5
^
SyntaxError: expected ':'else ناتوانێت مەرجی هەبێتelse_cond_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەx = 5
if x > 10:
print("big")
else x <= 10:
print("small") File "else_cond_bug.py", line 4
else x <= 10:
^
SyntaxError: expected ':'Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
elsemeans “in every other case”. For another test, writeelif x <= 10:— or simplyelse:.elseواتە «لە هەموو حاڵەتەکانی تردا». بۆ تاقیکردنەوەیەکی تر بنووسەelif x <= 10:— یان تەنهاelse:.
Common mistakesهەڵە باوەکان
Comparing text with a numberبەراوردکردنی دەق لەگەڵ ژمارە
answer = input("2 + 2 = ")
if answer == 4:
print("Correct!")
else:
print("Wrong!")2 + 2 = 4 Wrong!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
answeris the string"4", and"4" == 4is False. No error message — just a wrong result.answer"4"ـە و"4" == 4Falseـە. هیچ نییە — تەنها ئەنجامێکی هەڵە.
answer = int(input("2 + 2 = "))
if answer == 4:
print("Correct!")
else:
print("Wrong!")2 + 2 = 4 Correct!
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 یان IF | Keywords are small letters: if, elif, else. بە پیتی بچووکن: if، elif، else. |
Exercisesڕاهێنانەکان
Read a whole number and print Positive, Negative or Zero.
بخوێنەوە و Positive، Negative یان Zero چاپ بکە.
Show answerوەڵامەکە پیشان بدە
n = int(input("Enter a number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")Enter a number: -3 Negative Enter a number: 0 Zero
Read two whole numbers and print the larger one. If they are equal, say so.
دوو بخوێنەوە و گەورەترینیان چاپ بکە. ئەگەر یەکسان بوون، ئەوە بڵێ.
Show answerوەڵامەکە پیشان بدە
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.")First number: 12 Second number: 9 The larger number is 12 First number: 4 Second number: 4 The numbers are equal.
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وەڵامەکە پیشان بدە
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")Age: 4 Free entry Age: 12 Child ticket: 1,000 dinars Age: 30 Adult ticket: 2,000 dinars
Without running it, write down what this program prints. Careful: how many decisions are there?
بەبێ جێبەجێکردن، بنووسە ئەم چی چاپ دەکات. ئاگاداربە: چەند بڕیار هەیە؟
x = 15
if x > 10:
print("A")
if x > 20:
print("B")
elif x > 12:
print("C")
else:
print("D")
print("E")Show answerوەڵامەکە پیشان بدە
x = 15
if x > 10:
print("A")
if x > 20:
print("B")
elif x > 12:
print("C")
else:
print("D")
print("E")A C 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 لە دەرەوەی هەردووکیانە و هەمیشە چاپ دەکرێت.
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وەڵامەکە پیشان بدە
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")Mark (0-100): 78 Good Mark (0-100): 45 Fail
Lab 2 asks for the same kind of decision inside a function; you will write that in Return values.
تاقیگەی 2 هەمان جۆری بڕیار لەناو داوا دەکات؛ ئەوە لە نرخە گەڕاوەکان دەنووسیت.
Exercisesڕاهێنانەکان
Exercises: a calculator and a menuڕاهێنان: حاسیبەیەک و مێنیوێک
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وەڵامەکە پیشان بدە
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)First number: 7 Operator (+, -, * or /): / Second number: 2 Result: 3.5 First number: 7 Operator (+, -, * or /): / Second number: 0 You cannot divide by zero. First number: 6 Operator (+, -, * or /): x Second number: 2 Unknown operator: x
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11-15
The zero check sits inside the
/branch, so it runs only for division.پشکنینی سفر لەناو لقی
/ـدایە، بۆیە تەنها بۆ دابەشکردن جێبەجێ دەبێت.- Lineدێڕی 16-17
elsecatches every operator the program does not know.elseهەموو ئەو دەگرێت کە نایانناسێت.
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 بکە.
| Percentage | Gradeپلە |
|---|---|
| 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وەڵامەکە پیشان بدە
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.")1. Enter five marks 2. Show the grade table 3. Quit Choose 1, 2 or 3: 1 Mark 1: 78 Mark 2: 85 Mark 3: 90 Mark 4: 66 Mark 5: 72 Total: 391 out of 500 Percentage: 78.2% Grade: B
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 13
.
.
- Lineدێڕی 14-23
The grade chain goes from the highest limit down, so each
elifonly 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 = "" دەست پێ بکە):
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.")1. Enter five marks 2. Show the grade table 3. Quit Choose 1, 2 or 3: 2 A: above 85% B: 75-85% C: 50-74% D: 30-49% below 30%: re-sit 1. Enter five marks 2. Show the grade table 3. Quit Choose 1, 2 or 3: 1 Mark 1: 40 Mark 2: 55 Mark 3: 38 Mark 4: 61 Mark 5: 47 Total: 241 out of 500 Percentage: 48.2% Grade: D 1. Enter five marks 2. Show the grade table 3. Quit Choose 1, 2 or 3: 3 Goodbye!
Summaryپوختە
- A condition is an expression whose value is
TrueorFalse(typebool). - Comparison operators:
==,!=,<,>,<=,>=.=stores,==compares. if condition:+ an indented block runs only when the condition is True.else:catches every other case; exactly one ofif/elseruns.elifadds 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 پێویستی بە کەوانە لە دەوری و
;نییە.
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
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زاراوە سەرەکییەکان
Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی
Related lecturesوانە پەیوەندیدارەکان
- Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
- Return Values and Noneنرخە گەڕاوەکان و None
- Conditional Statements: if, else, elseif and switchڕستە مەرجییەکان: if و else و elseif و switch
- The while Loopخولی while
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python Tutorial 4.1: if Statements
- Harvard CS50's Introduction to Programming with Python, Lecture 1 notes: Conditionals (if, elif, else, modulo)
- Python Library: Built-in types — comparisons and Boolean values
- PEP 8 — Style Guide for Python Code (indentation, comparing to True/False)
- Liang, Introduction to Programming Using Python — table of contents (chapter 4, Selections)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.