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

PythonPython
2
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

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

משתנים כתוויות על ערכים, קלט מהמשתמש, f-strings ראשונים.

Variables as labels on values, user input, first f-strings.

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

משתנה הוא שם שאנחנו נותנים לערך כדי להשתמש בו שוב. הסימן = הוא השמה: Python מחשבת את מה שמימין, ומדביקה את השם שמשמאל על התוצאה.

A variable is a name we give a value so we can use it again. The = sign is assignment: Python computes what's on the right and sticks the name on the left onto the result.

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
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

אנלוגיה מהחייםReal-life 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.

ויזואלייזר זיכרוןMemory visualizerמשתנים בזיכרון, צעד אחר צעדVariables in memory, step by step

מה קורה עכשיוWhat happens now

פלטOutput

משתנים (frames)Variables (frames)

אובייקטים בזיכרוןObjects in memory

טעות נפוצהCommon mistake

משתמשים במשתנה לפני שיצרנו אותו. Python קוראת מלמעלה למטה, ובשורה 1 עוד אין author.

Using a variable before creating it. Python reads top to bottom, and on line 1 there's no author yet.

vars.py
print(author)
author = "אנטואן"
print(author)
author = "Antoine"
מה Python עונהWhat Python says
Traceback (most recent call last):
  File "/Users/you/python-course/vars.py", line 1, in <module>
    print(author)
          ^^^^^^
NameError: name 'author' is not defined
Traceback (most recent call last):
  File "/Users/you/python-course/vars.py", line 1, in <module>
    print(author)
          ^^^^^^
NameError: name 'author' is not defined
בעברית פשוטה: In plain words: NameError: השם עוד לא קיים. מזיזים את ההשמה מעל השימוש.NameError: the name doesn't exist yet. Move the assignment above the use.
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

משתנה הוא קופסה עם שם. כאן הקופסה savings מתמלאת לאט.

A variable is a box with a name. Here the box savings fills up slowly.

עוד דוגמה: קופת חיסכוןOne more: a piggy bank
example.py
savings = 0
savings = savings + 20
savings = savings + 15
print("I have", savings)
פלטOutput
I have 35
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

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

שם של משתנה יכול להכיל אותיות באנגלית, ספרות וקו תחתון, אבל לא להתחיל בספרה. ב-Python נהוג snake_case: אותיות קטנות ומילים עם _ ביניהן. אי אפשר להשתמש במילים שמורות כמו if או class.

A variable name can contain English letters, digits and underscores, but can't start with a digit. Python uses snake_case: lowercase words joined by _. You can't use keywords like if or class.

names.py
book_title = "Dune"
page_count = 412
is_finished = False
print(book_title, page_count, is_finished)
פלטOutput
Dune 412 False
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

שםNameתקין?Valid?למהWhy
book_titleכןYessnake_case, ברורsnake_case, clear
BookTitleעובד, לא מקובלWorks, not the conventionככה כותבים שמות של מחלקות (מפגש 15)That style is for class names (session 15)
2nd_bookלאNoמתחיל בספרהStarts with a digit
classלאNoמילה שמורהA keyword
xעובדWorksלא אומר כלום על התוכןSays nothing about the content
אנלוגיה מהחייםReal-life 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

שם שמתחיל בספרה. Python חושבת שזה מספר שנכתב לא נכון.

A name that starts with a digit. Python thinks it's a badly written number.

names.py
2nd_book = "Dune"
מה Python עונהWhat Python says
  File "/Users/you/python-course/names.py", line 1
    2nd_book = "Dune"
    ^
SyntaxError: invalid decimal literal
בעברית פשוטה: In plain words: SyntaxError: invalid decimal literal: מתחילים באות, למשל second_book.SyntaxError: invalid decimal literal: start with a letter, e.g. second_book.

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

אפשר לתת למשתנה ערך חדש בכל רגע. pages = pages + 10 נראה מוזר במתמטיקה, אבל ב-Python זה פשוט: מחשבים את הצד הימני עם הערך הישן, ואז מעדכנים. הקיצור הוא pages += 10.

You can give a variable a new value at any time. pages = pages + 10 looks odd in maths, but in Python it's simple: compute the right side with the old value, then update. The shortcut is pages += 10.

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
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

אנלוגיה מהחייםReal-life analogy

+= הוא כמו להוסיף מטבעות לקופה: לא מחליפים קופה, רק משנים כמה יש בה.

+= is like dropping coins in a jar: you don't replace the jar, you change how much is in it.

טיפTip

החלפה עם a, b = b, a היא טריק של Python. בשפות אחרות צריך משתנה עזר.

Swapping with a, b = b, a is a Python trick. Other languages need a helper variable.

טעות נפוצהCommon mistake

משתמשים ב-+= על משתנה שעוד לא קיים.

Using += on a variable that doesn't exist yet.

update.py
total += 10
print(total)
מה Python עונהWhat Python says
Traceback (most recent call last):
  File "/Users/you/python-course/update.py", line 1, in <module>
    total += 10
    ^^^^^
