PythonPython · Chapterبەش 5 · Lectureوانە 5.2
Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی
Combine conditions with and, or and not, read their truth tables, know which goes first, use short-circuit evaluation and chained comparisons, write nested if statements (with the weather example from the course's report and its hidden bug), choose a value with a conditional expression, and meet match.
پێکەوە بەستنی بە and، or و not، خوێندنەوەی ، زانینی ئەوەی کامیان یەکەم کار دەکات، بەکارهێنانی و ، نووسینی (لەگەڵ نموونەی کەشوهەوا لە ڕاپۆرتی کۆرسەکە و شاراوەکەی)، هەڵبژاردنی بە و ناسینی match.
What you will learnچی فێر دەبیت
- Combine conditions with
and,orandnot, and fill in their truth tables. - Predict the result of mixed conditions using the order
not,and,or, and add parentheses for clarity. - Use short-circuit evaluation as a guard, and write ranges as chained comparisons.
- Write and simplify nested
ifstatements, and find bugs hidden in untested branches. - Choose between two values with a conditional expression.
- بە
and،orوnotپێکەوە ببەستیت و پڕ بکەیتەوە. - ئەنجامی تێکەڵ بە ڕیزبەندیی
not،and،orپێشبینی بکەیت و بۆ ڕوونی کەوانە زیاد بکەیت. - وەک پاسەوان بەکار بهێنیت و نێوانەکان (وەک «لە 5 بۆ 15») وەک بنووسیت.
- بنووسیت و سادەیان بکەیتەوە و ئەو بدۆزیتەوە کە لە لقە تاقینەکراوەکاندا شاراونەتەوە.
- بە لە نێوان دوو هەڵبژێریت.
Decisions with more than one factبڕیار بە زیاتر لە یەک ڕاستی
Real decisions often depend on several facts at once. “We go on the picnic if it is Friday and it is not raining.” “You get a discount if you are a student or over 60.” Python joins conditions with three logical operators, written as plain English words: and, or and not.
بڕیارە ڕاستەقینەکان زۆرجار لە یەک کاتدا بەندن بە چەند ڕاستییەک. «دەچینە سەیران ئەگەر هەینی بێت و باران نەبارێت.» «داشکاندن وەردەگریت ئەگەر قوتابی بیت یان تەمەنت لە 60 زیاتر بێت.» Python بە سێ پێکەوە دەبەستێت کە وەک وشەی ئینگلیزیی ئاسایی دەنووسرێن: and، or و not.
An operator that combines true/false values: and, or and not. Python writes them as words (and, or, not); PHP, Java and C# use &&, || and ! (PHP also has and, or and xor with lower precedence).
کە نرخەکانی ڕاست و هەڵە پێکەوە دەبەستێت: and، or و not. Python بە وشە دەیاننووسێت (and، or، not)؛ PHP، Java و C# &&، || و ! بەکار دەهێنن (PHP هەروەها and، or و xorـی هەیە بە پێشینەیی نزمتر).
| Operator | Meaningواتا | True when…Trueـە کاتێک… |
|---|---|---|
A and B | A and BA و B | both sides are Trueهەردوو لا True بن |
A or B | A or B (or both)A یان B (یان هەردووکیان) | at least one side is Trueلانیکەم یەک لا True بێت |
not A | the opposite of Aپێچەوانەی A | A is FalseA False بێت |
and, or and not explained with a temperature-and-sunny example: when the whole condition is True or False.
and، or و not بە نموونەی گەرما و هەوای خۆرەتاو: کەی هەموو Trueـە و کەی Falseـە.
Whole video (5:03). Chapters: 0:37 and, 2:18 or, 3:24 not.
هەموو ڤیدیۆکە (5:03). بەشەکان: 0:37 and، 2:18 or، 3:24 not.
and: both must be trueand: دەبێت هەردووکیان ڕاست بن
A and B is True only when both A and B are True. If either one is False, the whole condition is False. A typical rule: to pass a course you need a mark of at least 50 and at least 75 % attendance.
A and B تەنها کاتێک Trueـە کە هەردوو A و B True بن. ئەگەر هەر یەکێکیان False بێت، هەموو Falseـە. یاسایەکی ئاسایی: بۆ دەرچوون لە کۆرسێک پێویستت بە نمرەی لانیکەم 50 و ئامادەبوونی لانیکەم 75٪ هەیە.
mark = int(input("Exam mark: "))
attendance = int(input("Attendance (%): "))
if mark >= 50 and attendance >= 75:
print("You passed the course.")
else:
print("You did not pass the course.")Exam mark: 65 Attendance (%): 80 You passed the course. Exam mark: 65 Attendance (%): 60 You did not pass the course.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Both comparisons are worked out first; then
andjoins their two answers.سەرەتا هەردوو بەراوردەکە هەژمار دەکرێن؛ پاشان
andدوو وەڵامەکەیان پێکەوە دەبەستێت.
| A | B | A and B |
|---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
or: at least one must be trueor: دەبێت لانیکەم یەکێکیان ڕاست بێت
A or B is True when at least one side is True — also when both are. It is False only when both sides are False. In the Kurdistan Region the weekend is Friday and Saturday:
A or B کاتێک Trueـە کە لانیکەم یەک لا True بێت — هەروەها کاتێک هەردووکیان True بن. تەنها کاتێک Falseـە کە هەردوو لا False بن. لە هەرێمی کوردستان پشووی کۆتایی هەفتە هەینی و شەممەیە:
day = input("Day: ")
if day == "Friday" or day == "Saturday":
print("Weekend!")
else:
print("School day.")Day: Friday Weekend! Day: Monday School day.
| A | B | A or B |
|---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
or: at least one must be trueor: دەبێت لانیکەم یەکێکیان ڕاست بێت
The or trap: day == "Friday" or "Saturday"تەڵەی or: day == "Friday" or "Saturday"
In English we say “if the day is Friday or Saturday”. Writing exactly that in Python is a classic bug:
بە ئینگلیزی دەڵێین «ئەگەر ڕۆژەکە هەینی یان شەممە بێت». نووسینی ڕێک ئەوە بە Python کلاسیکییە:
day = input("Day: ")
if day == "Friday" or "Saturday":
print("Weekend!")
else:
print("School day.")Day: Monday Weekend!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Python reads this as
(day == "Friday") or ("Saturday"). The right side is just the text"Saturday", and any non-empty text counts as True — so the condition is always True.Python ئەمە وەک
(day == "Friday") or ("Saturday")دەخوێنێتەوە. لای ڕاست تەنها دەقی"Saturday"ـە و هەر دەقێکی نابەتاڵ وەکTrueدادەنرێت — بۆیە هەمیشەTrueـە.
Each side of or must be a complete condition: day == "Friday" or day == "Saturday". (With collections, day in ("Friday", "Saturday") is shorter; you will meet in with strings and lists later.)
هەر لایەکی or دەبێت تەواو بێت: day == "Friday" or day == "Saturday". (لەگەڵ کۆمەڵە، day in ("Friday", "Saturday") کورتترە؛ دواتر in لەگەڵ و دەناسیت.)
not: the oppositenot: پێچەوانە
not turns True into False and False into True. It reads naturally with Boolean variables: if not is_raining:.
not True دەکات بە False و False دەکات بە True. لەگەڵ بە سروشتی دەخوێنرێتەوە: if not is_raining:.
is_raining = False
if not is_raining:
print("No umbrella needed.")
print(not True)
print(not 3 > 5)No umbrella needed. False True
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
not 3 > 5meansnot (3 > 5): comparisons are worked out beforenot.not 3 > 5واتەnot (3 > 5): بەراوردەکان پێشnotهەژمار دەکرێن.
| A | not A |
|---|---|
False | True |
True | False |
All combinations at once: truth tablesهەموو تێکەڵەکان پێکەوە: خشتەکانی ڕاستی
Two conditions A and B have exactly four combinations. A truth table lists all of them and the result of each operator. Watch the two switches go through the four rows:
دوو A و B ڕێک چوار تێکەڵەیان هەیە. هەموویان و ئەنجامی هەر ڕیز دەکات. سەیری دوو بکە کە بە چوار ڕیزەکەدا تێدەپەڕن:

and needs both switches on; or needs at least one.and پێویستی بە هەردوو داگیرساوە؛ or پێویستی بە لانیکەم یەکێکیانە.CêIT| A | B | A and B | A or B | not A |
|---|---|---|---|---|
False | False | False | False | True |
False | True | False | True | True |
True | False | False | True | False |
True | True | True | True | False |
These are the same tables that logic gates follow inside digital circuits. The Logic Design course builds them as the AND gate, the OR gate and the NOT gate, and Truth tables shows how to fill one in for any circuit. Boolean Expressions and Laws then gives the laws of AND and OR that let you rearrange and simplify conditions. The AI course meets them again in propositional logic.
ئەمانە هەمان ئەو خشتانەن کە لەناو پەیڕەویان دەکەن. کۆرسی دیزاینی ئەمانە وەک گەیتی AND، گەیتی OR و گەیتی NOT دروست دەکات و خشتەکانی ڕاستی پیشان دەدات چۆن بۆ هەر یەکێکیان پڕ بکەیتەوە. پاشان دەربڕینە بوولییەکان و یاساکانی جەبری بوول یاساکانی AND و OR پیشان دەدات کە پێیان ڕێک دەخەیتەوە و سادە دەکەیتەوە. کۆرسی جارێکی تر لە لۆجیکی گوزارەیی دەیانبینێتەوە.
Which goes first: not, and, orکامیان یەکەم: not، and، or
In a long condition Python works in this order: arithmetic first, then comparisons, then not, then and, and or last. So True or False and False means True or (False and False).
لە درێژدا Python بەم ڕیزبەندییە کار دەکات: یەکەم هەژمار، پاشان بەراوردەکان، پاشان not، پاشان and و لە کۆتاییدا or. بۆیە True or False and False واتە True or (False and False).
and پێش orprecedence.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەprint(True or False and False)
print((True or False) and False)
age = 20
print(age > 18 and age < 30)
print(not age > 18 and age < 30)True False True False
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
andfirst:False and FalseisFalse; thenTrue or FalseisTrue.یەکەم
and:False and FalseدەکاتەFalse؛ پاشانTrue or FalseدەکاتەTrue.- Lineدێڕی 2
Parentheses change the order and the answer.
کەوانە ڕیزبەندی و وەڵامەکە دەگۆڕێت.
- Lineدێڕی 5
notapplies only toage > 18, not to the whole line:(not True) and TrueisFalse.notتەنها بەسەرage > 18ـدا دەچێت، نەک هەموو دێڕەکە:(not True) and TrueدەکاتەFalse.
Python stops as soon as it knowsPython هەر کە زانی دەوەستێت
Python reads and and or from left to right and stops as soon as the answer is certain. If the left side of and is False, the whole thing is False, so the right side is never evaluated. If the left side of or is True, the right side is skipped. This is called short-circuit evaluation, and it lets the left side protect the right side:
Python and و or لە چەپەوە بۆ ڕاست دەخوێنێتەوە و هەر کە وەڵامەکە دڵنیا بوو دەوەستێت. ئەگەر لای چەپی and False بێت، هەموو شتەکە Falseـە، بۆیە لای ڕاست هەرگیز هەژمار ناکرێت. ئەگەر لای چەپی or True بێت، لای ڕاست بەجێ دەهێڵرێت. ئەمە پێی دەوترێت و ڕێگە دەدات لای چەپ لای ڕاست بپارێزێت:
Stopping the work on and / or (&& / ||) as soon as the answer is known: if the left side of and is false, or the left side of or is true, the right side is not checked. Python, PHP, Java and C# all do this.
وەستاندنی هەڵسەنگاندنی and / or (&& / ||) هەر کە وەڵامەکە زانرا: ئەگەر لای چەپی and هەڵە بێت، یان لای چەپی or ڕاست بێت، لای ڕاست ناپشکنرێت. Python، PHP، Java و C# هەموویان ئەمە دەکەن.
x = 0
if x != 0 and 10 / x > 2:
print("10 / x is more than 2")
else:
print("x is zero, or 10 / x is small")x is zero, or 10 / x is small
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
x != 0is False, so Python never computes10 / x: no division by zero.x != 0Falseـە، بۆیە Python هەرگیز10 / xهەژمار ناکات: هیچ دابەشکردنێک بەسەر سفردا نییە.
x = 0
if 10 / x > 2 and x != 0:
print("10 / x is more than 2")Traceback (most recent call last):
File "short_crash.py", line 2, in <module>
if 10 / x > 2 and x != 0:
~~~^~~
ZeroDivisionError: division by zeroExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Now the division comes first and crashes before
x != 0is ever checked.ئێستا دابەشکردنەکە یەکەم دێت و پێش ئەوەی
x != 0بپشکنرێت دەکەوێت.
Python stops as soon as it knowsPython هەر کە زانی دەوەستێت
Extra: values that count as True or Falseزیاتر: ئەو نرخانەی وەک True یان False دادەنرێن
The comparison operators ==, !=, <, >, <= and >= in Python, and which values Python treats as False in a condition.
==، !=، <، >، <= و >= لە ، هەروەها کام لە بە False دادەنرێت.
Plays 11:43-15:36: False, None, 0, empty strings, lists and dictionaries all count as False in a condition.
لە 11:43 تا 15:36 پەخش دەبێت: False، None، 0، ، و بەتاڵ هەموویان لە بە False دادەنرێن.
Ranges in one line: chained comparisonsنێوانێک لە یەک دێڕدا: بەراوردێکی زنجیرەیی
To ask whether a is between 5 and 15 you can write 5 <= a and a < 15. Python also lets you write it the way maths does: 5 <= a < 15. This chained comparison means exactly the same thing, and a is evaluated only once.
بۆ پرسینی ئەوەی ئایا a لە نێوان 5 و 15دایە، دەتوانیت بنووسیت 5 <= a and a < 15. Python ڕێگەش دەدات وەک بیرکاری بینووسیت: 5 <= a < 15. ئەم ڕێک هەمان واتای هەیە و a تەنها یەک جار هەژمار دەکرێت.
Several written one after another, as in maths: 5 <= a < 15 means 5 <= a and a < 15. Python allows it; Java and C# do not.
چەند کە لە دوای یەک دەنووسرێن، وەک بیرکاری: 5 <= a < 15 واتە 5 <= a and a < 15. Python ڕێگەی پێ دەدات؛ Java و C# نا.
a = 12
print(5 <= a < 15)
a = 15
print(5 <= a < 15)
score = 95
print(90 <= score <= 100)True False True
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
15 < 15isFalse, so 15 is outside5 <= a < 15. Decide whether your range includes its end:<leaves it out,<=keeps it in.15 < 15Falseـە، بۆیە 15 لە دەرەوەی5 <= a < 15ـە. بڕیار بدە ئایا نێوانەکەت کۆتاییەکەی لەخۆ دەگرێت:<دەری دەکات و<=دەیهێڵێتەوە.
One of the course's quiz programs counted the numbers “between 5 and 15” with an inner loop for j in range(5, 15): if a == j:. That works only for whole numbers from 5 to 14 and checks ten values for every input. A single chained comparison is simpler and works for any number:
یەکێک لە کویزی کۆرسەکە ژمارەکانی «نێوان 5 و 15»ـی بە ناوەکی دەژمارد: for j in range(5, 15): if a == j:. ئەوە تەنها بۆ لە 5 بۆ 14 کار دەکات و بۆ هەر دە دەپشکنێت. یەک سادەترە و بۆ هەر ژمارەیەک کار دەکات:
a = int(input("Enter an integer: "))
if 5 <= a < 15:
print(a, "is between 5 and 15 (15 not included).")
else:
print(a, "is outside.")Enter an integer: 12 12 is between 5 and 15 (15 not included). Enter an integer: 15 15 is outside.
An if inside an ifif لەناو ifـدا
A block can contain any statements — including another if. This is a nested if: the inner question is asked only after the outer one has chosen its branch. The course's Python and Java report shows it with clothing advice for the weather:
بلۆک دەتوانێت هەر لەخۆ بگرێت — تەنانەت ifـێکی تریش. ئەمە : پرسیارە ناوەکییەکە تەنها دوای ئەوە دەپرسرێت کە پرسیارە دەرەکییەکە لقەکەی هەڵبژارد. ڕاپۆرتی Python and Javaی کۆرسەکە ئەمە بە ئامۆژگاریی جلوبەرگ بەپێی کەشوهەوا پیشان دەدات:
An written inside the body of another if or else. The inner test runs only after the outer one has chosen its branch; when all the tests are about one value, an elif chain is often clearer.
کە لەناو جەستەی if یان elseـێکی تردا نووسراوە. پشکنینە ناوەکییەکە تەنها دوای ئەوە جێبەجێ دەبێت کە پشکنینە دەرەکییەکە ڕێگاکەی هەڵبژارد؛ کاتێک هەموو پشکنینەکان دەربارەی یەک نرخن، زنجیرەیەکی elif زۆرجار ڕوونترە.
if, and the inner diamonds are the nested ones.ئامۆژگاریی کەشوهەوا وەک دار: هەر ئەڵماسییەک ifـێکە و ئەڵماسییە ناوەکییەکان ئەوانەن کە لەناو یەکدان.CêITis_raining = True
is_snowing = False
temp = 30
if is_raining:
if temp > 20:
print("Wear something light.")
else:
print("Wear thick clothes.")
elif is_snowing:
if temp > 10:
print("The weather is not bad.")
else:
print("Look after your health.")
else:
print("It's a nice day!")Wear something light.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-3
Every name is defined before it is used — including
is_snowing.هەموو ناوێک پێش بەکارهێنان پێناسە کراوە — تەنانەت
is_snowing.- Lineدێڕی 5-9
Inside the
if is_raining:block is a second, innerif … elseabout the temperature. It is indented one more level.لەناو بلۆکی
if is_raining:ـداif … elseـێکی دووەمی ناوەکی هەیە دەربارەی پلەی گەرمی. ئاستێکی تر چووەتە ناوەوە.- Lineدێڕی 10-14
The
elifbranch has its own inner decision.لقی
elifبڕیاری ناوەکیی خۆی هەیە.
| Situationدۆخ | Messageپەیام |
|---|---|
| rain, above 20°باران، سەروو 20 پلە | Wear something light.شتێکی تەنک لەبەر بکە. |
| rain, 20° or lessباران، 20 پلە یان کەمتر | Wear thick clothes.جلی ئەستوور لەبەر بکە. |
| snow, above 10°بەفر، سەروو 10 پلە | The weather is not bad.کەشەکە خراپ نییە. |
| snow, 10° or lessبەفر، 10 پلە یان کەمتر | Look after your health.ئاگاداری تەندروستیت بە. |
| neitherهیچیان | It's a nice day!ڕۆژێکی خۆشە! |
In Kurdish: an if statement inside another if statement (nested if) in Python.
بە کوردی: لەناو تردا () لە .
Kurdish (Sorani), whole video (6:53): part 15 of MR. Ant's Python course.
ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە (6:53): بەشی 15 لە کۆرسی MR. Ant.
An if inside an ifif لەناو ifـدا
A bug hiding in a branchباگێک کە لە لقێکدا خۆی شاردووەتەوە
Here is the report's program as it was written, Kurdish messages and all. With isRaining = True it works:
ئەمە ڕاپۆرتەکەیە وەک نووسرابوو، لەگەڵ پەیامە کوردییەکانیشی. بە isRaining = True کار دەکات:
isRaining = True
temp = 30
if (isRaining):
if (temp > 20):
print("Ştêkî tenîk leber bike.")
else:
print("Cilêkî estûr leber bike.")
elif (isSnowing):
if (temp > 10):
print("Keşekey xirap nîye.")
else:
print("Agadarî tendrûstît be.")
else:
print("Rojêkî xweşe!")Ştêkî tenîk leber bike.
Now change the first line to isRaining = False and run it again:
ئێستا دێڕی یەکەم بگۆڕە بۆ isRaining = False و دووبارە جێبەجێی بکە:
isRaining = False
temp = 30
if (isRaining):
if (temp > 20):
print("Ştêkî tenîk leber bike.")
else:
print("Cilêkî estûr leber bike.")
elif (isSnowing):
if (temp > 10):
print("Keşekey xirap nîye.")
else:
print("Agadarî tendrûstît be.")
else:
print("Rojêkî xweşe!")Traceback (most recent call last):
File "weather.py", line 8, in <module>
elif (isSnowing):
^^^^^^^^^
NameError: name 'isSnowing' is not definedAn if inside an ifif لەناو ifـدا
Nested if, and, or elif?ifـی ناوەکی، and یان elif؟
Deep nesting is hard to read. The same weather advice can be written as one flat chain with and:
قووڵ خوێندنەوەیان قورسە. هەمان ئامۆژگاریی کەشوهەوا دەتوانرێت وەک یەک زنجیرەی ڕاست بە and بنووسرێت:
is_raining = False
is_snowing = True
temp = 5
if is_raining and temp > 20:
print("Wear something light.")
elif is_raining:
print("Wear thick clothes.")
elif is_snowing and temp > 10:
print("The weather is not bad.")
elif is_snowing:
print("Look after your health.")
else:
print("It's a nice day!")Look after your health.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5-8
elif is_raining:is reached only whenis_raining and temp > 20was False, so it means “raining, 20° or less”.تەنها کاتێک دەگەینە
elif is_raining:کەis_raining and temp > 20Falseبووبێت، بۆیە واتە «باران، 20 پلە یان کەمتر».
We checked both versions for every combination of rain, snow and temperatures from to : they always give the same message. Which one should you use?
هەردوو وەشانمان بۆ هەموو تێکەڵەیەکی باران، بەفر و پلەکانی گەرمی لە بۆ پشکنی: هەمیشە هەمان پەیام دەدەن. کامیان بەکار بهێنیت؟
andwhen two facts must both hold for one action:if age >= 18 and has_ticket:is clearer than two nestedifs.elifwhen one value is sorted into several cases (marks, ages, temperatures).- Nesting when the inner question only makes sense after the outer answer — or when the outer branch also does other work. Keep it to two or three levels.
andکاتێک دوو ڕاستی دەبێت پێکەوە ڕاست بن بۆ یەک کار:if age >= 18 and has_ticket:لە دووifـی ناوەکی ڕوونترە.elifکاتێک یەک بەسەر چەند حاڵەتێکدا دابەش دەکرێت (نمرە، تەمەن، پلەی گەرمی).- کاتێک پرسیارە ناوەکییەکە تەنها دوای وەڵامی دەرەکی واتای هەیە — یان کاتێک لقە دەرەکییەکە کاری تریش دەکات. لە دوو یان سێ ئاست زیاتر مەیبە.
Choosing a value in one lineهەڵبژاردنی نرخێک لە یەک دێڕدا
Sometimes an if … else only chooses between two values. Python has a short form for that, the conditional expression: value_if_true if condition else value_if_false. Other languages call this the ternary operator.
هەندێک جار if … else تەنها لە نێوان دوو هەڵدەبژێرێت. Python شێوەیەکی کورتی بۆ ئەمە هەیە، conditional expression: value_if_true if condition else value_if_false. زمانەکانی تر پێی دەڵێن .
A short form of if…else that gives one of two values: condition ? a : b in PHP, Java and C#. Python writes it a if condition else b and calls it a conditional expression.
شێوەیەکی کورتی if…elseـە کە یەکێک لە دوو نرخ دەدات: condition ? a : b لە PHP، Java و C#ـدا. Python بە a if condition else b دەینووسێت و پێی دەڵێت conditional expression.
n = 7
parity = "even" if n % 2 == 0 else "odd"
print(n, "is", parity)
age = 15
ticket = 1000 if age < 18 else 2000
print(f"Ticket: {ticket} dinars")7 is odd Ticket: 1000 dinars
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The condition
n % 2 == 0is checked first; the result is"even"if it is True, otherwise"odd".سەرەتا
n % 2 == 0دەپشکنرێت؛ ئەنجامەکە"even"ـە ئەگەرTrueبێت، ئەگەرنا"odd".- Lineدێڕی 6
Read it aloud: “ticket is 1000 if
age < 18, else 2000”.بە دەنگی بەرز بیخوێنەوە: «بلیت 1000ـە ئەگەر
age < 18، ئەگەرنا 2000».
Use it only for a simple choice between two values. If you need several statements or more than two cases, a normal if is easier to read. Java writes the same idea as age < 18 ? 1000 : 2000 — see Python next to Java.
تەنها بۆ هەڵبژاردنێکی سادە لە نێوان دوو بەکاری بهێنە. ئەگەر پێویستت بە چەند یان زیاتر لە دوو حاڵەت هەیە، ifـی ئاسایی خوێندنەوەی ئاسانترە. Java هەمان بیرۆکە بە age < 18 ? 1000 : 2000 دەنووسێت — بڕوانە Python لە تەنیشت Java.
Extra: matchزیاتر: match
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
if day == "Friday" or "Saturday":if day == "Friday" or "Saturday": | Always True.هەمیشە Trueـە. | day == "Friday" or day == "Saturday"day == "Friday" or day == "Saturday" |
&&, || or ! from other languages&&، || یان ! لە زمانەکانی ترەوە | SyntaxErrorSyntaxError | and, or, notand، or، not |
and and or mixed without bracketsand و or تێکەڵکراو بەبێ کەوانە | and is done first, maybe not what you meant.and یەکەم ئەنجام دەدرێت، لەوانەیە ئەوە نەبێت کە مەبەستت بوو. | Add parentheses.کەوانە زیاد بکە. |
Division before the zero check: 10 / x > 2 and x != 0دابەشکردن پێش پشکنینی سفر: 10 / x > 2 and x != 0 | ZeroDivisionErrorZeroDivisionError | Guard first: x != 0 and 10 / x > 2پاسەوان لە پێشەوە: x != 0 and 10 / x > 2 |
| A name used only in a rarely taken branchناوێک کە تەنها لە لقێکدا بەکار هاتووە کە کەم جێبەجێ دەبێت | NameError later, when that branch finally runs.دواتر NameError، کاتێک ئەو لقە سەرئەنجام جێبەجێ دەبێت. | Define every name at the top; test every branch.هەموو ناوێک لە سەرەوە پێناسە بکە؛ هەموو لقێک تاقی بکەرەوە. |
5 < a < 15 when 5 and 15 should count5 < a < 15 کاتێک 5 و 15 دەبێت بژمێردرێن | The ends are left out.کۆتاییەکان دەردەکرێن. | 5 <= a <= 155 <= a <= 15 |
Four or five levels of nested ifچوار یان پێنج ئاستی ifـی ناوەکی | Hard to read and to test.خوێندنەوە و تاقیکردنەوەی قورسە. | Join facts with and; sort cases with elif.ڕاستییەکان بە and ببەستەوە؛ حاڵەتەکان بە elif ڕیز بکە. |
Exercisesڕاهێنانەکان
A year is a leap year if it is divisible by 4 but not by 100 — unless it is also divisible by 400. Read a year and say whether it is a leap year. Try 2024, 1900, 2000 and 2026.
ساڵێک کەبیسەیە ئەگەر بەسەر 4دا دابەش ببێت بەڵام بەسەر 100دا نا — مەگەر بەسەر 400ـیشدا دابەش ببێت. ساڵێک بخوێنەوە و بڵێ ئایا کەبیسەیە. 2024، 1900، 2000 و 2026 تاقی بکەرەوە.
Hintئاماژە
“Divisible by 4” is year % 4 == 0. Combine three such tests with and, or and parentheses.
«بەسەر 4دا دابەش دەبێت» واتە year % 4 == 0. سێ تاقیکردنەوەی وا بە and، or و کەوانە پێکەوە ببەستە.
Show answerوەڵامەکە پیشان بدە
year = int(input("Year: "))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")Year: 2024 2024 is a leap year. Year: 1900 1900 is not a leap year. Year: 2000 2000 is a leap year. Year: 2026 2026 is not a leap year.
Three lengths make a triangle only if each pair of sides together is longer than the third side. Read three whole numbers and print the answer.
سێ درێژی تەنها کاتێک سێگۆشە دروست دەکەن کە کۆی هەر دوو لایەک لە لای سێیەم درێژتر بێت. سێ بخوێنەوە و وەڵامەکە چاپ بکە.
Show answerوەڵامەکە پیشان بدە
a = int(input("Side a: "))
b = int(input("Side b: "))
c = int(input("Side c: "))
if a + b > c and a + c > b and b + c > a:
print("These sides make a triangle.")
else:
print("These sides cannot make a triangle.")Side a: 3 Side b: 4 Side c: 5 These sides make a triangle. Side a: 1 Side b: 2 Side c: 8 These sides cannot make a triangle.
Work out each line by hand, using the order not, and, or and the truthy/falsy rules. Then run it.
هەر دێڕێک بە دەست هەژمار بکە، بە بەکارهێنانی ڕیزبەندیی not، and، or و یاساکانی truthy/falsy. پاشان جێبەجێی بکە.
print(5 > 3 and 2 > 4 or not False)
print(not (5 > 3 and 2 > 4))
print(0 or "" or "Kurdistan")
print(1 < 3 > 2)Show answerوەڵامەکە پیشان بدە
print(5 > 3 and 2 > 4 or not False)
print(not (5 > 3 and 2 > 4))
print(0 or "" or "Kurdistan")
print(1 < 3 > 2)True True Kurdistan True
- Line 1:
(True and False) or (not False)becomesFalse or True, which isTrue. - Line 2:
not (True and False)becomesnot False, which isTrue. - Line 3:
0and""count as False, soormoves on and gives"Kurdistan". - Line 4: a chain means
1 < 3 and 3 > 2, which is True. It does not compare 1 with 2.
- دێڕی 1:
(True and False) or (not False)دەبێتەFalse or True، کەTrueـە. - دێڕی 2:
not (True and False)دەبێتەnot False، کەTrueـە. - دێڕی 3:
0و""وەکFalseدادەنرێن، بۆیەorدەڕوات و"Kurdistan"دەدات. - دێڕی 4: زنجیرە واتە
1 < 3 and 3 > 2کەTrueـە. 1 لەگەڵ 2 بەراورد ناکات.
A museum ticket costs 4,000 dinars. Students and people aged 60 or more pay half. Read the age and whether the visitor is a student (yes/no), then print the price.
بلیتی مۆزەخانەیەک 4,000 دینارە. قوتابیان و کەسانی تەمەن 60 یان زیاتر نیوەی دەدەن. تەمەن و ئەوەی ئایا سەردانکەر قوتابییە (yes/no) بخوێنەوە، پاشان نرخەکە چاپ بکە.
Show answerوەڵامەکە پیشان بدە
age = int(input("Age: "))
is_student = input("Student? (yes/no): ") == "yes"
price = 4000
if is_student or age >= 60:
price = price // 2
print("Price:", price, "dinars")Age: 20 Student? (yes/no): yes Price: 2000 dinars Age: 35 Student? (yes/no): no Price: 4000 dinars Age: 65 Student? (yes/no): no Price: 2000 dinars
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The comparison already gives True or False, so
is_studentis a bool.بەراوردەکە خۆی
TrueیانFalseدەدات، بۆیەis_studentboolـە.
ifRewrite this code with a single if and the same behaviour:
ئەم کۆدە بە یەک if و هەمان ڕەفتار دووبارە بنووسەوە:
age = 20
has_ticket = True
if age >= 18:
if has_ticket:
print("You may enter.")Show answerوەڵامەکە پیشان بدە
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You may enter.")You may enter.
Exercisesڕاهێنانەکان
Exercises: write and read conditionsڕاهێنان: مەرجەکان بنووسە و بخوێنەوە
Each of these three conditions is False. Add one pair of parentheses to each so that it becomes True: 0 == 1 == 2, 2 + 3 == 4 + 5 == 7 and 1 < -1 == 3 > 4.
هەر یەک لەم سێ Falseـە. یەک جووت کەوانە بۆ هەر یەکێکیان زیاد بکە تا ببێتە True: 0 == 1 == 2، 2 + 3 == 4 + 5 == 7 و 1 < -1 == 3 > 4.
Hintئاماژە
Without parentheses each line is a chained comparison. Inside arithmetic or a comparison with a number, True counts as 1 and False as 0.
بەبێ کەوانە هەر دێڕێک . لەناو هەژمارکردن یان بەراوردکردن لەگەڵ ژمارەدا، True وەک 1 و False وەک 0 دادەنرێت.
Show answerوەڵامەکە پیشان بدە
print(0 == 1 == 2)
print(0 == (1 == 2))
print(2 + 3 == 4 + 5 == 7)
print(2 + (3 == 4) + 5 == 7)
print(1 < -1 == 3 > 4)
print((1 < -1) == (3 > 4))False True False True False True
0 == 1 == 2means0 == 1 and 1 == 2:False. In0 == (1 == 2)the inner test isFalse, which counts as 0, and0 == 0isTrue.2 + 3 == 4 + 5 == 7means5 == 9 and 9 == 7:False. In2 + (3 == 4) + 5 == 7,(3 == 4)isFalse(0), so the left side is :True.1 < -1 == 3 > 4means1 < -1 and -1 == 3 and 3 > 4:False.(1 < -1) == (3 > 4)comparesFalsewithFalse:True.
0 == 1 == 2واتە0 == 1 and 1 == 2:False. لە0 == (1 == 2)ـدا تاقیکردنەوە ناوەکییەکەFalseـە کە وەک 0 دادەنرێت و0 == 0Trueـە.2 + 3 == 4 + 5 == 7واتە5 == 9 and 9 == 7:False. لە2 + (3 == 4) + 5 == 7ـدا(3 == 4)Falseـە (0)، بۆیە لای چەپ :True.1 < -1 == 3 > 4واتە1 < -1 and -1 == 3 and 3 > 4:False.(1 < -1) == (3 > 4)FalseلەگەڵFalseبەراورد دەکات:True.
Start with num1 = 5, num2 = 8, num3 = 24 and stationery = [] (an empty list: a list with nothing in it — lists come later in the course). Write each sentence below as one Python condition, guess whether it is True or False, then check with print().
بە num1 = 5، num2 = 8، num3 = 24 و stationery = [] دەست پێ بکە ( بەتاڵ: کە هیچی تێدا نییە — دواتر لە کۆرسەکەدا دێن). هەر ڕستەیەکی خوارەوە وەک یەک Python بنووسە، پێشبینی بکە ئایا Trueـە یان False، پاشان بە print() بیپشکنە.
- 20 plus is less than 12.
num3is not more than 24.- 6.75 lies between
num1andnum2. - The word
"middle"comes between"first"and"last"in alphabetical order. - The list
stationeryis empty.
- 20 کۆ لە 12 کەمترە.
num3لە 24 زیاتر نییە.- 6.75 دەکەوێتە نێوان
num1وnum2. - وشەی
"middle"بەپێی ڕیزبەندیی ئەلفوبێیی دەکەوێتە نێوان"first"و"last". -
stationeryبەتاڵە.
Show answerوەڵامەکە پیشان بدە
num1 = 5
num2 = 8
num3 = 24
stationery = []
print(20 + (-10) < 12)
print(not num3 > 24)
print(num1 < 6.75 < num2)
print("first" < "middle" < "last")
print(not stationery)True True True False True
20 + (-10) < 12:10 < 12isTrue.not num3 > 24(or simplynum3 <= 24): 24 is not more than 24, soTrue.num1 < 6.75 < num2: a chained comparison,5 < 6.75 < 8,True."first" < "middle" < "last"isFalse: text is compared letter by letter, andmcomes afterl, so"middle"comes after"last"(Working with strings explains string order).not stationery: an empty list counts asFalse(see values that count as True or False), sonotgivesTrue.
20 + (-10) < 12:10 < 12Trueـە.not num3 > 24(یان بە سادەییnum3 <= 24): 24 لە 24 زیاتر نییە، بۆیەTrue.num1 < 6.75 < num2: ،5 < 6.75 < 8،True."first" < "middle" < "last"Falseـە: دەق پیت بە پیت بەراورد دەکرێت وmدوایlدێت، بۆیە"middle"دوای"last"دێت (کارکردن لەگەڵ زنجیرەنووسەکان ڕیزبەندیی ڕوون دەکاتەوە).not stationery: بەتاڵ وەکFalseدادەنرێت (بڕوانە ئەو نرخانەی وەکTrueیانFalseدادەنرێن)، بۆیەnotTrueدەدات.
Summaryپوختە
andis True only if both sides are True;orif at least one is;notflips a value.- Order: arithmetic, comparisons,
not,and,or— use parentheses when you mix them. - Short-circuit: Python stops as soon as the answer is known, so put a guard such as
x != 0on the left. 5 <= a < 15is a chained comparison:5 <= a and a < 15.- A nested
ifasks an inner question inside a branch; preferandorelifwhen they are clearer, and test every branch. a if condition else bchooses between two values in one line.
andتەنها کاتێکTrueـە کە هەردوو لاTrueبن؛orئەگەر لانیکەم یەکێکیان؛notپێچەوانە دەکاتەوە.- ڕیزبەندی: هەژمار، بەراورد،
not،and،or— کاتێک تێکەڵیان دەکەیت کەوانە بەکار بهێنە. - : Python هەر کە وەڵامەکە زانرا دەوەستێت، بۆیە پاسەوانێکی وەک
x != 0لە لای چەپ دابنێ. 5 <= a < 15:5 <= a and a < 15.- پرسیارێکی ناوەکی لەناو لقێکدا دەپرسێت؛ کاتێک
andیانelifڕوونترن ئەوان هەڵبژێرە و هەموو لقێک تاقی بکەرەوە. a if condition else bلە یەک دێڕدا لە نێوان دوو هەڵدەبژێرێت.
Why is day == "Friday" or "Saturday" always True?بۆچی day == "Friday" or "Saturday" هەمیشە Trueـە؟
The right side is the non-empty string "Saturday", which counts as True. Write day == "Saturday" on the right.
لای ڕاست نابەتاڵی "Saturday"ـە کە وەک True دادەنرێت. لە لای ڕاست بنووسە day == "Saturday".
What does short-circuit evaluation mean?هەڵسەنگاندنی کورتبڕ واتای چییە؟
Python skips the right side of and when the left is False, and of or when the left is True.
Python لای ڕاستی and بەجێ دەهێڵێت کاتێک لای چەپ False بێت؛ هی orـیش کاتێک لای چەپ True بێت.
Why did the weather program crash only when it did not rain?بۆچی بەرنامەی کەشوهەوا تەنها کاتێک باران نەدەباری دەکەوت؟
isSnowing was never defined, and Python only evaluates the elif when the first condition is False.
isSnowing هەرگیز پێناسە نەکرابوو و Python تەنها کاتێک elif هەژمار دەکات کە یەکەم False بێت.
Write “even” or “odd” for n in one line.لە یەک دێڕدا بۆ n «even» یان «odd» بنووسە.
parity = "even" if n % 2 == 0 else "odd"
parity = "even" if n % 2 == 0 else "odd"
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
One-line conditional expressions, the logical operators and, or, not, short-circuit evaluation and chained comparisons like 18 <= age < 65.
لە یەک دێڕدا، and، or و not، و وەک 18 <= age < 65.
Plays 4:14-14:00: ternary operator, logical operators, short-circuit evaluation, chaining comparison operators.
لە 4:14 تا 14:00 پەخش دەبێت: ، ، و .
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Working with Stringsکارکردن لەگەڵ زنجیرەنووسەکان
Related lecturesوانە پەیوەندیدارەکان
- Logic Gates, Truth Tables and the NOT Gateگەیتە لۆجیکییەکان، خشتەکانی ڕاستی و گەیتی NOT
- The OR Gateگەیتی OR
- The AND Gateگەیتی AND
- Propositional Logic and Knowledge Basesلۆجیکی گوزارەیی و بنکەکانی زانین
- Python Next to JavaPython لە تەنیشت Javaـەوە
- Conditional Statements: if, else, elseif and switchڕستە مەرجییەکان: if و else و elseif و switch
- Project: Binary to Decimal Converterپڕۆژیلە: گۆڕینی دووانی بۆ دەیی
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python Language Reference 6.10–6.13: comparisons, Boolean operations, conditional expressions
- Python Tutorial 5.7: More on Conditions (and/or/not, chaining, short-circuit)
- Python Library: Built-in types — truth value testing and Boolean operations
- Python Tutorial 4.7: match Statements
- Harvard CS50's Introduction to Programming with Python, Lecture 1 notes (or, and, match)
- PEP 8 — Style Guide for Python Code (naming, indentation)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.