נבנה על ידיBuilt by Ariel Yohimovich
if תנאי: ואז בלוק מוזחif condition: then an indented blockif בודק תנאי. אם הוא True, הבלוק המוזח שאחריו רץ; אם לא, Python מדלגת עליו. ההזחה (4 רווחים) היא מה שמסמן איפה הבלוק מתחיל ונגמר.if checks a condition. If it's True, the indented block after it runs; if not, Python skips it. The indentation (4 spaces) marks where the block starts and ends.pages = int(input("כמה עמודים? "))
if pages > 500:
print("ספר עבה!")
print("כדאי לפנות סופ״ש")
print("בהצלחה בקריאה")pages = int(input("How many pages? "))
if pages > 500:
print("A thick book!")
print("Clear a weekend")
print("Happy reading")pages = int(input("כמה עמודים? "))
if pages > 500:
print("ספר עבה!")
print("כדאי לפנות סופ״ש")
print("בהצלחה בקריאה")pages = int(input("How many pages? "))
if pages > 500:
print("A thick book!")
print("Clear a weekend")
print("Happy reading")כמה עמודים? 640 ספר עבה! כדאי לפנות סופ״ש בהצלחה בקריאה
How many pages? 640 A thick book! Clear a weekend Happy reading
if הוא כמו שומר בכניסה למועדון: בודק תנאי (גיל), ורק מי שעומד בו נכנס. השאר ממשיכים הלאה ברחוב.if is like a bouncer at a club door: checks a condition (age) and only those who pass go in. Everyone else carries on down the street.pages = 640
if pages > 500
print("Thick")SyntaxError: expected ':'
SyntaxError: expected ':': כל if, elif, else נגמרים בנקודתיים.SyntaxError: expected ':': every if, elif, else ends with a colon.else כשאף תנאי לא התקייםelse when no condition heldelif בודק תנאי נוסףelif checks another conditionelse רץ כשהתנאי לא התקיים. elif (קיצור של else if) מוסיף תנאים נוספים. Python בודקת מלמעלה למטה ומריצה את המסלול הראשון שמתאים, ורק אותו.else runs when the condition didn't hold. elif (short for else if) adds more conditions. Python checks top to bottom and runs the first matching path, and only that one.score = int(input("ציון: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
else:
print("נכשל")score = int(input("Score: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
else:
print("Fail")score = int(input("ציון: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
else:
print("נכשל")score = int(input("Score: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
else:
print("Fail")ציון: 84 B
Score: 84 B
x = 5
if x > 10:
print("big")
else x > 3:
print("medium")SyntaxError: expected ':'
SyntaxError: ל-else אין תנאי. תנאי נוסף כותבים עם elif.SyntaxError: else takes no condition. Another condition uses elif.else עם תנאי.Writing a condition after else.== != < > <= >=== != < > <= >== משייך, == משווה= assigns, == compares1 < x < 10You can chain: 1 < x < 10= (השמה) שונה מ-== (שאלה: האם שווים?). אפשר גם להשוות מחרוזות: "apple" < "banana" לפי סדר מילוני.Comparison operators return True or False. Most important: = (assignment) is not == (asking: are they equal?). Strings compare too: "apple" < "banana" in dictionary order.age = 16
print(age == 16, age != 16)
print(13 <= age < 18)
print("apple" < "banana")
print("Zebra" < "apple")age = 16
print(age == 16, age != 16)
print(13 <= age < 18)
print("apple" < "banana")
print("Zebra" < "apple")True False True True True
= הוא "שים את זה בקופסה". == הוא "האם מה שבקופסה זהה לזה?". פעולה מול שאלה.= is "put this in the box". == is "is what's in the box the same as this?". An action versus a question.age = 16
if age = 16:
print("16!")SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
SyntaxError: Python אפילו שואלת אם התכוונתם ל-==. בתנאי משווים עם ==.SyntaxError: Python even asks whether you meant ==. Conditions compare with ==.= בתוך תנאי.Using = inside a condition.and: שניהם נכוניםand: both trueor: לפחות אחדor: at least onenot: הופךnot: flipsand נכון רק אם שני הצדדים נכונים, or אם לפחות אחד, ו-not הופך True ל-False ולהפך.Logical operators combine conditions. and is true only if both sides are, or if at least one is, and not flips True and False.and הוא כמו כספת עם שני מפתחות: צריך את שניהם. or הוא כמו בית עם שתי דלתות: מספיקה אחת.and is like a safe with two keys: you need both. or is like a house with two doors: one is enough.x = 5
if x == 1 or x = 2:
print("one or two")SyntaxError: invalid syntax
SyntaxError: כל צד צריך תנאי שלם עם ==: x == 1 or x == 2, או x in (1, 2).SyntaxError: each side needs a full condition with ==: x == 1 or x == 2, or x in (1, 2).x == 1 or 2 במקום שני תנאים מלאים. זה לא נופל, אבל תמיד True!Writing x == 1 or 2 instead of two full conditions. It doesn't crash, but it's always True!0, "", None הם falsy0, "", None are falsyif name: = "אם יש שם"if name: = "if there is a name"0, "", None, רשימה ריקה) נחשבים falsy, וכל השאר truthy. ככה בודקים בקצרה אם המשתמש הקליד משהו.A condition doesn't need an explicit True/False. "Empty" values (0, "", None, an empty list) are falsy, everything else truthy. That's the short way to check whether the user typed something.name = input("שם (אפשר ריק): ")
if name:
print(f"שלום {name}")
else:
print("שלום, אורח/ת")name = input("Name (can be empty): ")
if name:
print(f"Hi {name}")
else:
print("Hi, guest")name = input("שם (אפשר ריק): ")
if name:
print(f"שלום {name}")
else:
print("שלום, אורח/ת")name = input("Name (can be empty): ")
if name:
print(f"Hi {name}")
else:
print("Hi, guest")שם (אפשר ריק): שלום, אורח/ת
Name (can be empty): Hi, guest
and עדיף על קינוןOften and beats nestingif בתוך if, וכל רמה מוזחת עוד 4 רווחים. הזחה היא חלק מהתחביר: היא קובעת לאיזה בלוק שייכת כל שורה. כשהקינון עמוק, כדאי לחבר תנאים עם and.You can put an if inside an if, each level indented 4 more spaces. Indentation is part of the syntax: it decides which block each line belongs to. When nesting gets deep, combine conditions with and.member = True
books = 3
if member:
if books < 5:
print("אפשר לשאול עוד ספר")
else:
print("הגעת למקסימום")
else:
print("צריך להירשם")member = True
books = 3
if member:
if books < 5:
print("You can borrow another")
else:
print("You reached the limit")
else:
print("Please sign up")member = True
books = 3
if member:
if books < 5:
print("אפשר לשאול עוד ספר")
else:
print("הגעת למקסימום")
else:
print("צריך להירשם")member = True
books = 3
if member:
if books < 5:
print("You can borrow another")
else:
print("You reached the limit")
else:
print("Please sign up")אפשר לשאול עוד ספר
You can borrow another
pages = 600
if pages > 500:
print("thick")IndentationError: expected an indented block after 'if' statement on line 2
IndentationError: expected an indented block: מזיחים 4 רווחים (או Tab ב-VS Code).IndentationError: expected an indented block: indent 4 spaces (or Tab in VS Code).if תנאי: ואז בלוק מוזחif condition: then an indented blockif תנאי: ואז בלוק מוזחif condition: then an indented blockelse כשאף תנאי לא התקייםelse when no condition held== != < > <= >=== != < > <= >=and: שניהם נכוניםand: both true0, "", None הם falsy0, "", None are falsyנבנה על ידיBuilt by Ariel Yohimovich