NameError: name 'total' is not defined
בעברית פשוטה: In plain words: NameError: אין ערך ישן להוסיף אליו. מתחילים ב-total = 0.NameError: there's no old value to add to. Start with total = 0.
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

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

input עוצרת את התוכנית ומחכה שהמשתמש יקליד שורה וילחץ Enter. הטקסט בסוגריים (הארגומנט) מוצג כשאלה. התוצאה תמיד מחרוזת, גם אם הקלידו מספר.

input pauses the program and waits for the user to type a line and press Enter. The text in the parentheses (the argument) is shown as a question. The result is always a string, even if they typed a number.

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
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

אנלוגיה מהחייםReal-life analogy

input הוא כמו מלצר שמחכה ליד השולחן עד שתזמינו. כל מה שתגידו נרשם בפנקס כטקסט, גם "שתיים".

input is like a waiter standing by until you order. Whatever you say is written down as text, even "two".

שימו לבNote

בדפדפן, הקלט מגיע מהשורות שהוגדרו בתרגיל. במחשב שלכם, הטרמינל מחכה שתקלידו.

In the browser, input comes from the lines set in the exercise. On your computer, the terminal waits for you to type.

טעות נפוצהCommon mistake

מחברים את הקלט למספר. הקלט הוא מחרוזת, ו-Python לא מחברת טקסט ומספר.

Adding a number to the input. The input is a string, and Python won't add text and a number.

age.py
age = input("Age? ")
print(age + 1)
מה Python עונהWhat Python says
Age? 30
Traceback (most recent call last):
  File "/Users/you/python-course/age.py", line 2, in <module>
    print(age + 1)
          ~~~~^~~
TypeError: can only concatenate str (not "int") to str
בעברית פשוטה: In plain words: TypeError: אי אפשר לחבר מחרוזת ומספר. במפגש 3 נלמד להמיר עם int(age).TypeError: you can't add a string and a number. In session 3 we convert with int(age).
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

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

f-string היא מחרוזת עם f לפני המירכאות. כל מה שבתוך סוגריים מסולסלים {} מחושב ומוכנס לטקסט. זו הדרך הנוחה ביותר לבנות הודעות.

An f-string is a string with f before the quotes. Anything inside curly braces {} is evaluated and placed in the text. It's the easiest way to build messages.

fstring.py
title = "Dune"
pages = 412
read = 103
print(f"הספר {title} כולל {pages} עמודים")
print(f"נשארו {pages - read} עמודים")
title = "Dune"
pages = 412
read = 103
print(f"The book {title} has {pages} pages")
print(f"Left: {pages - read} pages")
פלטOutput
הספר Dune כולל 412 עמודים
נשארו 309 עמודים
The book Dune has 412 pages
Left: 309 pages
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

אנלוגיה מהחייםReal-life 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

שוכחים לסגור סוגריים מסולסלים.

Forgetting to close a curly brace.

fstring.py
name = "Noa"
print(f"Hi {name")
מה Python עונהWhat Python says
  File "/Users/you/python-course/fstring.py", line 2
    print(f"Hi {name")
                    ^
SyntaxError: f-string: expecting '}'
בעברית פשוטה: In plain words: SyntaxError על ה-f-string: לכל { צריך } מתאים.SyntaxError in the f-string: every { needs a matching }.

f-string משלבת קופסאות בתוך טקסט, עם סוגריים מסולסלים.

An f-string puts boxes inside text, with curly braces.

עוד דוגמה: תעודת זהות של דמותOne more: a character card
example.py
name = "Luna"
age = 9
hp = 72.5
print(f"{name} is {age} years old")
print(f"Health: {hp:.0f}%")
פלטOutput
Luna is 9 years old
Health: 72%
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

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

ל-print יש שתי הגדרות שימושיות: sep (מה מפריד בין הערכים, ברירת מחדל רווח) ו-end (מה בא בסוף, ברירת מחדל ירידת שורה \n).

print has two handy settings: sep (what separates values, a space by default) and end (what comes at the end, a newline \n by default).

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
צפו בהרצה צעד אחר צעדWatch it run, step by step
צפו בהרצהWatch it runשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

אנלוגיה מהחייםReal-life 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

מנסים להדביק מספר למחרוזת עם + בתוך print.

Trying to glue a number to a string with + inside print.

sep_end.py
pages = 96
print("Pages: " + pages)
מה Python עונהWhat Python says
Traceback (most recent call last):
  File "/Users/you/python-course/sep_end.py", line 2, in <module>
    print("Pages: " + pages)
          ~~~~~~~~~~^~~~~~~
TypeError: can only concatenate str (not "int") to str
בעברית פשוטה: In plain words: TypeError: משתמשים בפסיק (print("Pages:", pages)) או ב-f-string.TypeError: use a comma (print("Pages:", pages)) or an f-string.
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

מה קורה עכשיוWhat happens now

הקופסאות (משתנים)The boxes (variables)

מסך הפלטOutput screen

סיימתם את ההרצאה!Lecture complete!

עכשיו המצגת לחזרה מהירה, או ישר לתרגול.

Next: the slides for a quick review, or straight to practice.

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