Project 3: an expense tracker
We build a tool that's useful every day: record your expenses, group them by category and check your monthly budget. Each expense is a dataclass (label, amount, category); an ExpenseTracker class keeps them and provides the calculations.
Good project habits: one method = one responsibility (add, total, by_category, report), reject invalid data at the door with raise ValueError, and polish the output with f-strings ({amount:>8.2f} right-aligns on 8 characters with 2 decimals).
from dataclasses import dataclass
@dataclass
class Expense:
label: str
amount: float
category: str
expenses = [Expense("Metro pass", 86.4, "transport"), Expense("Pizza", 11.5, "food")]
for e in expenses:
print(f"{e.label:<12}{e.amount:>8.2f} € [{e.category}]")