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

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

מה זה תכנות? התקנה וטרמינלWhat is programming? Setup & terminal

איך מחשב מבצע הוראות, התקנת Python ו-VS Code ב-Windows וב-Mac, הטרמינל, והתוכנית הראשונה עם print.

How a computer follows instructions, installing Python and VS Code on Windows and Mac, the terminal, and a first program with print.

01מה זה תכנות?What is programming?

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

A computer is a very fast machine, but it never guesses. It does exactly what it is told, and only that. A program is a list of instructions the computer carries out in order, and programming is writing those instructions in a language the computer can run.

הטקסט שאנחנו כותבים נקרא קוד. הנה התוכנית הכי קטנה שיש: שתי שורות, ובכל אחת הוראה אחת: "תציג את הטקסט הזה".

The text we write is called code. Here is about the smallest program there is: two lines, each with one instruction: "show this text".

hello.py
print("שלום! זו התוכנית הראשונה שלי")
print("Python מבצעת שורה אחרי שורה")
print("Hello! This is my first program")
print("Python runs one line after another")
פלטOutput
שלום! זו התוכנית הראשונה שלי
Python מבצעת שורה אחרי שורה
Hello! This is my first program
Python runs one line after another
צפו בהרצה צעד אחר צעד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

תחשבו על רובוט-שף שמקבל מתכון. הוא יבצע "שבור 2 ביצים" בדיוק, צעד אחרי צעד. אבל אם כתבתם "שבור 2 ביצם", הוא לא ינחש שהתכוונתם לביצים. הוא יעצור ויגיד "לא הבנתי". ככה בדיוק מתנהגת Python: מדויקת, צייתנית, ובלי דמיון.

Think of a robot chef following a recipe. It will "crack 2 eggs" exactly, step by step. But if you wrote "crack 2 egs", it won't guess you meant eggs. It stops and says "I didn't understand". Python behaves exactly like that: precise, obedient, and with zero imagination.

טעות נפוצהCommon mistake

כותבים Print עם P גדולה. לבני אדם זה אותו דבר, אבל ל-Python אלה שתי מילים שונות לגמרי. Python רגישה לאותיות גדולות וקטנות.

Writing Print with a capital P. To a human it's the same, but to Python they are two completely different words. Python is case-sensitive.

hello.py
Print("שלום")
Print("Hello")
מה Python עונהWhat Python says
Traceback (most recent call last):
  File "/Users/you/python-course/hello.py", line 1, in <module>
    Print("שלום")
    ^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?
Traceback (most recent call last):
  File "/Users/you/python-course/hello.py", line 1, in <module>
    Print("Hello")
    ^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?
בעברית פשוטה: In plain words: NameError אומרת "אני לא מכירה את השם הזה". Python אפילו מנחשת למה התכוונתם: Did you mean: 'print'?. מתקנים ל-print עם p קטנה.NameError means "I don't know this name". Python even guesses what you meant: Did you mean: 'print'?. Fix it to lowercase print.
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

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

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

מסך הפלטOutput screen

02למה דווקא Python?Why Python?

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

There are hundreds of programming languages. Python is one of the most popular in the world, for good reason: its code reads almost like plain English, and you can build almost anything with it.

תחוםFieldמה עושים עם PythonWhat Python does thereאיפה בקורסWhere in the course
אוטומציהAutomationמשימות משעממות שהמחשב עושה במקומכםBoring tasks the computer does for youמפגש 28Session 28
נתוניםDataטבלאות, סטטיסטיקה וגרפיםTables, statistics and chartsמפגשים 24-25Sessions 24-25
אתרים ו-APIWeb & APIsשרתים שאתרים ואפליקציות מדברים איתםServers that sites and apps talk toמפגש 29Session 29
AIAIעבודה עם מודלי שפהWorking with language modelsמפגשים 32-33Sessions 32-33
משחקיםGamesמשחקים דו-ממדיים עם Pygame2D games with Pygameחלק 7Part 7

