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

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. מה ההבדל בין import math ל-from math import sqrt?What's the difference between import math and from math import sqrt?

2. מתי __name__ == "__main__" נכון?When is __name__ == "__main__" true?

3. למה משתמשים ב-venv?Why use a venv?

תרגיליםExercises

תרגילExercise 1

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

השתמשו ב-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).

איך הפלט אמור להיראות?What should the output look like?
Emma
['Matilda', 'Ulysses', 'Emma', 'Dune']
main.py
רמזHint 1: random.seed(1) בהתחלה.random.seed(1) first.
רמזHint 2: random.choice(books) ו-random.shuffle(books).random.choice(books) and random.shuffle(books).
לפתרוןSolution
תרגילExercise 2

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

ספר נשאל ב-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.

איך הפלט אמור להיראות?What should the output look like?
10/12/2026
Thursday
main.py
רמזHint 1: date(2026, 11, 19) + timedelta(weeks=3)date(2026, 11, 19) + timedelta(weeks=3)
רמזHint 2: strftime("%d/%m/%Y") ו-strftime("%A").strftime("%d/%m/%Y") and strftime("%A").
לפתרוןSolution
תרגילExercise 3

CounterCounter

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

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

איך הפלט אמור להיראות?What should the output look like?
e: 5
main.py
רמזHint 1: Counter(text.replace(" ", "").lower())Counter(text.replace(" ", "").lower())
רמזHint 2: .most_common(1)[0].most_common(1)[0]
לפתרוןSolution
תרגילExercise 4

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

הקוד כותב קובץ 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).

איך הפלט אמור להיראות?What should the output look like?
26.22
main.py
רמזHint 1: הוסיפו את הפונקציה לתוך המחרוזת שנכתבת לקובץ.Add the function inside the string written to the file.
רמזHint 2: from units import km_to_milesfrom units import km_to_miles
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

פצלו: כתבו (מתוך הקוד, כמו בהרצאה) מודול 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__":.

איך הפלט אמור להיראות?What should the output look like?
Dune by Frank Herbert
main.py
רמזHint 1: מחרוזת רב-שורתית עם """ נוחה לכתיבת המודול.A triple-quoted string is handy for writing the module.
רמזHint 2: import storage ואז storage.save(...).import storage then storage.save(...).
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

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

כתבו 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.

איך הפלט אמור להיראות?What should the output look like?
76
0
main.py
רמזHint 1: nxt = date(today.year, month, day)nxt = date(today.year, month, day)
רמזHint 2: אם nxt < today: שנה הבאה.If nxt < today: next year.
לפתרוןSolution
אתגרChallenge 2

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

כתבו 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.

איך הפלט אמור להיראות?What should the output look like?
12 True
main.py
רמזHint 1: string.ascii_letters + string.digitsstring.ascii_letters + string.digits
רמזHint 2: "".join(secrets.choice(chars) for _ in range(n)) בתוך while."".join(secrets.choice(chars) for _ in range(n)) inside a while.
לפתרוןSolution

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