משתנים, 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.
title = "הנסיך הקטן"
pages = 96
print(title)
print(pages)
print(title, pages)title = "The Little Prince"
pages = 96
print(title)
print(pages)
print(title, pages)הנסיך הקטן 96 הנסיך הקטן 96
The Little Prince 96 The Little Prince 96
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
משתנה הוא כמו מדבקה עם שם שמדביקים על קופסה. הקופסה (הערך) נמצאת בזיכרון, והמדבקה (השם) עוזרת לנו למצוא אותה.
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.
מה קורה עכשיוWhat happens now
פלטOutput
משתנים (frames)Variables (frames)
אובייקטים בזיכרוןObjects in memory
משתמשים במשתנה לפני שיצרנו אותו. Python קוראת מלמעלה למטה, ובשורה 1 עוד אין author.
Using a variable before creating it. Python reads top to bottom, and on line 1 there's no author yet.
print(author)
author = "אנטואן"print(author)
author = "Antoine"Traceback (most recent call last):
File "/Users/you/python-course/vars.py", line 1, in <module>
print(author)
^^^^^^
NameError: name 'author' is not definedTraceback (most recent call last):
File "/Users/you/python-course/vars.py", line 1, in <module>
print(author)
^^^^^^
NameError: name 'author' is not definedNameError: השם עוד לא קיים. מזיזים את ההשמה מעל השימוש.NameError: the name doesn't exist yet. Move the assignment above the use.צפו איפה זה נשברWatch where it breaks
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
משתנה הוא קופסה עם שם. כאן הקופסה savings מתמלאת לאט.
A variable is a box with a name. Here the box savings fills up slowly.
savings = 0
savings = savings + 20
savings = savings + 15
print("I have", savings)I have 35
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיו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.
book_title = "Dune"
page_count = 412
is_finished = False
print(book_title, page_count, is_finished)Dune 412 False
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
| שםName | תקין?Valid? | למהWhy |
|---|---|---|
book_title | כןYes | snake_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 |
שם משתנה הוא כמו כיתוב על צנצנת במטבח. "סוכר" עדיף בהרבה על "צנצנת 3", במיוחד כשחוזרים למטבח אחרי חודש.
A variable name is like a label on a kitchen jar. "sugar" beats "jar 3", especially when you come back a month later.
שם שמתחיל בספרה. Python חושבת שזה מספר שנכתב לא נכון.
A name that starts with a digit. Python thinks it's a badly written number.
2nd_book = "Dune" File "/Users/you/python-course/names.py", line 1
2nd_book = "Dune"
^
SyntaxError: invalid decimal literalSyntaxError: 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.
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)עמודים שקראתי: 40 ספר ב | ספר א
Pages read: 40 Book B | Book A
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
+= הוא כמו להוסיף מטבעות לקופה: לא מחליפים קופה, רק משנים כמה יש בה.
+= is like dropping coins in a jar: you don't replace the jar, you change how much is in it.
החלפה עם a, b = b, a היא טריק של Python. בשפות אחרות צריך משתנה עזר.
Swapping with a, b = b, a is a Python trick. Other languages need a helper variable.
משתמשים ב-+= על משתנה שעוד לא קיים.
Using += on a variable that doesn't exist yet.
total += 10
print(total)Traceback (most recent call last):
File "/Users/you/python-course/update.py", line 1, in <module>
total += 10
^^^^^
NameError: name 'total' is not definedNameError: אין ערך ישן להוסיף אליו. מתחילים ב-total = 0.NameError: there's no old value to add to. Start with total = 0.צפו איפה זה נשברWatch where it breaks
מה קורה עכשיו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.
name = input("איך קוראים לך? ")
city = input("מאיפה את/ה? ")
print("שלום", name, "מ", city)name = input("What is your name? ")
city = input("Where are you from? ")
print("Hello", name, "from", city)איך קוראים לך? Noa מאיפה את/ה? Haifa שלום Noa מ Haifa
What is your name? Noa Where are you from? Haifa Hello Noa from Haifa
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
input הוא כמו מלצר שמחכה ליד השולחן עד שתזמינו. כל מה שתגידו נרשם בפנקס כטקסט, גם "שתיים".
input is like a waiter standing by until you order. Whatever you say is written down as text, even "two".
בדפדפן, הקלט מגיע מהשורות שהוגדרו בתרגיל. במחשב שלכם, הטרמינל מחכה שתקלידו.
In the browser, input comes from the lines set in the exercise. On your computer, the terminal waits for you to type.
מחברים את הקלט למספר. הקלט הוא מחרוזת, ו-Python לא מחברת טקסט ומספר.
Adding a number to the input. The input is a string, and Python won't add text and a number.
age = input("Age? ")
print(age + 1)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 strTypeError: אי אפשר לחבר מחרוזת ומספר. במפגש 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
מה קורה עכשיו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.
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")הספר Dune כולל 412 עמודים נשארו 309 עמודים
The book Dune has 412 pages Left: 309 pages
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
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.
שוכחים לסגור סוגריים מסולסלים.
Forgetting to close a curly brace.
name = "Noa"
print(f"Hi {name") File "/Users/you/python-course/fstring.py", line 2
print(f"Hi {name")
^
SyntaxError: f-string: expecting '}'SyntaxError על ה-f-string: לכל { צריך } מתאים.SyntaxError in the f-string: every { needs a matching }.f-string משלבת קופסאות בתוך טקסט, עם סוגריים מסולסלים.
An f-string puts boxes inside text, with curly braces.
name = "Luna"
age = 9
hp = 72.5
print(f"{name} is {age} years old")
print(f"Health: {hp:.0f}%")Luna is 9 years old Health: 72%
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיו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).
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=" -> ")19/11/2026 טוען... מוכן! a -> b -> c
19/11/2026 Loading... done! a -> b -> c
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
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.
מנסים להדביק מספר למחרוזת עם + בתוך print.
Trying to glue a number to a string with + inside print.
pages = 96
print("Pages: " + pages)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 strTypeError: משתמשים בפסיק (print("Pages:", pages)) או ב-f-string.TypeError: use a comma (print("Pages:", pages)) or an f-string.צפו איפה זה נשברWatch where it breaks
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.