๐งพ Project Overview
In this project, you will build a Python Expense Tracker App that allows users to:

โ Add new expenses
โ View all expenses
โ Search by category
โ Calculate total spending
โ Save all data automatically using a JSON file
โ Provide a clean menu-based interface
This project teaches:
- JSON handling
- File operations
- Lists & dictionaries
- Functions
- Loops and user input
Final Output: A fully functional console application that stores expenses permanently.
๐งฑ 1. Data Structure
Each expense will be stored as a dictionary:
{
"amount": 25.5,
"category": "Food",
"note": "Lunch",
"date": "2025-01-12"
}
All expenses are stored inside expenses.json as a list.
๐ฆ 2. Full Python Code (expense_tracker.py)
๐ Create a file named:
expense_tracker.py
๐ Create also an empty JSON file:
expenses.json
Now paste this full code:
import json
import os
from datetime import datetime
# Load existing expenses from JSON file
def load_expenses():
if os.path.exists("expenses.json"):
with open("expenses.json", "r") as file:
try:
return json.load(file)
except json.JSONDecodeError:
return []
return []
# Save expenses to JSON file
def save_expenses(expenses):
with open("expenses.json", "w") as file:
json.dump(expenses, file, indent=4)
# Add a new expense
def add_expense(expenses):
amount = float(input("Enter amount: "))
category = input("Enter category (Food, Travel, Bills...): ").capitalize()
note = input("Enter a short note: ")
date = datetime.now().strftime("%Y-%m-%d")
expense = {
"amount": amount,
"category": category,
"note": note,
"date": date
}
expenses.append(expense)
save_expenses(expenses)
print("โ Expense added successfully!\n")
# View all expenses
def view_expenses(expenses):
if not expenses:
print("No expenses found.\n")
return
print("\n๐ All Expenses:")
for i, exp in enumerate(expenses, start=1):
print(f"{i}. {exp['date']} | ${exp['amount']} | {exp['category']} โ {exp['note']}")
print()
# Search expenses by category
def search_by_category(expenses):
cat = input("Enter category to search: ").capitalize()
results = [exp for exp in expenses if exp["category"] == cat]
if not results:
print("No expenses found for this category.\n")
return
print(f"\n๐ Expenses in '{cat}':")
for exp in results:
print(f"- {exp['date']} | ${exp['amount']} | {exp['note']}")
print()
# Calculate total spending
def total_spent(expenses):
total = sum(exp["amount"] for exp in expenses)
print(f"\n๐ฐ Total spending: ${total}\n")
# Main menu
def main():
expenses = load_expenses()
while True:
print("๐ Expense Tracker App")
print("1. Add Expense")
print("2. View All Expenses")
print("3. Search by Category")
print("4. Total Spending")
print("5. Exit")
choice = input("๐ Choose an option (1-5): ")
if choice == "1":
add_expense(expenses)
elif choice == "2":
view_expenses(expenses)
elif choice == "3":
search_by_category(expenses)
elif choice == "4":
total_spent(expenses)
elif choice == "5":
print("Goodbye!")
break
else:
print("โ Invalid option. Try again.\n")
if __name__ == "__main__":
main()
๐ฅ 3. How to Run the Program
- Save both files in the same folder:
expense_tracker.pyexpenses.json(empty file)
- Open CMD in that folder.
- Run:
python expense_tracker.py
You will see:
๐ Expense Tracker App
1. Add Expense
2. View All Expenses
3. Search by Category
4. Total Spending
5. Exit
๐ Choose an option (1-5):
๐ 4. Example Usage
โ Add Expense:
Enter amount: 12.5
Enter category: Food
Enter note: Sandwich
โ Expense added successfully!
๐ View All:
1. 2025-01-12 | $12.5 | Food โ Sandwich
๐ Search Category:
Enter category: Food
- 2025-01-12 | $12.5 | Sandwich
๐ฐ Total Spending:
Total spending: $12.5
๐ง 5. Summary
| Feature | Description |
|---|---|
| Add Expense | Add amount, category, note, date |
| View Expenses | List all recorded expenses |
| Category Search | Filter by category |
| Total Spending | Auto-calculates all expenses |
| JSON Storage | Saves data permanently |
๐ Project Completed!
This is a real, practical Python app that can evolve into:
โ GUI version (Tkinter)
โ Mobile app (Kivy)
โ Dashboard with charts (Matplotlib)
โ Cloud-based version