חלקPart 1: יסודותFoundations
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
טיפוסים ומספריםTypes & numbers
int, float, bool, המרות בין טיפוסים, עיגול, ומה קורה כשמערבבים.
int, float, bool, converting between types, rounding, and what happens when you mix them.
הנקודות החשובותKey points
intשלם,floatעשרוניintwhole,floatdecimalstrטקסט,boolאמת/שקרstrtext,booltrue/falseint("7")→ 7int("7")→ 7float("2.5")→ 2.5float("2.5")→ 2.5- השוואות מחזירות
True/FalseComparisons returnTrue/False ==שווה,!=שונה==equal,!=not equalround(x, 2)לשתי ספרותround(x, 2)to two places0.1 + 0.2הוא 0.300000000000000040.1 + 0.2is 0.30000000000000004import mathטוען מודולimport mathloads a module
מונחיםTerms
- טיפוס (type)type: סוג הערך: מספר שלם, עשרוני, מחרוזת, בוליאני ועוד. הטיפוס קובע אילו פעולות מותרות.The kind of value: integer, float, string, boolean and more. The type decides which operations are allowed.
- המרת טיפוסtype conversion: יצירת ערך חדש מטיפוס אחר:
int("5"),str(5),float("2.5").Making a new value of another type:int("5"),str(5),float("2.5"). - בוליאני (bool)boolean (bool): טיפוס עם שני ערכים בלבד:
Trueו-False.A type with only two values:TrueandFalse. - מודולmodule: קובץ קוד מוכן עם פונקציות. טוענים אותו עם
import, למשלimport math.A file of ready-made code. Load it withimport, e.g.import math. - ביטויexpression: צירוף שמחושב לערך אחד, כמו
2 + 3 * x.A combination that evaluates to one value, like2 + 3 * x.
Cheatsheet
טיפוסיםTypes
print(type(42))
print(type(3.5))
print(type("42"))
print(type(True))
print(7 / 2, type(7 / 2))המרותConversions
age = int(input("גיל: "))
print(f"בשנה הבאה תהיו בני {age + 1}")
price = float("19.90")
print(price * 2)
print(str(2026) + "!")age = int(input("Age: "))
print(f"Next year you will be {age + 1}")
price = float("19.90")
print(price * 2)
print(str(2026) + "!")boolbool
pages = 412
print(pages > 300)
print(pages == 400)
print(pages != 400)
print(bool(0), bool(7), bool(""), bool("hi"))עיגולRounding
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
print(round(2.675, 2))
price = 19.9
print(f"{price:.2f} ₪")
print(round(7.5), round(8.5))mathmath
import math
print(math.sqrt(81))
print(round(math.pi, 3))
print(math.ceil(412 / 30), "ימים לסיים ספר")
print(math.floor(9.99))
print(abs(-5), min(3, 9, 1), max(3, 9, 1))import math
print(math.sqrt(81))
print(round(math.pi, 3))
print(math.ceil(412 / 30), "days to finish a book")
print(math.floor(9.99))
print(abs(-5), min(3, 9, 1), max(3, 9, 1))ביטוייםExpressions
minutes = 135
hours = minutes // 60
rest = minutes % 60
print(f"{hours}:{rest:02d}")
print(2 + 3 * 2 ** 2)
print((2 + 3) * 2 ** 2)שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
TypeError | מחברים מספר לטקסט. ל-Python אין דרך לנחש אם התכוונתם ל-"55" או ל-10.Adding a number to text. Python can't guess whether you meant "55" or 10. | TypeError: ממירים קודם: int("5") + 5 נותן 10, "5" + str(5) נותן "55".TypeError: convert first: int("5") + 5 gives 10, "5" + str(5) gives "55". |
ValueError | ממירים למספר טקסט שאינו מספר.Converting text that isn't a number. | ValueError: int מקבלת רק ספרות. בהמשך (מפגש 11) נתפוס שגיאה כזו בלי שהתוכנית תיפול.ValueError: int accepts digits only. Later (session 11) we catch this without crashing. |
NameError | כותבים true באות קטנה. ב-Python זה True עם T גדולה.Writing true in lowercase. In Python it's True with a capital T. | NameError: true הוא סתם שם לא מוכר. כותבים True.NameError: true is just an unknown name. Write True. |
ValueError | ממירים מספר עם פסיק עשרוני. Python מבינה רק נקודה.Converting a number with a decimal comma. Python only understands a dot. | ValueError: משתמשים בנקודה: "19.90". אם הקלט מגיע עם פסיק: text.replace(",", ".").ValueError: use a dot: "19.90". If input comes with a comma: text.replace(",", "."). |
NameError | משתמשים ב-math בלי import.Using math without import. | NameError: name 'math' is not defined: מוסיפים import math בראש הקובץ.NameError: name 'math' is not defined: add import math at the top of the file. |
SyntaxError | שוכחים לסגור סוגריים בביטוי ארוך.Forgetting to close a parenthesis in a long expression. | SyntaxError: '(' was never closed: סופרים סוגריים פותחים וסוגרים.SyntaxError: '(' was never closed: count opening and closing brackets. |