נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

טיפוסים ומספריםTypes & numbers

מפגש 3: int, float, bool, המרות בין טיפוסים, עיגול, ומה קורה כשמערבבים.Session 3: int, float, bool, converting between types, rounding, and what happens when you mix them.

מה נעשה היוםToday's plan

  • טיפוסיםTypes
  • המרותConversions
  • boolbool
  • עיגולRounding
  • mathmath
  • ביטוייםExpressions
נושאTopic 1

טיפוסיםTypes

כל ערך שייך לטיפוסEvery value has a type

  • int שלם, float עשרוניint whole, float decimal
  • str טקסט, bool אמת/שקרstr text, bool true/false
  • type(x) מגלה את הטיפוסtype(x) reveals the type
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

python
print(type(42))
print(type(3.5))
print(type("42"))
print(type(True))
print(7 / 2, type(7 / 2))

    טיפוסיםTypes

    type_demo.py
    print(type(42))
    print(type(3.5))
    print(type("42"))
    print(type(True))
    print(7 / 2, type(7 / 2))
    פלטOutput
    <class 'int'>
    <class 'float'>
    <class 'str'>
    <class 'bool'>
    3.5 <class 'float'>

    אנלוגיהAnalogy

    טיפוס הוא כמו סוג הכלי במטבח: בכוס מוזגים, בסכין חותכים. אותו דבר (42) בכוס ובצלחת מתנהג אחרת.A type is like the kind of kitchenware: you pour into a cup, you cut with a knife. The same thing (42) behaves differently in a cup and on a plate.

    טעות נפוצהCommon mistake

    type_demo.py
    print("5" + 5)
    מה Python עונהWhat Python says
    TypeError: can only concatenate str (not "int") to str
    TypeError: ממירים קודם: int("5") + 5 נותן 10, "5" + str(5) נותן "55".TypeError: convert first: int("5") + 5 gives 10, "5" + str(5) gives "55".
    נושאTopic 2

    המרותConversions

    המרות: int, float, strConversions: int, float, str

    • int("7") → 7int("7") → 7
    • float("2.5") → 2.5float("2.5") → 2.5
    • str(7) → "7"str(7) → "7"
    שאלה לכיתהQuestion for the class

    מה ידפיס הקוד?What will this print?

    python
    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) + "!")

      המרותConversions

      convert.py
      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) + "!")
      פלטOutput
      גיל: 30
      בשנה הבאה תהיו בני 31
      39.8
      2026!
      Age: 30
      Next year you will be 31
      39.8
      2026!

      אנלוגיהAnalogy

      המרה היא כמו להחליף כסף בדלפק: נותנים שקלים ומקבלים דולרים. הערך המקורי לא משתנה, מקבלים ערך חדש.Conversion is like a currency exchange counter: you hand over shekels and get dollars. The original stays the same; you get a new value.

      טעות נפוצהCommon mistake

      convert.py
      pages = int("96 pages")
      מה Python עונהWhat Python says
      ValueError: invalid literal for int() with base 10: '96 pages'
      ValueError: int מקבלת רק ספרות. בהמשך (מפגש 11) נתפוס שגיאה כזו בלי שהתוכנית תיפול.ValueError: int accepts digits only. Later (session 11) we catch this without crashing.
      נושאTopic 3

      boolbool

      אמת ושקר: boolTrue and False: bool

      • השוואות מחזירות True/FalseComparisons return True/False
      • == שווה, != שונה== equal, != not equal
      • bool(0) הוא Falsebool(0) is False

      אנלוגיהAnalogy

      bool הוא מתג אור: דלוק או כבוי, אין מצב שלישי.A bool is a light switch: on or off, no third option.

      טעות נפוצהCommon mistake

      bools.py
      done = true
      מה Python עונהWhat Python says
      NameError: name 'true' is not defined. Did you mean: 'True'?
      NameError: true הוא סתם שם לא מוכר. כותבים True.NameError: true is just an unknown name. Write True.
      נושאTopic 4

      עיגולRounding

      עיגול ועשרונייםRounding and decimals

      • round(x, 2) לשתי ספרותround(x, 2) to two places
      • 0.1 + 0.2 הוא 0.300000000000000040.1 + 0.2 is 0.30000000000000004
      • להצגה: f"{x:.2f}"For display: f"{x:.2f}"
      שאלה לכיתהQuestion for the class

      מה ידפיס הקוד?What will this print?

      python
      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))

        עיגולRounding

        round.py
        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))
        פלטOutput
        0.30000000000000004
        0.3
        2.67
        19.90 ₪
        8 8

        אנלוגיהAnalogy

        זה כמו לכתוב שליש כמספר עשרוני: 0.3333... תמיד חסר קצת. מחשב בבסיס 2 נתקל באותה בעיה עם 0.1.It's like writing a third as a decimal: 0.3333... is always a bit short. A base-2 computer hits the same problem with 0.1.

        טעות נפוצהCommon mistake

        round.py
        price = float("19,90")
        מה Python עונהWhat Python says
        ValueError: could not convert string to float: '19,90'
        ValueError: משתמשים בנקודה: "19.90". אם הקלט מגיע עם פסיק: text.replace(",", ".").ValueError: use a dot: "19.90". If input comes with a comma: text.replace(",", ".").
        נושאTopic 5

        mathmath

        המודול mathThe math module

        • import math טוען מודולimport math loads a module
        • math.sqrt, math.pi, math.ceilmath.sqrt, math.pi, math.ceil
        • abs, min, max מובנותabs, min, max are built in

        אנלוגיהAnalogy

        import הוא כמו להוציא ארגז כלים מהמחסן. הכלים היו שם תמיד, רק צריך להביא אותם לשולחן.import is like fetching a toolbox from the shed. The tools were always there; you just bring them to the table.

        טעות נפוצהCommon mistake

        mathdemo.py
        print(math.sqrt(16))
        מה Python עונהWhat Python says
        NameError: name 'math' is not defined. Did you forget to import 'math'?
        NameError: name 'math' is not defined: מוסיפים import math בראש הקובץ.NameError: name 'math' is not defined: add import math at the top of the file.
        נושאTopic 6

        ביטוייםExpressions

        ביטויים וסדר פעולותExpressions and precedence

        • ** לפני * לפני +** before * before +
        • סוגריים מבהיריםParentheses clarify
        • // ו-% לחלוקה ושארית// and % for division and remainder
        שאלה לכיתהQuestion for the class

        מה ידפיס הקוד?What will this print?

        python
        minutes = 135
        hours = minutes // 60
        rest = minutes % 60
        print(f"{hours}:{rest:02d}")
        print(2 + 3 * 2 ** 2)
        print((2 + 3) * 2 ** 2)

          ביטוייםExpressions

          expr.py
          minutes = 135
          hours = minutes // 60
          rest = minutes % 60
          print(f"{hours}:{rest:02d}")
          print(2 + 3 * 2 ** 2)
          print((2 + 3) * 2 ** 2)
          פלטOutput
          2:15
          14
          20

          אנלוגיהAnalogy

          סדר פעולות הוא כמו סדר לבישה: גרביים לפני נעליים. אם רוצים סדר אחר, אומרים במפורש (סוגריים).Precedence is like the order of getting dressed: socks before shoes. For a different order, say so explicitly (parentheses).

          טעות נפוצהCommon mistake

          expr.py
          total = (2 + 3 * (4 - 1)
          print(total)
          מה Python עונהWhat Python says
          SyntaxError: '(' was never closed
          SyntaxError: '(' was never closed: סופרים סוגריים פותחים וסוגרים.SyntaxError: '(' was never closed: count opening and closing brackets.

          int שלם, float עשרוניint whole, float decimal

          str טקסט, bool אמת/שקרstr text, bool true/false

          type(x) מגלה את הטיפוסtype(x) reveals the type

          int("7") → 7int("7") → 7

          מה למדנוWhat we learned

          • int שלם, float עשרוניint whole, float decimal
          • int("7") → 7int("7") → 7
          • השוואות מחזירות True/FalseComparisons return True/False
          • round(x, 2) לשתי ספרותround(x, 2) to two places
          • import math טוען מודולimport math loads a module
          • ** לפני * לפני +** before * before +

          עכשיו לתרגולNow, practice

          5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
          הערות מרצהSpeaker notes

          נבנה על ידיBuilt by Ariel Yohimovich