Python Basics Mini Project — Personal Expense Tracker
This mini project is designed for entry-level Python learners. It covers fundamental
concepts: variables, data types, lists/dictionaries, functions, control flow, file I/O (JSON),
and a simple command-line interface (CLI).
Learning Objectives
- Understand how to structure a small Python program.
- Work with lists and dictionaries to store records.
- Read from and write to files using JSON.
- Implement functions and a menu-driven loop.
- Practice error handling and basic input validation.
Prerequisites
- Python 3.8+ installed.
- Basic familiarity with running Python scripts in a terminal.
- (Optional) A code editor such as VS Code, PyCharm, or even Notepad.
Project Description
Build a small command-line Personal Expense Tracker that lets the user:
1. Add expenses (date, amount, category, optional description).
2. View all recorded expenses.
3. See a summary total by category.
4. Delete an expense.
5. Save/load data to/from a JSON file.
Estimated time
About 45–90 minutes depending on familiarity with Python.
Step-by-step Implementation
Step 1 — Create the project file
Create a new file named `expense_tracker.py`. We'll implement the entire program inside it.
Step 2 — Imports and constants
We will use `json` for saving data and `datetime` to default the date when left blank.
Step 3 — Load and save functions
Implement `load_expenses()` and `save_expenses(expenses)` to persist data in
`[Link]`.
Step 4 — Add expense
Implement `add_expense(expenses)` to collect user input (date, amount, category,
description) and append a dictionary to the list.
Step 5 — View expenses
Implement `view_expenses(expenses)` to print a numbered list of saved expenses.
Step 6 — Summary by category
Implement `summary_by_category(expenses)` to compute totals per category.
Step 7 — Delete expense
Implement `delete_expense(expenses)` to remove an entry by its number.
Step 8 — Main menu loop
Implement `main()` to repeatedly show a menu and call the above functions. Provide
options to save and exit.
Full Code (save as expense_tracker.py)
Sample run (example)
Example interaction (user input is after '>>'):
=== Personal Expense Tracker ===
1) Add expense
2) View expenses
3) Summary by category
4) Delete expense
5) Save and exit
6) Exit without saving
Choose an option: >> 1
Date (YYYY-MM-DD), leave blank for today: >>
Amount: >> 12.5
Category (e.g., food, transport): >> food
Description (optional): >> lunch
Expense added.
Choose an option: >> 2
Recorded expenses:
1. 2025-09-15 | food | 12.50 | lunch
Extension ideas (for practice)
- Add input validation for the date format using `[Link]()`.
- Support filtering by date range or category.
- Add a small report that shows weekly/monthly totals.
- Build a simple GUI using Tkinter.
- Export summary as CSV.
Notes for instructors or reviewers
This project is intentionally small so learners can quickly implement it and then extend it.
Encourage learners to write unit tests for the helper functions (e.g., summary computation).