Put two weeks of Python together. Plan, build and test a small expense tracker that uses functions, classes, dictionaries, files, errors and comprehensions.
By the end of today you can
- Break a project into small, testable pieces
- Model data with a class and store it as JSON
- Validate input with exceptions
- Produce summaries with dictionaries and comprehensions
- Test your code with assert
Congratulations, you've reached the last day! Today you'll build a complete small program, an expense tracker, using almost everything from the past two weeks. Run each step as you go: every block builds on the previous ones.
The plan
Before writing code, break the problem into pieces:
| Skill | Where it's used today |
|---|---|
| Functions (Day 9) | Reports and helpers |
| Modules (Day 10) | json, datetime, collections |
| Files & errors (Day 11) | Saving, loading, validation |
| Classes (Day 12) | Expense and Tracker |
| Pythonic code (Day 13) | Comprehensions, sorted(key=…) |
Step 1: the Expense class
Each expense is a small object that validates itself when it's created:
@classmethod makes from_dict an alternative way to create an Expense from saved data: Expense.from_dict({...}).
Step 2: the Tracker
The tracker holds the list of expenses and knows how to add, report and save:
Step 3: save, load and a report
Save the data, load it back as if the program restarted, and print a small report:
Step 4: test it
Professional code comes with tests. assert stops with an AssertionError if something isn't as expected. If every assert passes, nothing interrupts and you see the final message:
Where to go next
Recap: your two weeks
- Days 1–4: running code, variables and types, strings, operators.
- Days 5–8: decisions, loops, lists and tuples, dictionaries and sets.
- Days 9–11: functions, modules, files and error handling.
- Days 12–14: classes, Pythonic style, and a complete project with tests.
You can now read, write and structure real Python programs. Well done! 🎉
Knowledge check
Test what you learned
4 questions · pick an answer to see if you're right
- 1
Why build the tracker as several small functions and a class instead of one long script?
- 2
What does assert total_by_category(...)["food"] == 23.5 do if the value is wrong?
- 3
Why convert Expense objects to dictionaries before saving?
- 4
Where is the best place to reject a negative amount?
Finished today's lesson?