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.

18 min read18 خولەک خوێندنەوە · 20 stepsهەنگاو

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

  • Combine conditions with and, or and not, 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 if statements, 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.

Definitionپێناسە

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ـی هەیە بە پێشینەیی نزمتر).

Python's three logical operatorsسێ Python
OperatorMeaningواتاTrue when…Trueـە کاتێک…
A and BA and BA و Bboth sides are Trueهەردوو لا True بن
A or BA or B (or both)A یان B (یان هەردووکیان)at least one side is Trueلانیکەم یەک لا True بێت
not Athe opposite of Aپێچەوانەی AA is FalseA False بێت
Logical operators in Python are easy 🔣Bro Code · Englishئینگلیزی · 214K viewsبینین · 4.6K likesلایک

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⁩٪ هەیە.

Passing a courseدەرچوون لە کۆرسێکand.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Both comparisons are worked out first; then and joins their two answers.

سەرەتا هەردوو بەراوردەکە هەژمار دەکرێن؛ پاشان and دوو وەڵامەکەیان پێکەوە دەبەستێت.

Truth table of and and
ABA and B
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue
Quick checkپشکنینی خێراmark = 80, attendance = 70. What is mark >= 50 and attendance >= 75?mark = 80، attendance = 70. mark >= 50 and attendance >= 75 چییە؟

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

Is it the weekend?ئایا پشووی کۆتایی هەفتەیە؟or.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
day = input("Day: ")
if day == "Friday" or day == "Saturday":
    print("Weekend!")
else:
    print("School day.")
Truth table of or or
ABA or B
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue
Quick checkپشکنینی خێراWhich values make A or B False?کام A or B دەکەن بە False؟

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 کلاسیکییە:

Monday is the weekend?!دووشەممە پشووە؟!or_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
day = input("Day: ")
if day == "Friday" or "Saturday":
    print("Weekend!")
else:
    print("School day.")
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 لەگەڵ و دەناسیت.)

Quick checkپشکنینی خێراWhich condition correctly checks for 1 or 2?کام بە دروستی 1 یان 2 دەپشکنێت؟

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

Flipping a conditionپێچەوانەکردنەوەی مەرجnot.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
is_raining = False
if not is_raining:
    print("No umbrella needed.")
print(not True)
print(not 3 > 5)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

not 3 > 5 means not (3 > 5): comparisons are worked out before not.

not 3 > 5 واتە not (3 > 5): بەراوردەکان پێش not هەژمار دەکرێن.

Truth table of not not
Anot A
FalseTrue
TrueFalse
Quick checkپشکنینی خێراis_open = True. What does print(not is_open) show?is_open = True. print(not is_open) چی پیشان دەدات؟

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

Animation: two switches, A and B, go through the four combinations False/False, False/True, True/False and True/True. Two lamps show the results: the lamp for A and B lights only in the last row; the lamp for A or B lights in every row except the first. A truth table below fills in one row per frame.
and needs both switches on; or needs at least one.and پێویستی بە هەردوو داگیرساوە؛ or پێویستی بە لانیکەم یەکێکیانە.CêIT
The complete truth table تەواو
ABA and BA or Bnot A
FalseFalseFalseFalseTrue
FalseTrueFalseTrueTrue
TrueFalseFalseTrueFalse
TrueTrueTrueTrueFalse

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

Quick checkپشکنینی خێراIn how many of the four rows is A and B True?لە چەند ڕیز لە چوار ڕیزەکەدا A and B Trueـە؟

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 before orand پێش 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)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

and first: False and False is False; then True or False is True.

یەکەم and: False and False دەکاتە False؛ پاشان True or False دەکاتە True.

Lineدێڕی 2

Parentheses change the order and the answer.

کەوانە ڕیزبەندی و وەڵامەکە دەگۆڕێت.

Lineدێڕی 5

not applies only to age > 18, not to the whole line: (not True) and True is False.

not تەنها بەسەر age > 18ـدا دەچێت، نەک هەموو دێڕەکە: (not True) and True دەکاتە False.

Quick checkپشکنینی خێراWhat is False and True or True?False and True or True چییە؟

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 بێت، لای ڕاست بەجێ دەهێڵرێت. ئەمە پێی دەوترێت و ڕێگە دەدات لای چەپ لای ڕاست بپارێزێت:

Definitionپێناسە

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#⁩ هەموویان ئەمە دەکەن.

A guard on the leftپاسەوانێک لە لای چەپshort_ok.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

x != 0 is False, so Python never computes 10 / x: no division by zero.

