כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
תרגיליםExercises
תרגילExercise 1
הלוך ושובThere and back
שמרו את המילון settings לקובץ settings.json (עם indent=2), טענו אותו לתוך משתנה חדש והדפיסו True אם הם שווים, ואת theme.
Save the dict settings to settings.json (with indent=2), load it into a new variable and print True if they're equal, plus theme.
איך הפלט אמור להיראות?What should the output look like?
True dark
main.py
רמזHint 1:with open("settings.json", "w", encoding="utf-8") as f: json.dump(settings, f, indent=2)with open("settings.json", "w", encoding="utf-8") as f: json.dump(settings, f, indent=2)
כתבו json_to_csv(json_path, csv_path) שקוראת רשימת מילונים מ-JSON וכותבת אותה ל-CSV, כשהעמודות הן המפתחות של המילון הראשון. החזירו את מספר השורות שנכתבו.
Write json_to_csv(json_path, csv_path) reading a list of dicts from JSON and writing it to CSV, with the first dict's keys as columns. Return the number of rows written.
איך הפלט אמור להיראות?What should the output look like?
כתבו safe_load(path, default) שמחזירה את התוכן של קובץ JSON, או את default אם הקובץ לא קיים או פגום (ובמקרה של קובץ פגום מדפיסה broken: <path>).
Write safe_load(path, default) returning a JSON file's content, or default if the file is missing or broken (and for a broken file prints broken: <path>).
איך הפלט אמור להיראות?What should the output look like?
מדף, צעד 21: JSON ויצוא ל-CSVShelf, step 21: JSON and CSV export
פרויקט מלווהCompanion project
כתבו save(shelf, path) ו-load(path) ל-JSON (load מחזירה [] אם אין קובץ), ו-export_csv(shelf, path) שכותבת title,author,pages בסדר הזה (ומתעלמת משדות אחרים). שמרו, טענו וייצאו מדף של שני ספרים.
Write save(shelf, path) and load(path) for JSON (load returns [] if there's no file), and export_csv(shelf, path) writing title,author,pages in that order (ignoring other fields). Save, load and export a two-book shelf.
איך הפלט אמור להיראות?What should the output look like?
title,author,pages
Dune,Frank Herbert,412
הנסיך הקטן,Antoine de Saint-Exupéry,96
title,author,pages
Dune,Frank Herbert,412
The Little Prince,Antoine de Saint-Exupéry,96
shelf.py
רמזHint 1:extrasaction="ignore" מדלג על שדות שלא בעמודות.extrasaction="ignore" skips fields not in the columns.
שני קבצי CSV של רשימות קריאה (a.csv, b.csv, עמודות title,rating). כתבו merge(paths) שמחזירה רשימת מילונים {title, rating, votes} כאשר rating הוא הממוצע (מעוגל ל-1) של כל הדירוגים לאותה כותרת (בלי רגישות לאותיות ורווחים), ו-votes כמה דירוגים היו. מיינו לפי rating מהגבוה, ואז לפי כותרת.
Two reading-list CSV files (a.csv, b.csv, columns title,rating). Write merge(paths) returning a list of dicts {title, rating, votes} where rating is the average (rounded to 1) of all ratings for that title (ignoring case and spaces), and votes is how many there were. Sort by rating high to low, then by title.
איך הפלט אמור להיראות?What should the output look like?
כתבו to_json(obj) שממירה כל אובייקט לטיפוסים ש-JSON מכיר, באופן רקורסיבי: dataclass → מילון (asdict), set → רשימה ממוינת, date → isoformat(), מילון → מילון עם ערכים מומרים, רשימה/טאפל → רשימה. השתמשו בה עם json.dumps(..., default=to_json) או ישירות.
Write to_json(obj) that converts any object into JSON-friendly types, recursively: dataclass → dict (asdict), set → sorted list, date → isoformat(), dict → dict with converted values, list/tuple → list. Use it with json.dumps(..., default=to_json) or directly.
איך הפלט אמור להיראות?What should the output look like?