חלקPart 1: יסודותFoundations
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
תנאיםConditions
if, elif, else, השוואות, and/or/not ואמת/שקר.
if, elif, else, comparisons, and/or/not and truthiness.
הנקודות החשובותKey points
if תנאי:ואז בלוק מוזחif condition:then an indented block- הבלוק רץ רק אם התנאי TrueThe block runs only if the condition is True
elseכשאף תנאי לא התקייםelsewhen no condition heldelifבודק תנאי נוסףelifchecks another condition==!=<><=>===!=<><=>==משייך,==משווה=assigns,==comparesand: שניהם נכוניםand: both trueor: לפחות אחדor: at least one0,"",Noneהם falsy0,"",Noneare falsy
מונחיםTerms
- תנאיcondition: ביטוי שערכו True או False, ולפיו מחליטים אם להריץ קוד.An expression that's True or False, used to decide whether to run code.
- בלוקblock: קבוצת שורות עם אותה הזחה, שרצות יחד. בלוק מתחיל אחרי שורה שנגמרת בנקודתיים.Lines with the same indentation that run together. A block starts after a line ending in a colon.
- אופרטור לוגיlogical operator:
and,or,not: מחברים או הופכים תנאים.and,or,not: combine or invert conditions. - truthy / falsytruthy / falsy: ערכים שנחשבים אמת או שקר בתנאי. falsy:
0,"",[],None,False. כל השאר truthy.Values treated as true or false in a condition. Falsy:0,"",[],None,False. Everything else is truthy.
Cheatsheet
ifif
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")elif ו-elseelif and else
score = int(input("ציון: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")score = int(input("Score: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")השוואותComparisons
age = 16
print(age == 16, age != 16)
print(13 <= age < 18)
print("apple" < "banana")
print("Zebra" < "apple")and/or/notand/or/not
age = 15
has_ticket = True
with_parent = False
if has_ticket and (age >= 16 or with_parent):
print("נכנסים")
else:
print("מצטערים")age = 15
has_ticket = True
with_parent = False
if has_ticket and (age >= 16 or with_parent):
print("Come in")
else:
print("Sorry")truthinessTruthiness
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")קינוןNesting
member = True
books = 3
if member:
if books < 5:
print("אפשר לשאול עוד ספר")
else:
print("הגעת למקסימום")member = True
books = 3
if member:
if books < 5:
print("You can borrow another")
else:
print("You reached the limit")שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
SyntaxError | שוכחים את הנקודתיים בסוף שורת ה-if.Forgetting the colon at the end of the if line. | SyntaxError: expected ':': כל if, elif, else נגמרים בנקודתיים.SyntaxError: expected ':': every if, elif, else ends with a colon. |
SyntaxError | כותבים else עם תנאי.Writing a condition after else. | SyntaxError: ל-else אין תנאי. תנאי נוסף כותבים עם elif.SyntaxError: else takes no condition. Another condition uses elif. |
SyntaxError | משתמשים ב-= בתוך תנאי.Using = inside a condition. | SyntaxError: Python אפילו שואלת אם התכוונתם ל-==. בתנאי משווים עם ==.SyntaxError: Python even asks whether you meant ==. Conditions compare with ==. |
SyntaxError | כותבים x == 1 or 2 במקום שני תנאים מלאים. זה לא נופל, אבל תמיד True!Writing x == 1 or 2 instead of two full conditions. It doesn't crash, but it's always True! | 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). |
TypeError | משווים מחרוזת מהקלט למספר.Comparing a string from input with a number. | TypeError: '>=' not supported between instances of 'str' and 'int': ממירים עם int(input(...)).TypeError: '>=' not supported between instances of 'str' and 'int': convert with int(input(...)). |
IndentationError | שוכחים להזיח את הבלוק.Forgetting to indent the block. | IndentationError: expected an indented block: מזיחים 4 רווחים (או Tab ב-VS Code).IndentationError: expected an indented block: indent 4 spaces (or Tab in VS Code). |