x != 0 Falseـە، بۆیە Python هەرگیز 10 / x هەژمار ناکات: هیچ دابەشکردنێک بەسەر سفردا نییە.

The same test in the wrong orderهەمان تاقیکردنەوە بە ڕیزبەندیی هەڵەshort_crash.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 0
if 10 / x > 2 and x != 0:
    print("10 / x is more than 2")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Now the division comes first and crashes before x != 0 is ever checked.

ئێستا دابەشکردنەکە یەکەم دێت و پێش ئەوەی x != 0 بپشکنرێت دەکەوێت.

Quick checkپشکنینی خێراitems = 0. Which condition is safe?items = 0. کام سەلامەتە؟

Python stops as soon as it knowsPython هەر کە زانی دەوەستێت

Extra: values that count as True or Falseزیاتر: ئەو نرخانەی وەک True یان False دادەنرێن

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

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 تەنها یەک جار هەژمار دەکرێت.

Definitionپێناسە

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#⁩ نا.

Chained comparisonsبەراوردی زنجیرەیی لە کارداchained.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = 12
print(5 <= a < 15)
a = 15
print(5 <= a < 15)
score = 95
print(90 <= score <= 100)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

15 < 15 is False, so 15 is outside 5 <= a < 15. Decide whether your range includes its end: < leaves it out, <= keeps it in.

15 < 15 Falseـە، بۆیە ⁦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⁩ کار دەکات و بۆ هەر دە دەپشکنێت. یەک سادەترە و بۆ هەر ژمارەیەک کار دەکات:

One test instead of a loopیەک تاقیکردنەوە لەبری خولێکq1_fix.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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.")
Quick checkپشکنینی خێراWhich values make 10 <= n <= 20 True?کام 10 <= n <= 20 دەکەن بە True؟

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⁩ی کۆرسەکە ئەمە بە ئامۆژگاریی جلوبەرگ بەپێی کەشوهەوا پیشان دەدات:

Definitionپێناسە

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 زۆرجار ڕوونترە.

Decision tree. First question: is_raining? If True, ask temp > 20: True gives Wear something light, False gives Wear thick clothes. If is_raining is False, ask is_snowing? If True, ask temp > 10: True gives The weather is not bad, False gives Look after your health. If is_snowing is False: It's a nice day!دارێکی بڕیار. پرسیاری یەکەم: is_raining? ئەگەر True بێت، بپرسە temp > 20: True دەبێتە «شتێکی تەنک لەبەر بکە»، False دەبێتە «جلی ئەستوور لەبەر بکە». ئەگەر is_raining False بێت، بپرسە is_snowing? ئەگەر True بێت، بپرسە temp > 10: True دەبێتە «کەشەکە خراپ نییە»، False دەبێتە «ئاگاداری تەندروستیت بە». ئەگەر is_snowing False بێت: «ڕۆژێکی خۆشە!»
The weather advice as a tree: each diamond is one if, and the inner diamonds are the nested ones.ئامۆژگاریی کەشوهەوا وەک دار: هەر ئەڵماسییەک ifـێکە و ئەڵماسییە ناوەکییەکان ئەوانەن کە لەناو یەکدان.CêIT
Nested ifs: weather adviceڕستەکانی ifـی ناوەکی: ئامۆژگاریی کەشوهەواweather.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
is_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!")
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, inner if … else about the temperature. It is indented one more level.

لەناو بلۆکی if is_raining:ـدا if … elseـێکی دووەمی ناوەکی هەیە دەربارەی پلەی گەرمی. ئاستێکی تر چووەتە ناوەوە.

Lineدێڕی 10-14

The elif branch has its own inner decision.

لقی elif بڕیاری ناوەکیی خۆی هەیە.

The five messages (the report wrote them in Kurdish with Latin letters, e.g. “Rojêkî xweşe!”)پێنج پەیامەکە (ڕاپۆرتەکە بە کوردی و بە پیتی لاتینی نووسیبوونی، بۆ نموونە «Rojêkî xweş⁦e!⁩»)
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!ڕۆژێکی خۆشە!
Quick checkپشکنینی خێراIn the weather program, when is temp > 10 checked?لە کەشوهەوادا، temp > 10 کەی دەپشکنرێت؟

15- ئه گه ر له ناو ئه گه ر! Nested If StatementsMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 6.5K viewsبینین · 225 likesلایک

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 کار دەکات:

The report's versionوەشانی ڕاپۆرتەکەweather.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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!")

Now change the first line to isRaining = False and run it again:

