Expense Tracker Project in Python (with source code)

← Back to Projects

Expense Tracker in Python.

This Expense Tracker in Python is an intermediate-level programming project designed to help you manage and analyze daily expenses efficiently.

Using Python file handling and JSON storage, this project teaches you how to build a real-world command-line application that records expenses, categorizes spending, and generates summaries. It is ideal for learners looking to strengthen their Python skills with practical, hands-on experience.

About the project:This is the project for an Expense Tracker in Python

This program will allow you to record expenses with a description, amount, and category. It will save all the expense data to a file, so your records are persistent.


Key Features of the Python Expense Tracker

  • Command-line based interactive menu system
  • Add, view, and summarize expenses easily
  • Persistent storage using JSON file handling
  • Expense categorization for better tracking
  • Beginner-friendly yet practical real-world project
  • This Python script is a complete and interactive Expense Tracker application.
  • It provides a menu-driven interface for logging and reviewing your spending.
  • All your recorded expenses are saved to a expenses.json file, so they'll be preserved between sessions.


Who Should Use This Project?

This Python Expense Tracker project is suitable for students, beginners, and intermediate Python developers who want to practice file handling, dictionaries, and menu-driven programs. It is also useful for anyone looking to understand how real-world expense tracking applications work at a basic level.



How to use this program:

You can run this script, and it will guide you through adding expenses and viewing summaries of your spending.


Project Level: Intermediate

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# expense_tracker.py

import json
import os
from datetime import datetime

# Filename for storing expenses
EXPENSES_FILE = "expenses.json"

def load_expenses():
    """
    Loads expenses from a JSON file. Returns a dictionary of expenses.
    If the file doesn't exist, it returns a new dictionary.
    """
    if os.path.exists(EXPENSES_FILE) and os.stat(EXPENSES_FILE).st_size > 0:
        try:
            with open(EXPENSES_FILE, 'r') as f:
                return json.load(f)
        except json.JSONDecodeError:
            print("Warning: Expenses file is corrupted or empty. Starting with a new tracker.")
            return {}
    return {}

def save_expenses(expenses):
    """
    Saves the expenses dictionary to a JSON file.
    """
    with open(EXPENSES_FILE, 'w') as f:
        json.dump(expenses, f, indent=4)
    print("Expenses saved successfully.")

def add_expense(expenses):
    """
    Prompts the user for a new expense and adds it to the expenses dictionary.
    """
    description = input("Enter description: ").strip()
    if not description:
        print("Description cannot be empty. Expense not added.")
        return

    try:
        amount = float(input("Enter amount: ").strip())
        category = input("Enter category (e.g., Food, Transport, Bills): ").strip()
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        # Create a unique ID for each expense
        expense_id = str(len(expenses) + 1)

        expenses[expense_id] = {
            "description": description,
            "amount": amount,
            "category": category,
            "timestamp": timestamp
        }
        print(f"Expense '{description}' added with ID: {expense_id}")
    except ValueError:
        print("Invalid amount. Please enter a number.")

def view_expenses(expenses):
    """
    Displays all recorded expenses in a clean, readable format.
    """
    if not expenses:
        print("\nNo expenses recorded yet.")
        return

    print("\n--- All Expenses ---")
    for expense_id, details in expenses.items():
        print(f"ID: {expense_id}")
        print(f"  Description: {details.get('description', 'N/A')}")
        print(f"  Amount: ${details.get('amount', 0):.2f}")
        print(f"  Category: {details.get('category', 'N/A')}")
        print(f"  Date: {details.get('timestamp', 'N/A')}")
        print("-" * 20)
    print("--------------------")

def summarize_expenses(expenses):
    """
    Calculates and displays a summary of spending by category and the total amount.
    """
    if not expenses:
        print("\nNo expenses recorded to summarize.")
        return

    total_expenses = 0.0
    category_summary = {}

    for details in expenses.values():
        amount = details.get('amount', 0)
        category = details.get('category', 'Other')
        
        total_expenses += amount
        
        # Add to the category summary
        category_summary[category] = category_summary.get(category, 0) + amount

    print("\n--- Expense Summary ---")
    print(f"Total Expenses: ${total_expenses:.2f}")
    print("\nSpending by Category:")
    for category, amount in category_summary.items():
        print(f"  {category}: ${amount:.2f}")
    print("-----------------------")

def main():
    """
    Main function to run the Expense Tracker app with a menu.
    """
    expenses = load_expenses()

    print("--- Python Expense Tracker ---")

    while True:
        print("\nMenu:")
        print("1. Add a new expense")
        print("2. View all expenses")
        print("3. View spending summary")
        print("4. Quit and Save")

        choice = input("Enter your choice (1-4): ").strip()

        if choice == '1':
            add_expense(expenses)
        elif choice == '2':
            view_expenses(expenses)
        elif choice == '3':
            summarize_expenses(expenses)
        elif choice == '4':
            save_expenses(expenses)
            print("Exiting Expense Tracker. Goodbye!")
            break
        else:
            print("Invalid choice. Please enter a number between 1 and 4.")

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()





Sample Output

Below is a sample output showing how the Python Expense Tracker works when you run the program and interact with the menu.

--- Python Expense Tracker ---

Menu:
1. Add a new expense
2. View all expenses
3. View spending summary
4. Quit and Save

Enter your choice (1-4): 1
Enter description: Grocery Shopping
Enter amount: 45.50
Enter category (e.g., Food, Transport, Bills): Food
Expense 'Grocery Shopping' added with ID: 1

Menu:
1. Add a new expense
2. View all expenses
3. View spending summary
4. Quit and Save

Enter your choice (1-4): 3

--- Expense Summary ---
Total Expenses: $45.50

Spending by Category:
  Food: $45.50
-----------------------



How Data Is Stored (JSON Example)

This Python Expense Tracker stores all expense records in a local JSON file named expenses.json. Using JSON ensures that the data remains persistent and easy to read, even after the program is closed.

Each expense is saved with a unique ID and includes the description, amount, category, and timestamp. Below is an example of how the data looks inside the expenses.json file:

{
  "1": {
    "description": "Grocery Shopping",
    "amount": 45.5,
    "category": "Food",
    "timestamp": "2025-01-10 14:32:18"
  },
  "2": {
    "description": "Bus Fare",
    "amount": 2.75,
    "category": "Transport",
    "timestamp": "2025-01-10 18:05:42"
  }
}

This structure makes it easy to load, update, and summarize expenses using Python’s built-in json module.



Frequently Asked Questions (FAQs)

Is this Expense Tracker suitable for beginners?

Yes, this project is beginner-friendly but best suited for learners who already understand basic Python concepts such as functions, dictionaries, and file handling.

Which file stores the expense data?

All expense records are stored in a file named expenses.json, allowing the data to persist even after the program is closed.

Can I extend this project?

Yes, you can extend this project by adding features like monthly reports, CSV export, graphical interface, or database integration.

Does this project require external libraries?

No, the project uses only Python’s built-in libraries such as json, os, and datetime.



Related Python Projects