כשמתקינים "Python", מתקינים בעצם מפרש: תוכנה שקוראת את הקוד שלכם ומבצעת אותו, שורה אחרי שורה.

When you "install Python", you actually install an interpreter: a program that reads your code and runs it, line after line.

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

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

The interpreter is like a simultaneous interpreter at a conference: it hears one sentence, translates it into the computer's language, the computer acts. Then the next sentence.

עוד דבר חשוב: מירכאות משנות הכול. מה שבתוך מירכאות הוא טקסט (מחרוזת), ו-Python מציגה אותו כמו שהוא. מה שבלי מירכאות, Python מחשבת.

One more key thing: quotes change everything. Whatever is inside quotes is text (a string), and Python shows it as is. Without quotes, Python calculates.

quotes.py
print(2 + 3)
print("2 + 3")
פלטOutput
5
2 + 3
צפו בהרצה צעד אחר צעד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

טעות נפוצהCommon mistake

משאירים חישוב באמצע. ל-Python חסר משהו אחרי ה-+, אז היא לא יכולה להבין את השורה בכלל.

Leaving a calculation half-written. Python is missing something after +, so it can't understand the line at all.

quotes.py
print(2 +)
מה Python עונהWhat Python says
  File "/Users/you/python-course/quotes.py", line 1
    print(2 +)
             ^
SyntaxError: invalid syntax
בעברית פשוטה: In plain words: SyntaxError היא שגיאת תחביר: השורה לא כתובה לפי חוקי השפה. החץ ^ מצביע בערך למקום שבו Python הסתבכה. משלימים את החישוב: print(2 + 3).SyntaxError is a syntax error: the line doesn't follow the language rules. The ^ points roughly to where Python got confused. Complete the calculation: print(2 + 3).

03התקנת Python (Windows ו-Mac)Installing Python (Windows and Mac)

התקנה נעשית פעם אחת. מורידים מהאתר הרשמי python.org את הגרסה האחרונה (3.12 ומעלה), ומתקינים. יש הבדל קטן אבל חשוב בין Windows ל-Mac, אז קראו את החלק שלכם.

Installation happens once. Download the latest version (3.12 or newer) from the official site python.org and install it. There is a small but important difference between Windows and Mac, so read your part.

WindowsWindows

  1. מריצים את קובץ ההתקנה שהורד.Run the installer you downloaded.
  2. חשוב מאוד: מסמנים בתחתית החלון את התיבה Add python.exe to PATH.Very important: at the bottom of the window, tick Add python.exe to PATH.
  3. לוחצים Install Now ומחכים.Click Install Now and wait.
  4. פותחים PowerShell (מחפשים "PowerShell" בתפריט התחל) ובודקים:Open PowerShell (search "PowerShell" in the Start menu) and check:
PowerShell
python --version
פלט לדוגמהSample output
Python 3.13.18

MacMac

  1. פותחים את קובץ ה-‎.pkg שהורד ומאשרים את השלבים.Open the downloaded .pkg file and go through the steps.
  2. בסוף ההתקנה נפתחת תיקייה. אפשר ללחוץ פעמיים על Install Certificates.command (עוזר בהמשך לגישה לאינטרנט מ-Python).A folder opens at the end. You can double-click Install Certificates.command (it helps later when Python talks to the internet).
  3. פותחים את Terminal (‏Cmd+Space ומקלידים Terminal) ובודקים. שימו לב: ב-Mac הפקודה היא python3.Open Terminal (Cmd+Space and type Terminal) and check. Note: on a Mac the command is python3.
Terminal (Mac)
python3 --version
פלט לדוגמהSample output
Python 3.13.18
טיפTip

המספר אצלכם יכול להיות קצת שונה. כל גרסה 3.12 ומעלה מתאימה לקורס.

Your number may be slightly different. Any version 3.12 or newer works for this course.

טעות נפוצהCommon mistake

ב-Windows שכחו לסמן את Add to PATH. הטרמינל לא יודע איפה Python נמצאת, כי היא לא ב-PATH.

On Windows, forgot to tick Add to PATH. The terminal doesn't know where Python is, because it isn't on PATH.

