Python project 05
Python Expense Tracker Project
Last updated: 31 August 2026
Build a command-line expense tracker that records everyday spending, saves entries in a JSON file, and calculates the total. It is a practical beginner project for menus, lists, functions, decimal amounts, and local data storage.
Develop practical Python skills in VizagView all project ideas
What you will build
The app lets users add an expense with a category and note, view all saved entries, and see the total amount spent. Data stays in expenses.json, so it can be reopened later.
Skills practised
- Lists and dictionaries
- Functions and menu loops
- JSON file storage
- Input validation with Decimal
Requirements
- Python 3.8 or later
- A terminal or command prompt
- No external packages
- About 30 minutes to build
Full Python code
Save the code as expense_tracker.py, then run it from a terminal.
"""A small command-line expense tracker that saves entries in expenses.json."""
import json
from datetime import date
from decimal import Decimal, InvalidOperation
from pathlib import Path
DATA_FILE = Path("expenses.json")
def load_expenses():
"""Return saved expenses, or an empty list when no data exists yet."""
if not DATA_FILE.exists():
return []
try:
return json.loads(DATA_FILE.read_text(encoding="utf-8"))
except json.JSONDecodeError:
print("The saved file could not be read. Starting with an empty list.")
return []
def save_expenses(expenses):
DATA_FILE.write_text(json.dumps(expenses, indent=2), encoding="utf-8")
def read_amount():
while True:
try:
amount = Decimal(input("Amount: Rs. ").strip())
if amount <= 0:
raise ValueError
return amount.quantize(Decimal("0.01"))
except (InvalidOperation, ValueError):
print("Enter a positive amount, for example 125.50.")
def add_expense(expenses):
category = input("Category (for example Food or Travel): ").strip() or "Other"
note = input("Short note: ").strip() or "No note"
amount = read_amount()
expenses.append(
{"date": str(date.today()), "category": category, "note": note, "amount": str(amount)}
)
save_expenses(expenses)
print("Expense saved.\n")
def show_expenses(expenses):
if not expenses:
print("No expenses have been saved yet.\n")
return
total = Decimal("0")
print("\nDate Category Amount Note")
print("-" * 54)
for item in expenses:
amount = Decimal(item["amount"])
total += amount
print(f"{item['date']:<11} {item['category'][:13]:<14} Rs. {amount:>8.2f} {item['note']}")
print("-" * 54)
print(f"Total spent: Rs. {total:.2f}\n")
def main():
expenses = load_expenses()
print("Simple Expense Tracker")
while True:
print("1. Add expense 2. View expenses 3. Quit")
choice = input("Choose an option: ").strip()
if choice == "1":
add_expense(expenses)
elif choice == "2":
show_expenses(expenses)
elif choice in {"3", "q", "quit", "exit"}:
print("Your expense data is saved in expenses.json.")
break
else:
print("Choose 1, 2, or 3.\n")
if __name__ == "__main__":
main()
How it works
load_expenses() reads saved data when it exists, while save_expenses() writes new entries to JSON. Amount validation rejects invalid, zero, and negative values before anything is stored. The view option converts saved amounts to Decimal values and calculates a reliable total.
Run the project
- Open a terminal in the project folder.
- Run
python expense_tracker.py. - Choose
1to add an expense and2to view your entries. - Keep
expenses.jsonin the same folder to retain records.
Notes for your notebook
- JSON: a simple format for structured local data.
- Decimal: prevents common money-display issues from floating-point values.
- Privacy: do not commit real financial records to GitHub.
Ways to extend it
Add monthly budgets, category totals, edit and delete options, CSV export, or a simple Tkinter interface.
What to learn next
Practise Python functions and choose another project from the Softenant project library.