Step up to a small but complete application: an expense tracker that records spending, persists it to a JSON file so it survives restarts, and reports totals by category. It combines collections, files, functions and a command loop — the shape of a real tool.
What to build
- Model each expense as a dict (
amount,category,note); keep a list of them. - Persist the list to a JSON file on every change so data survives restarts.
- Summarise totals per category using a dict accumulator.
- Offer
add/list/summary/quitcommands.
Persistence scaffold
import jsonfrom pathlib import PathFILE = Path("expenses.json")def load():return json.loads(FILE.read_text()) if FILE.exists() else []def save(expenses):FILE.write_text(json.dumps(expenses, indent=2))def summary(expenses):totals = {}for e in expenses:totals[e["category"]] = totals.get(e["category"], 0) + e["amount"]return totals
Stretch goals
Add date filtering (this month's spend), a budget warning per category, or export the summary to CSV (Module 6).
Key takeaways
- 01Combines lists, dicts, JSON persistence and functions into a real app.
- 02json load/dump gives durable storage with no database.
- 03Aggregation by category uses a dict accumulator (get with default 0).
- 04A command loop (add/list/summary/quit) makes it interactive.
- 05Stretch it with date filters, budgets, or CSV export.