PowerShell
python --version
מה הטרמינל עונהWhat the terminal says
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
בעברית פשוטה: In plain words: "לא מוכר" אומר שהטרמינל לא מצא תוכנה בשם הזה. הפתרון: להריץ שוב את ההתקנה, לבחור Modify או להתקין מחדש עם התיבה מסומנת, ולפתוח PowerShell חדש. בינתיים אפשר לנסות py --version."Not recognized" means the terminal found no program with that name. Fix: run the installer again, choose Modify or reinstall with the box ticked, then open a new PowerShell. Meanwhile, try py --version.

04הטרמינל: לדבר עם המחשב בטקסטThe terminal: talking to the computer in text

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

The terminal is a window where you type commands instead of clicking. It looks scary at first, but five or six commands are enough to get along great.

הטרמינל תמיד "עומד" בתיקייה אחת: התיקייה הנוכחית. הכתובת שלה נקראת נתיב.

The terminal always "stands" in one folder (directory): the current directory. Its address is called a path.

מה רוציםWhat you wantMacMacWindows (PowerShell)Windows (PowerShell)
איפה אני?Where am I?pwdpwd
מה יש כאן?What's here?lsdir (או ls)dir (or ls)
להיכנס לתיקייהGo into a foldercd Desktopcd Desktop
לעלות תיקייה אחת למעלהGo up one foldercd ..cd ..
ליצור תיקייהMake a foldermkdir python-coursemkdir python-course
לנקות את המסךClear the screenclearcls
אנלוגיה מהחייםReal-life analogy

הטרמינל הוא כמו לנווט בבניין עם עיניים עצומות ומישהו שעונה לכם: pwd = "באיזה חדר אני?", ls = "מה יש בחדר?", cd = "תיכנס לדלת הזו", cd .. = "צא החוצה למסדרון".

The terminal is like moving through a building with your eyes closed and a helper who answers: pwd = "which room am I in?", ls = "what's in this room?", cd = "go through this door", cd .. = "step back out to the hallway".

נסו עכשיו בסימולטור. הוא מתנהג כמו טרמינל אמיתי (ב-Mac או ב-Windows), אבל הכול קורה בתוך העמוד. בצעו את המשימות שמתחת:

Try it now in the simulator. It behaves like a real terminal (Mac or Windows), but everything happens inside this page. Complete the tasks below it:

סימולטור טרמינלTerminal simulator נסו בעצמכם. שום דבר כאן לא נוגע במחשב שלכם.Try it yourself. Nothing here touches your computer.
  • גלו איפה אתם עם pwdFind out where you are with pwd
  • צרו תיקייה: mkdir python-courseMake a folder: mkdir python-course
  • היכנסו אליה: cd python-courseGo into it: cd python-course
  • צרו קובץ: echo 'print("Hi")' > hello.pyCreate a file: echo 'print("Hi")' > hello.py
  • הריצו אותו: python3 hello.py (ב-Windows: python hello.py)Run it: python3 hello.py (Windows: python hello.py)
טעות נפוצהCommon mistake

מריצים קובץ מהתיקייה הלא נכונה, או עם שגיאת כתיב בשם הקובץ. Python מחפשת את הקובץ רק בתיקייה הנוכחית.

Running a file from the wrong folder, or with a typo in its name. Python only looks for the file in the current directory.

Terminal
python3 helo.py
מה הטרמינל עונהWhat the terminal says
python3: can't open file '/Users/you/python-course/helo.py': [Errno 2] No such file or directory
בעברית פשוטה: In plain words: No such file or directory = "אין קובץ כזה כאן". בודקים שני דברים: האם אני בתיקייה הנכונה (pwd, ls), והאם השם כתוב בדיוק נכון.No such file or directory = "there is no such file here". Check two things: am I in the right folder (pwd, ls), and is the name spelled exactly right.

05VS Code והקובץ הראשוןVS Code and the first file

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