ئێستا دێڕی یەکەم بگۆڕە بۆ isRaining = False و دووبارە جێبەجێی بکە:

One change: it is not rainingیەک گۆڕان: باران نابارێتweather.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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!")
Quick checkپشکنینی خێراWhy did the report's program run without an error when isRaining was True?بۆچی ڕاپۆرتەکە کاتێک isRaining True بوو بەبێ کاری کرد؟

An 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 بنووسرێت:

The same advice without nestingهەمان ئامۆژگاری بەبێ ڕستەی ifـی ناوەکیweather_flat.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5-8

elif is_raining: is reached only when is_raining and temp > 20 was False, so it means “raining, 20° or less”.

تەنها کاتێک دەگەینە elif is_raining: کە is_raining and temp > 20 False بووبێت، بۆیە واتە «باران، ⁦20⁩ پلە یان کەمتر».

We checked both versions for every combination of rain, snow and temperatures from −10∘-10^\circ to 39∘39^\circ: they always give the same message. Which one should you use?

هەردوو وەشانمان بۆ هەموو تێکەڵەیەکی باران، بەفر و پلەکانی گەرمی لە −10∘-10^\circ بۆ 39∘39^\circ پشکنی: هەمیشە هەمان پەیام دەدەن. کامیان بەکار بهێنیت؟

  • and when two facts must both hold for one action: if age >= 18 and has_ticket: is clearer than two nested ifs.
  • elif when 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 کاتێک یەک بەسەر چەند حاڵەتێکدا دابەش دەکرێت (نمرە، تەمەن، پلەی گەرمی).
  • کاتێک پرسیارە ناوەکییەکە تەنها دوای وەڵامی دەرەکی واتای هەیە — یان کاتێک لقە دەرەکییەکە کاری تریش دەکات. لە دوو یان سێ ئاست زیاتر مەیبە.
Quick checkپشکنینی خێراWhich is the clearest way to write “enter only if 18 or older and holding a ticket”?ڕوونترین ڕێگا بۆ نووسینی «تەنها ئەگەر ⁦18⁩ ساڵ یان زیاتر بێت و بلیتی هەبێت بچێتە ژوورەوە» کامیانە؟

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. زمانەکانی تر پێی دەڵێن .

Definitionپێناسە

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⁩.

Two values, one lineدوو نرخ، یەک دێڕcond_expr.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The condition n % 2 == 0 is 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.

Quick checkپشکنینی خێراscore = 45. What is "pass" if score >= 50 else "fail"?score = 45. "pass" if score >= 50 else "fail" چییە؟

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&&، || یان ! لە زمانەکانی ترەوەSyntaxErrorSyntaxErrorand, 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 != 0ZeroDivisionErrorZeroDivisionErrorGuard 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ڕاهێنانەکان

ExerciseڕاهێنانLeap yearsساڵی کەبیسە

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وەڵامەکە پیشان بدە
ex_leap.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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.")
ExerciseڕاهێنانCan these sides make a triangle?ئایا ئەم لایانە سێگۆشە دروست دەکەن؟

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وەڵامەکە پیشان بدە
ex_triangle.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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.")
ExerciseڕاهێنانPredict the outputدەرخستە پێشبینی بکە

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. پاشان جێبەجێی بکە.

PYTHON
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وەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
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)
  • Line 1: (True and False) or (not False) becomes False or True, which is True.
  • Line 2: not (True and False) becomes not False, which is True.
  • Line 3: 0 and "" count as False, so or moves 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 بەراورد ناکات.
ExerciseڕاهێنانHalf priceنیوە نرخ

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وەڵامەکە پیشان بدە
ex_discount.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The comparison already gives True or False, so is_student is a bool.

بەراوردەکە خۆی True یان False دەدات، بۆیە is_student boolـە.

ExerciseڕاهێنانFlatten the nestingڕستەی ifـی ناوەکی بکە بە یەک if

Rewrite this code with a single if and the same behaviour:

ئەم کۆدە بە یەک if و هەمان ڕەفتار دووبارە بنووسەوە:

PYTHON
age = 20
has_ticket = True
if age >= 18:
    if has_ticket:
        print("You may enter.")
Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = 20
has_ticket = True
if age >= 18 and has_ticket:
    print("You may enter.")
Quick checkپشکنینی خێراx = 7. Which condition is True?x = 7. کام Trueـە؟

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

Exercises: write and read conditionsڕاهێنان: مەرجەکان بنووسە و بخوێنەوە

