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

PythonPython
13
חלקPart 2: פונקציות וקוד אמיתיFunctions & real code כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מודולים, pip ו-venvModules, pip & venv

import, מודולים משלך, הספרייה הסטנדרטית, התקנת חבילות וסביבה וירטואלית.

import, your own modules, the standard library, installing packages and virtual environments.

נסו לפתור לבד לפני שמציצים. בנגן: חיצים במקלדת או הכפתורים. כל צעד מראה איפה (קובץ ושורה), מה כותבים ולמה.Try on your own before peeking. In the player use the arrow keys or the buttons. Each step shows where (file and line), what to write, and why.
תרגילExercise 1

מגריל ספריםBook picker

תזכורת: מה ביקשוReminder: the task

השתמשו ב-random עם seed(1) כדי לבחור ספר אקראי מהרשימה ולהדפיס אותו, ואז הדפיסו את הרשימה מעורבבת (shuffle).

Use random with seed(1) to pick a random book from the list and print it, then print the list shuffled (shuffle).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
import random

random.seed(1)
books = ["Dune", "Emma", "Matilda", "Ulysses"]
print(random.choice(books))
random.shuffle(books)
print(books)
פלטOutput
Emma
['Matilda', 'Ulysses', 'Emma', 'Dune']

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 2

תאריך החזרהReturn date

תזכורת: מה ביקשוReminder: the task

ספר נשאל ב-19/11/2026 לשלושה שבועות. הדפיסו את תאריך ההחזרה בפורמט DD/MM/YYYY, ואת היום בשבוע באנגלית.

A book was borrowed on 19/11/2026 for three weeks. Print the return date as DD/MM/YYYY, and the weekday in English.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
from datetime import date, timedelta

due = date(2026, 11, 19) + timedelta(weeks=3)
print(due.strftime("%d/%m/%Y"))
print(due.strftime("%A"))
פלטOutput
10/12/2026
Thursday

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 3

CounterCounter

תזכורת: מה ביקשוReminder: the task

השתמשו ב-collections.Counter כדי למצוא את האות הנפוצה ביותר במשפט (בלי רווחים, באותיות קטנות). הדפיסו letter: count.

Use collections.Counter to find the most common letter in a sentence (no spaces, lowercase). Print letter: count.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
from collections import Counter

text = "The Little Prince loves his rose"
letter, n = Counter(text.replace(" ", "").lower()).most_common(1)[0]
print(f"{letter}: {n}")
פלטOutput
e: 5

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 4

מודול כליםA tools module

תזכורת: מה ביקשוReminder: the task

הקוד כותב קובץ units.py. הוסיפו לו פונקציה km_to_miles(km) (×0.621371, מעוגל ל-2), ייבאו אותה, והדפיסו את km_to_miles(42.195).

The code writes units.py. Add a function km_to_miles(km) to it (×0.621371, rounded to 2), import it, and print km_to_miles(42.195).

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
import importlib
from pathlib import Path
Path("units.py").write_text(
    "def km_to_miles(km):\n    return round(km * 0.621371, 2)\n",
    encoding="utf-8",
)
importlib.invalidate_caches()
from units import km_to_miles
print(km_to_miles(42.195))
פלטOutput
26.22

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

תרגילExercise 5

מדף, צעד 13: מודוליםShelf, step 13: modules

תזכורת: מה ביקשוReminder: the task

פצלו: כתבו (מתוך הקוד, כמו בהרצאה) מודול storage.py עם save ו-load מהמפגש הקודם, ומודול ראשי שמייבא אותו, שומר ספר אחד, טוען ומדפיס Dune by Frank Herbert. בתוך storage הוסיפו בדיקה עצמית ב-if __name__ == "__main__":.

Split: write (from code, as in the lecture) a storage.py module with save and load from last session, and a main module that imports it, saves one book, loads and prints Dune by Frank Herbert. Inside storage add a self-check in if __name__ == "__main__":.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
import importlib
from pathlib import Path

Path("storage.py").write_text('''from pathlib import Path

def save(shelf, path):
    with open(path, "w", encoding="utf-8") as f:
        for b in shelf:
            f.write(b["title"] + "|" + b["author"] + "\\n")

def load(path):
    if not Path(path).exists():
        return []
    with open(path, encoding="utf-8") as f:
        return [dict(zip(("title", "author"), line.strip().split("|"))) for line in f]

if __name__ == "__main__":
    save([{"title": "T", "author": "A"}], "_t.txt")
    assert load("_t.txt") == [{"title": "T", "author": "A"}]
    print("storage ok")
''', encoding="utf-8")
importlib.invalidate_caches()

import storage
storage.save([{"title": "Dune", "author": "Frank Herbert"}], "shelf.txt")
for b in storage.load("shelf.txt"):
    print(f"{b['title']} by {b['author']}")
פלטOutput
Dune by Frank Herbert

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 1

ימים עד יום הולדתDays until a birthday

תזכורת: מה ביקשוReminder: the task

כתבו days_until(today, month, day) שמחזירה כמה ימים נשארו ליום ההולדת הבא (אם עבר השנה, בשנה הבאה; אם היום, 0). הדפיסו עבור היום 19/11/2026 ויום הולדת 3/2 ו-19/11.

Write days_until(today, month, day) returning days until the next birthday (next year if it passed; 0 if today). Print it for today 19/11/2026 and birthdays 3/2 and 19/11.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
from datetime import date

def days_until(today, month, day):
    nxt = date(today.year, month, day)
    if nxt < today:
        nxt = date(today.year + 1, month, day)
    return (nxt - today).days

today = date(2026, 11, 19)
print(days_until(today, 2, 3))
print(days_until(today, 11, 19))
פלטOutput
76
0

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

אתגרChallenge 2

סיסמאות אקראיותRandom passwords

תזכורת: מה ביקשוReminder: the task

כתבו make_password(n) שמחזירה סיסמה באורך n מאותיות וספרות בעזרת secrets ו-string, ומוודאת שיש בה לפחות ספרה אחת (מנסה שוב אם לא). הדפיסו את האורך של סיסמה באורך 12 ואם יש בה ספרה.

Write make_password(n) returning an n-character password of letters and digits using secrets and string, guaranteeing at least one digit (retry if not). Print the length of a 12-character password and whether it has a digit.

סרטון מונפש: על מה לוחצים, מה כותבים ולמה, צעד אחר צעד.An animated movie: what to click, what to type and why, step by step.
איפהWhere
מהWhat
למהWhy
הפתרון המלא והפלט שלוFull solution and its output
main.py
import secrets
import string

def make_password(n):
    chars = string.ascii_letters + string.digits
    while True:
        pw = "".join(secrets.choice(chars) for _ in range(n))
        if any(c.isdigit() for c in pw):
            return pw

pw = make_password(12)
print(len(pw), any(c.isdigit() for c in pw))
פלטOutput
12 True

יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.

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