A code editor is where you write code comfortably. In this course we use VS Code: free, runs everywhere, and has an excellent Python extension.

  1. מורידים ומתקינים מ-code.visualstudio.com.Download and install from code.visualstudio.com.
  2. פותחים את חלון התוספים (Ctrl+Shift+X, ב-Mac Cmd+Shift+X), מחפשים Python של Microsoft ומתקינים.Open Extensions (Ctrl+Shift+X, on Mac Cmd+Shift+X), search Python by Microsoft and install it.
  3. File → Open Folder ובוחרים את התיקייה python-course.File → Open Folder and choose the python-course folder.
  4. יוצרים קובץ חדש בשם hello.py. הסיומת ‎.py אומרת "זה קוד Python".Create a new file named hello.py. The .py ending says "this is Python code".
  5. כותבים קוד, שומרים (Ctrl+S), ולוחצים על ▶ בפינה העליונה. הפלט מופיע בחלונית הטרמינל למטה.Write code, save (Ctrl+S), and click ▶ in the top corner. The output appears in the terminal panel at the bottom.
hello.py
print("שלום מ-VS Code!")
print("הקובץ הזה הוא סקריפט")
print("כל הכבוד לי!")
print("Hello from VS Code!")
print("This file is a script")
print("Well done, me!")
פלטOutput
שלום מ-VS Code!
הקובץ הזה הוא סקריפט
כל הכבוד לי!
Hello from VS Code!
This file is a script
Well done, me!
צפו בהרצה צעד אחר צעד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

קובץ כזה, שמריצים מההתחלה עד הסוף, נקרא סקריפט. כשלוחצים ▶, VS Code בעצם מקליד בשבילכם בטרמינל את הפקודה python3 hello.py.

A file like this, run from start to finish, is called a script. When you click ▶, VS Code really just types python3 hello.py in the terminal for you.

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

VS Code הוא כמו מטבח מאובזר: אפשר לבשל גם על גזייה בשטח (Notepad + טרמינל), אבל עם כיריים, סכינים חדים ותבלינים בהישג יד, הכול מהיר ונעים יותר.

VS Code is like a well-equipped kitchen: you could cook on a camping stove (Notepad + terminal), but with a proper stove, sharp knives and spices in reach, everything is faster and nicer.

טעות נפוצהCommon mistake

שוכחים לשמור לפני הרצה, או שומרים בשם hello.py.txt. רואים את הנקודה הלבנה ליד שם הקובץ בלשונית? היא אומרת "לא נשמר".

Forgetting to save before running, or saving as hello.py.txt. See the white dot next to the file name in the tab? It means "not saved".

Terminal
python3 hello.py
מה הטרמינל עונהWhat the terminal says
python3: can't open file '/Users/you/python-course/hello.py': [Errno 2] No such file or directory
בעברית פשוטה: In plain words: Python מריצה את מה ששמור בדיסק, לא את מה שמוצג בעורך. אם הקובץ לא נשמר או נשמר בשם אחר, היא לא מוצאת אותו. מפעילים File → Auto Save וזה לא יקרה יותר.Python runs what is saved on disk, not what you see in the editor. If the file wasn't saved, or was saved under another name, it can't find it. Turn on File → Auto Save and this stops happening.

06print: מדברים עם המסךprint: talking to the screen

print היא פונקציה: פעולה עם שם, שמפעילים בעזרת סוגריים. מה שבתוך הסוגריים נשלח לפלט. אפשר לתת לה טקסט, מספרים, חישובים, וכמה דברים ביחד מופרדים בפסיקים. כל print מתחילה שורה חדשה, ו-print() ריקה מדפיסה שורה ריקה.

print is a function: a named action you run with parentheses. Whatever is inside the parentheses goes to the output. You can give it text, numbers, calculations, and several things separated by commas. Each print starts a new line, and an empty print() prints a blank line.

סימולציהSimulationככה Python מריצה את הקוד, שורה אחרי שורהHow Python runs the code, line by line

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

מסך הפלטOutput screen

טעות נפוצהCommon mistake

שוכחים לסגור מירכאות. Python לא יודעת איפה הטקסט נגמר.