ExerciseڕاهێنانWhere do the parentheses go?کەوانەکان لە کوێ دابنرێن؟

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وەڵامەکە پیشان بدە
PYTHON
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))
  • 0 == 1 == 2 means 0 == 1 and 1 == 2: False. In 0 == (1 == 2) the inner test is False, which counts as 0, and 0 == 0 is True.
  • 2 + 3 == 4 + 5 == 7 means 5 == 9 and 9 == 7: False. In 2 + (3 == 4) + 5 == 7, (3 == 4) is False (0), so the left side is 2+0+5=72 + 0 + 5 = 7: True.
  • 1 < -1 == 3 > 4 means 1 < -1 and -1 == 3 and 3 > 4: False. (1 < -1) == (3 > 4) compares False with False: True.
  • 0 == 1 == 2 واتە 0 == 1 and 1 == 2: False. لە 0 == (1 == 2)ـدا تاقیکردنەوە ناوەکییەکە Falseـە کە وەک 0 دادەنرێت و 0 == 0 Trueـە.
  • 2 + 3 == 4 + 5 == 7 واتە 5 == 9 and 9 == 7: False. لە 2 + (3 == 4) + 5 == 7ـدا (3 == 4) Falseـە ⁦(0)⁩، بۆیە لای چەپ 2+0+5=72 + 0 + 5 = 7: True.
  • 1 < -1 == 3 > 4 واتە 1 < -1 and -1 == 3 and 3 > 4: False. (1 < -1) == (3 > 4) False لەگەڵ False بەراورد دەکات: True.
ExerciseڕاهێنانWrite the conditionمەرجەکە بنووسە

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() بیپشکنە.

  1. 20 plus −10-10 is less than 12.
  2. num3 is not more than 24.
  3. 6.75 lies between num1 and num2.
  4. The word "middle" comes between "first" and "last" in alphabetical order.
  5. The list stationery is empty.
  1. ⁦20⁩ کۆ −10-10 لە ⁦12⁩ کەمترە.
  2. num3 لە ⁦24⁩ زیاتر نییە.
  3. ⁦6.75⁩ دەکەوێتە نێوان num1 و num2.
  4. وشەی "middle" بەپێی ڕیزبەندیی ئەلفوبێیی دەکەوێتە نێوان "first" و "last".
  5. stationery بەتاڵە.
Show answerوەڵامەکە پیشان بدە
PYTHON
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)
  1. 20 + (-10) < 12: 10 < 12 is True.
  2. not num3 > 24 (or simply num3 <= 24): 24 is not more than 24, so True.
  3. num1 < 6.75 < num2: a chained comparison, 5 < 6.75 < 8, True.
  4. "first" < "middle" < "last" is False: text is compared letter by letter, and m comes after l, so "middle" comes after "last" (Working with strings explains string order).
  5. not stationery: an empty list counts as False (see values that count as True or False), so not gives True.
  1. 20 + (-10) < 12: 10 < 12 Trueـە.
  2. not num3 > 24 (یان بە سادەیی num3 <= 24): ⁦24⁩ لە ⁦24⁩ زیاتر نییە، بۆیە True.
  3. num1 < 6.75 < num2: ، 5 < 6.75 < 8، True.
  4. "first" < "middle" < "last" Falseـە: دەق پیت بە پیت بەراورد دەکرێت و m دوای l دێت، بۆیە "middle" دوای "last" دێت (کارکردن لەگەڵ زنجیرەنووسەکان ڕیزبەندیی ڕوون دەکاتەوە).
  5. not stationery: بەتاڵ وەک False دادەنرێت (بڕوانە ئەو نرخانەی وەک True یان False دادەنرێن)، بۆیە not True دەدات.
Quick checkپشکنینی خێراWhat does print(True + True) show?print(True + True) چی پیشان دەدات؟

Summaryپوختە

  • and is True only if both sides are True; or if at least one is; not flips 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 != 0 on the left.
  • 5 <= a < 15 is a chained comparison: 5 <= a and a < 15.
  • A nested if asks an inner question inside a branch; prefer and or elif when they are clearer, and test every branch.
  • a if condition else b chooses 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 لە یەک دێڕدا لە نێوان دوو هەڵدەبژێرێت.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Control Flow in Python - If Elif Else StatementsProgramming with Mosh · Englishئینگلیزی · 401K viewsبینین · 6.7K likesلایک · Partبەشی 4:14–14:00 (ofلە 16:08)

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

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

Working with Stringsکارکردن لەگەڵ زنجیرەنووسەکان

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

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