נבנה על ידי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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • משתנה = שם שמצביע על ערךVariable = a name pointing to a value
  • = הוא השמה, לא שוויון= means assign, not equals
  • אותיות, ספרות וקו תחתון; לא מתחילים בספרהLetters, digits, underscore; never start with a digit
  • snake_case: book_titlesnake_case: book_title
  • x = x + 1 מחשב ואז מעדכןx = x + 1 computes, then updates
  • x += 1 הוא קיצורx += 1 is the shortcut
  • input() מחכה להקלדהinput() waits for typing
  • תמיד מחזיר מחרוזתAlways returns a string
  • f"...{x}..." מכניס ערכים לטקסטf"...{x}..." puts values in text

מונחיםTerms

  • משתנהvariable: שם שמצביע על ערך בזיכרון. יוצרים אותו בהשמה: x = 5.A name that points to a value in memory. Created by assignment: x = 5.
  • השמהassignment: הפעולה =: מחשבים את הצד הימני ומדביקים את השם שבצד השמאלי על התוצאה.The = operation: compute the right side and stick the name on the left onto the result.
  • inputinput: פונקציה שמחכה שהמשתמש יקליד שורה ומחזירה אותה כמחרוזת.A function that waits for the user to type a line and returns it as a string.
  • f-stringf-string: מחרוזת עם f לפני המירכאות, שבה ביטויים בתוך {} מוחלפים בערך שלהם.A string with f before the quotes, where expressions inside {} are replaced by their value.
  • snake_casesnake_case: סגנון שמות ב-Python: אותיות קטנות ומילים מופרדות בקו תחתון, כמו book_title.Python's naming style: lowercase words separated by underscores, like book_title.
  • מילה שמורהkeyword: מילה שיש לה משמעות מיוחדת ב-Python (if, for, class...) ולכן אי אפשר להשתמש בה כשם.A word with special meaning in Python (if, for, class...) that can't be used as a name.
  • ארגומנטargument: ערך שמעבירים לפונקציה בתוך הסוגריים, למשל "Hi" ב-print("Hi").A value passed to a function inside the parentheses, like "Hi" in print("Hi").

Cheatsheet

משתניםVariables
title = "הנסיך הקטן"
pages = 96
print(title)
print(pages)
print(title, pages)title = "The Little Prince"
pages = 96
print(title)
print(pages)
print(title, pages)
שמותNames
book_title = "Dune"
page_count = 412
is_finished = False
print(book_title, page_count, is_finished)
עדכון ערכיםUpdating values
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)
קלטInput
name = input("איך קוראים לך? ")
city = input("מאיפה את/ה? ")
print("שלום", name, "מ", city)name = input("What is your name? ")
city = input("Where are you from? ")
print("Hello", name, "from", city)
f-stringsf-strings
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")
sep ו-endsep and end
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=" -> ")

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
NameErrorמשתמשים במשתנה לפני שיצרנו אותו. Python קוראת מלמעלה למטה, ובשורה 1 עוד אין author.Using a variable before creating it. Python reads top to bottom, and on line 1 there's no author yet.NameError: השם עוד לא קיים. מזיזים את ההשמה מעל השימוש.NameError: the name doesn't exist yet. Move the assignment above the use.
SyntaxErrorשם שמתחיל בספרה. Python חושבת שזה מספר שנכתב לא נכון.A name that starts with a digit. Python thinks it's a badly written number.SyntaxError: invalid decimal literal: מתחילים באות, למשל second_book.SyntaxError: invalid decimal literal: start with a letter, e.g. second_book.
NameErrorמשתמשים ב-+= על משתנה שעוד לא קיים.Using += on a variable that doesn't exist yet.NameError: אין ערך ישן להוסיף אליו. מתחילים ב-total = 0.NameError: there's no old value to add to. Start with total = 0.
TypeErrorמחברים את הקלט למספר. הקלט הוא מחרוזת, ו-Python לא מחברת טקסט ומספר.Adding a number to the input. The input is a string, and Python won't add text and a number.TypeError: אי אפשר לחבר מחרוזת ומספר. במפגש 3 נלמד להמיר עם int(age).TypeError: you can't add a string and a number. In session 3 we convert with int(age).
SyntaxErrorשוכחים לסגור סוגריים מסולסלים.Forgetting to close a curly brace.SyntaxError על ה-f-string: לכל { צריך } מתאים.SyntaxError in the f-string: every { needs a matching }.
TypeErrorמנסים להדביק מספר למחרוזת עם + בתוך print.Trying to glue a number to a string with + inside print.TypeError: משתמשים בפסיק (print("Pages:", pages)) או ב-f-string.TypeError: use a comma (print("Pages:", pages)) or an f-string.

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