Forgetting to close the quotes. Python doesn't know where the text ends.

hello.py
print("שלום)
print("Hello)
מה Python עונהWhat Python says
  File "/Users/you/python-course/hello.py", line 1
    print("שלום)
          ^
SyntaxError: unterminated string literal (detected at line 1)
  File "/Users/you/python-course/hello.py", line 1
    print("Hello)
          ^
SyntaxError: unterminated string literal (detected at line 1)
בעברית פשוטה: In plain words: unterminated string literal = "מחרוזת שלא נסגרה". סופרים מירכאות: תמיד צריכות להיות שתיים, אחת בהתחלה ואחת בסוף.unterminated string literal = "a string that was never closed". Count the quotes: there must always be two, one at the start and one at the end.
טיפTip

אפשר להשתמש במירכאות כפולות "..." או בודדות '...'. רק שיהיו מאותו סוג בהתחלה ובסוף.

You can use double quotes "..." or single quotes '...'. Just use the same kind at the start and the end.

כמה print אחד אחרי השני יוצרים ציור קטן. כל print הוא שורה חדשה על המסך.

A few prints in a row make a small picture. Each print is a new line on the screen.

עוד דוגמה: כרטיס ברכהOne more: a greeting card
example.py
print("*********")
print("* Hello! *")
print("*********")
print("Made by me")
פלטOutput
*********
* Hello! *
*********
Made by me
צפו בהרצה צעד אחר צעד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

07Python כמחשבוןPython as a calculator

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

Python can calculate. The symbols that do operations are called operators. The order is like in school: multiply and divide before add and subtract, and parentheses first.

calc.py
print(7 + 3)     # חיבור
print(7 - 3)     # חיסור
print(7 * 3)     # כפל
print(7 / 2)     # חילוק
print(7 // 2)    # חילוק שלם
print(7 % 2)     # שארית
print(2 ** 10)   # חזקה
print(2 + 3 * 4)
print((2 + 3) * 4)
print(7 + 3)     # add
print(7 - 3)     # subtract
print(7 * 3)     # multiply
print(7 / 2)     # divide
print(7 // 2)    # whole division
print(7 % 2)     # remainder
print(2 ** 10)   # power
print(2 + 3 * 4)
print((2 + 3) * 4)
פלטOutput
10
4
21
3.5
3
1
1024
14
20
10
4
21
3.5
3
1
1024
14
20
צפו בהרצה צעד אחר צעד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

שימו לב: 7 / 2 נותן 3.5, מספר עשרוני, ו-7 // 2 נותן 3, מספר שלם. על סוגי מספרים נדבר לעומק במפגש 3.

Notice: 7 / 2 gives 3.5, a float, and 7 // 2 gives 3, an integer (int). We go deep into number types in session 3.

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

// ו-% הם כמו חלוקת 7 עוגיות בין 2 ילדים: כל ילד מקבל 3 עוגיות (7 // 2), ונשארת עוגייה אחת (7 % 2).

// and % are like sharing 7 cookies between 2 kids: each kid gets 3 cookies (7 // 2), and 1 cookie is left over (7 % 2).

טעות נפוצהCommon mistake

מחלקים באפס. גם במתמטיקה אי אפשר, ו-Python עוצרת את התוכנית.

Dividing by zero. You can't do that in math either, and Python stops the program.

calc.py
print(10 / 0)
מה Python עונהWhat Python says
Traceback (most recent call last):
  File "/Users/you/python-course/calc.py", line 1, in <module>
    print(10 / 0)
          ~~~^~~
ZeroDivisionError: division by zero
בעברית פשוטה: In plain words: ZeroDivisionError: division by zero = "חילוק באפס". מתקנים את המספר. בהמשך הקורס (מפגש 11) נלמד איך לתפוס שגיאה כזו בלי שהתוכנית תיפול.ZeroDivisionError: division by zero means exactly that. Fix the number. Later (session 11) we learn to catch such errors without crashing.
צפו איפה זה נשברWatch where it breaks
איפה זה נשברWhere it breaksשורה אחרי שורה, כמו ש-Python קוראתLine by line, the way Python reads it

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

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

מסך הפלטOutput screen

Python היא מחשבון: פיצה ב-84 שקלים לשלושה חברים.

Python is a calculator: an 84-shekel pizza for three friends.

עוד דוגמה: כמה עולה פיצה לכל אחדOne more: pizza per person
example.py
print(84 / 3)
print(84 // 3)
print(2 * 3 + 1)
print(2 * (3 + 1))
פלטOutput
28.0
28
7
8
צפו בהרצה צעד אחר צעד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

08הערות, סדר והזחהComments, order and indentation

כל מה שאחרי # הוא הערה: Python מתעלמת ממנו לגמרי. הערות הן פתקים לבני אדם, כולל לעצמכם בעוד חודש. Python מבצעת את השורות לפי הסדר, מלמעלה למטה, ולכן הסדר בקובץ הוא הסדר בפלט.

Everything after # is a comment: Python ignores it completely. Comments are notes for humans, including yourself a month from now. Python runs the lines in order, top to bottom, so the order in the file is the order in the output.

card.py
# התוכנית מדפיסה כרטיס ביקור
print("דנה כהן")
print("מפתחת Python (מתחילה)")
# print("השורה הזו לא תודפס")
print("dana@example.com")  # הערה בסוף שורה
# This program prints a business card
print("Dana Cohen")
print("Python developer (beginner)")
# print("This line will not print")
print("dana@example.com")  # a comment at the end of a line
פלטOutput
דנה כהן
מפתחת Python (מתחילה)
dana@example.com
Dana Cohen
Python developer (beginner)
dana@example.com
צפו בהרצה צעד אחר צעד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

טיפTip

טריק שימושי: כדי "לכבות" שורה בלי למחוק אותה, שמים לפניה #. ב-VS Code: Ctrl+/ (ב-Mac Cmd+/).

Handy trick: to "switch off" a line without deleting it, put # in front. In VS Code: Ctrl+/ (Mac: Cmd+/).

טעות נפוצהCommon mistake

מתחילים שורה ברווח. ב-Python לרווחים בתחילת שורה, הזחה, יש משמעות (נלמד עליה במפגש 5), ולכן רווח "סתם" הוא שגיאה.

Starting a line with a space. In Python, spaces at the start of a line, indentation, have meaning (session 5), so a "random" space is an error.

card.py
print("שורה ראשונה")
 print("שורה שנייה")
print("First line")
 print("Second line")
מה Python עונהWhat Python says
  File "/Users/you/python-course/card.py", line 2
    print("שורה שנייה")
IndentationError: unexpected indent
  File "/Users/you/python-course/card.py", line 2
    print("Second line")
IndentationError: unexpected indent
בעברית פשוטה: In plain words: IndentationError: unexpected indent = "הזחה שלא ציפיתי לה". מוחקים את הרווח בתחילת שורה 2.IndentationError: unexpected indent = "an indent I didn't expect". Delete the space at the start of line 2.

09מצב אינטראקטיבי (REPL) מול סקריפטInteractive mode (REPL) vs a script

אם מקלידים בטרמינל רק python3 (או python ב-Windows), נכנסים למצב אינטראקטיבי. Python מציגה >>> ומחכה לשורה אחת. מקלידים, לוחצים Enter, ורואים תוצאה מיד. יוצאים עם exit().

Typing just python3 in the terminal (python on Windows) enters interactive mode. Python shows >>> and waits for one line. Type, press Enter, see the result immediately. Leave with exit().

Terminal · REPL
python3
>>> 2 + 2
4
>>> print("Hi")
Hi
>>> exit()
REPL ‏(‎>>>‎)REPL (>>>)סקריפט (קובץ ‎.py)Script (.py file)
למה זה טובGood forניסוי קטן ומהירA quick small experimentתוכנית שרוצים לשמור ולהריץ שובA program you keep and run again
נשמר?Saved?לא, נעלם כשסוגריםNo, gone when you close itכן, בקובץYes, in the file
איך מריציםHow to runpython3 ואז שורה אחרי שורהpython3, then line by linepython3 hello.py או ▶python3 hello.py or ▶
אנלוגיה מהחייםReal-life analogy

ה-REPL הוא כמו מחשבון כיס: מהיר ונוח, אבל שום דבר לא נשמר. סקריפט הוא כמו מתכון כתוב: אפשר לבשל אותו שוב מחר.

The REPL is like a pocket calculator: quick and handy, but nothing is saved. A script is like a written recipe: you can cook it again tomorrow.

טעות נפוצהCommon mistake

מעתיקים מהאינטרנט קוד עם >>> לתוך קובץ. ה->>> הוא חלק מהתצוגה של ה-REPL, לא חלק מהקוד.

Copying code from the internet with >>> into a file. The >>> is part of the REPL display, not part of the code.

hello.py
>>> print("Hi")
מה Python עונהWhat Python says
  File "/Users/you/python-course/hello.py", line 1
    >>> print("Hi")
    ^^
SyntaxError: invalid syntax
בעברית פשוטה: In plain words: Python לא מכירה את הסימן >>> כקוד, ולכן invalid syntax. מוחקים את >>> ואת הרווח שאחריו.Python doesn't know >>> as code, hence invalid syntax. Delete the >>> and the space after it.

10איך קוראים הודעת שגיאהHow to read an error message

שגיאות הן לא כישלון. הן Python שמנסה לעזור. כשתוכנית נופלת, Python מדפיסה דוח שנקרא Traceback. קוראים אותו מלמטה למעלה: השורה האחרונה אומרת מה קרה, ומעליה כתוב איפה (קובץ ומספר שורה).

Errors are not failure. They are Python trying to help. When a program crashes, Python prints a report called a traceback. Read it bottom to top: the last line says what happened, and above it is where (file and line number).

crash.py
print("לפני השגיאה")
print("התוצאה:", 10 / 0)
print("השורה הזו כבר לא תרוץ")
print("Before the error")
print("Result:", 10 / 0)
print("This line will never run")
מה Python עונהWhat Python says
לפני השגיאה
Traceback (most recent call last):
  File "/Users/you/python-course/crash.py", line 2, in <module>
    print("התוצאה:", 10 / 0)
                     ~~~^~~
ZeroDivisionError: division by zero
Before the error
Traceback (most recent call last):
  File "/Users/you/python-course/crash.py", line 2, in <module>
    print("Result:", 10 / 0)
                     ~~~^~~
ZeroDivisionError: division by zero
חלק בהודעהPart of the messageמה הוא אומרWhat it says
File ".../crash.py", line 2File ".../crash.py", line 2איפה: הקובץ והשורהWhere: the file and the line
השורה עם ~~^~~The line with ~~^~~הקוד עצמו, והסימון מראה את החלק הבעייתיThe code itself, with the problem part marked
ZeroDivisionErrorZeroDivisionErrorמה: סוג השגיאהWhat: the error type
division by zerodivision by zeroהסבר קצר באנגליתA short explanation

שימו לב: השורה הראשונה כן הודפסה. Python ביצעה את שורה 1, נפלה בשורה 2, ושורה 3 לא רצה בכלל. טעות בקוד נקראת באג, ומציאת באגים היא חלק רגיל לגמרי מהעבודה, גם של מתכנתים ותיקים.

Notice: the first line was printed. Python ran line 1, crashed on line 2, and line 3 never ran. A mistake in code is called a bug, and finding bugs is a perfectly normal part of the job, even for experienced developers.

טיפTip

שיטת 3 השאלות לכל שגיאה: (1) מה הסוג? (2) איפה (שורה)? (3) מה שונה בשורה הזו ממה שהתכוונתי? במפגש 11 נעמיק עם Traceback ארוכים.

The 3-question method for any error: (1) what type? (2) where (line)? (3) what is different on that line from what I meant? In session 11 we go deep into long tracebacks.

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

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

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

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