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

חזרה למפגשBack
PythonPython

משתנים, print ו-inputVariables, print & input

מפגש 2: משתנים כתוויות על ערכים, קלט מהמשתמש, f-strings ראשונים.Session 2: Variables as labels on values, user input, first f-strings.

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

  • משתניםVariables
  • שמותNames
  • עדכון ערכיםUpdating values
  • קלטInput
  • f-stringsf-strings
  • sep ו-endsep and end
נושאTopic 1

משתניםVariables

משתנים: תוויות על ערכיםVariables: labels on values

  • משתנה = שם שמצביע על ערךVariable = a name pointing to a value
  • = הוא השמה, לא שוויון= means assign, not equals
  • מגדירים לפני שמשתמשיםDefine before you use
שאלה לכיתהQuestion for the class

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

python
title = "הנסיך הקטן"
pages = 96
print(title)
print(pages)
print(title, pages)
title = "The Little Prince"
pages = 96
print(title)
print(pages)
print(title, pages)

    משתניםVariables

    vars.py
    title = "הנסיך הקטן"
    pages = 96
    print(title)
    print(pages)
    print(title, pages)
    title = "The Little Prince"
    pages = 96
    print(title)
    print(pages)
    print(title, pages)
    פלטOutput
    הנסיך הקטן
    96
    הנסיך הקטן 96
    The Little Prince
    96
    The Little Prince 96

    אנלוגיהAnalogy

    משתנה הוא כמו מדבקה עם שם שמדביקים על קופסה. הקופסה (הערך) נמצאת בזיכרון, והמדבקה (השם) עוזרת לנו למצוא אותה.A variable is like a name sticker on a box. The box (the value) lives in memory, and the sticker (the name) helps us find it.

    טעות נפוצהCommon mistake

    vars.py
    print(author)
    author = "אנטואן"
    print(author)
    author = "Antoine"
    מה Python עונהWhat Python says
    NameError: name 'author' is not defined
    NameError: השם עוד לא קיים. מזיזים את ההשמה מעל השימוש.NameError: the name doesn't exist yet. Move the assignment above the use.
    נושאTopic 2

    שמותNames

    איך קוראים למשתניםNaming variables

    • אותיות, ספרות וקו תחתון; לא מתחילים בספרהLetters, digits, underscore; never start with a digit
    • snake_case: book_titlesnake_case: book_title
    • שם טוב מסביר מה יש בפניםA good name says what's inside
    שאלה לכיתהQuestion for the class

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

    python
    book_title = "Dune"
    page_count = 412
    is_finished = False
    print(book_title, page_count, is_finished)

      שמותNames

      names.py
      book_title = "Dune"
      page_count = 412
      is_finished = False
      print(book_title, page_count, is_finished)
      פלטOutput
      Dune 412 False

      אנלוגיהAnalogy

      שם משתנה הוא כמו כיתוב על צנצנת במטבח. "סוכר" עדיף בהרבה על "צנצנת 3", במיוחד כשחוזרים למטבח אחרי חודש.A variable name is like a label on a kitchen jar. "sugar" beats "jar 3", especially when you come back a month later.

      טעות נפוצהCommon mistake

      names.py
      2nd_book = "Dune"
      מה Python עונהWhat Python says
      SyntaxError: invalid decimal literal
      SyntaxError: invalid decimal literal: מתחילים באות, למשל second_book.SyntaxError: invalid decimal literal: start with a letter, e.g. second_book.
      נושאTopic 3

      עדכון ערכיםUpdating values

      שינוי ערך ומשתנים מרוביםChanging values and several variables

      • x = x + 1 מחשב ואז מעדכןx = x + 1 computes, then updates
      • x += 1 הוא קיצורx += 1 is the shortcut
      • החלפה: a, b = b, aSwap: a, b = b, a
      שאלה לכיתהQuestion for the class

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

      python
      read = 20
      read = read + 15
      read += 5
      print("עמודים שקראתי:", read)
      a, b = "ספר א", "ספר ב"
      a, b = b, a
      print(a, "|", b)
      read = 20
      read = read + 15
      read += 5
      print("Pages read:", read)
      a, b = "Book A", "Book B"
      a, b = b, a
      print(a, "|", b)

        עדכון ערכיםUpdating values

        update.py
        read = 20
        read = read + 15
        read += 5
        print("עמודים שקראתי:", read)
        a, b = "ספר א", "ספר ב"
        a, b = b, a
        print(a, "|", b)
        read = 20
        read = read + 15
        read += 5
        print("Pages read:", read)
        a, b = "Book A", "Book B"
        a, b = b, a
        print(a, "|", b)
        פלטOutput
        עמודים שקראתי: 40
        ספר ב | ספר א
        Pages read: 40
        Book B | Book A

        אנלוגיהAnalogy

        += הוא כמו להוסיף מטבעות לקופה: לא מחליפים קופה, רק משנים כמה יש בה.+= is like dropping coins in a jar: you don't replace the jar, you change how much is in it.

        טעות נפוצהCommon mistake

        update.py
        total += 10
        print(total)
        מה Python עונהWhat Python says
        NameError: name 'total' is not defined
        NameError: אין ערך ישן להוסיף אליו. מתחילים ב-total = 0.NameError: there's no old value to add to. Start with total = 0.
        נושאTopic 4

        קלטInput

        קלט מהמשתמש עם inputUser input with input

        • input() מחכה להקלדהinput() waits for typing
        • תמיד מחזיר מחרוזתAlways returns a string
        • הטקסט בסוגריים הוא שאלה למשתמשThe text in parentheses asks the user
        שאלה לכיתהQuestion for the class

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

        python
        name = input("איך קוראים לך? ")
        city = input("מאיפה את/ה? ")
        print("שלום", name, "מ", city)
        name = input("What is your name? ")
        city = input("Where are you from? ")
        print("Hello", name, "from", city)

          קלטInput

          hello_input.py
          name = input("איך קוראים לך? ")
          city = input("מאיפה את/ה? ")
          print("שלום", name, "מ", city)
          name = input("What is your name? ")
          city = input("Where are you from? ")
          print("Hello", name, "from", city)
          פלטOutput
          איך קוראים לך? Noa
          מאיפה את/ה? Haifa
          שלום Noa מ Haifa
          What is your name? Noa
          Where are you from? Haifa
          Hello Noa from Haifa

          אנלוגיהAnalogy

          input הוא כמו מלצר שמחכה ליד השולחן עד שתזמינו. כל מה שתגידו נרשם בפנקס כטקסט, גם "שתיים".input is like a waiter standing by until you order. Whatever you say is written down as text, even "two".
          נושאTopic 5

          f-stringsf-strings

          f-strings: טקסט עם ערכים בתוכוf-strings: text with values inside

          • f"...{x}..." מכניס ערכים לטקסטf"...{x}..." puts values in text
          • בתוך {} אפשר גם חישוב{} can hold a calculation too
          • בלי ה-f הסוגריים מודפסים כמו שהםWithout the f, braces print as-is

          אנלוגיהAnalogy

          f-string היא כמו טופס עם שדות ריקים: הטקסט מודפס מראש, ו-Python ממלאת כל שדה {} בערך הנכון.An f-string is like a form with blanks: the text is pre-printed and Python fills each {} blank with the right value.

          טעות נפוצהCommon mistake

          fstring.py
          name = "Noa"
          print(f"Hi {name")
          מה Python עונהWhat Python says
          SyntaxError: f-string: expecting '}'
          SyntaxError על ה-f-string: לכל { צריך } מתאים.SyntaxError in the f-string: every { needs a matching }.
          נושאTopic 6

          sep ו-endsep and end

          print מתקדם: sep ו-endMore print: sep and end

          • sep קובע מה בין הערכיםsep sets what goes between values
          • end קובע מה בסוף השורהend sets what ends the line
          • ברירת מחדל: רווח וירידת שורהDefaults: a space and a newline
          שאלה לכיתהQuestion for the class

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

          python
          print(19, 11, 2026, sep="/")
          print("טוען", end="")
          print("...", end="")
          print(" מוכן!")
          print("a", "b", "c", sep=" -> ")
          print(19, 11, 2026, sep="/")
          print("Loading", end="")
          print("...", end="")
          print(" done!")
          print("a", "b", "c", sep=" -> ")

            sep ו-endsep and end

            sep_end.py
            print(19, 11, 2026, sep="/")
            print("טוען", end="")
            print("...", end="")
            print(" מוכן!")
            print("a", "b", "c", sep=" -> ")
            print(19, 11, 2026, sep="/")
            print("Loading", end="")
            print("...", end="")
            print(" done!")
            print("a", "b", "c", sep=" -> ")
            פלטOutput
            19/11/2026
            טוען... מוכן!
            a -> b -> c
            19/11/2026
            Loading... done!
            a -> b -> c

            אנלוגיהAnalogy

            sep הוא הדבק בין לבנים, ו-end הוא מה ששמים בסוף הקיר. בלי end מיוחד, כל print מתחילה קיר חדש בשורה הבאה.sep is the mortar between bricks and end is what you put at the end of the wall. Without a special end, each print starts a new wall on the next line.

            טעות נפוצהCommon mistake

            sep_end.py
            pages = 96
            print("Pages: " + pages)
            מה Python עונהWhat Python says
            TypeError: can only concatenate str (not "int") to str
            TypeError: משתמשים בפסיק (print("Pages:", pages)) או ב-f-string.TypeError: use a comma (print("Pages:", pages)) or an f-string.

            משתנה = שם שמצביע על ערךVariable = a name pointing to a value

            = הוא השמה, לא שוויון= means assign, not equals

            מגדירים לפני שמשתמשיםDefine before you use

            מה למדנוWhat we learned

            • משתנה = שם שמצביע על ערךVariable = a name pointing to a value
            • אותיות, ספרות וקו תחתון; לא מתחילים בספרהLetters, digits, underscore; never start with a digit
            • x = x + 1 מחשב ואז מעדכןx = x + 1 computes, then updates
            • input() מחכה להקלדהinput() waits for typing
            • f"...{x}..." מכניס ערכים לטקסטf"...{x}..." puts values in text
            • sep קובע מה בין הערכיםsep sets what goes between values

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

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

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