Simple Expense Tracker in Python with Source Code

Managing expenses is a crucial aspect of personal finance. While there are numerous sophisticated tools available online, building a basic expense tracker can be an excellent project for beginners to grasp programming concepts. In this blog post, we'll walk you through creating a rudimentary expense tracker using Python. 


Basic understanding of Python, especially data structures like lists and dictionaries. Familiarity with file operations for data persistence. 

Steps to Create the Expense Tracker

Setup the Basic Structure: Set up a menu-driven system allowing users to add expenses, view expenses, and exit. 

Taking User Input: Depending on the choice, take necessary inputs from the user. 

Data Storage: Use a list of dictionaries to store individual expenses. 

Data Persistence: Use file operations to save and retrieve the data so that the user can resume where they left off. 

Source Code

import json

FILENAME = "expenses.json"

def load_expenses():
        with open(FILENAME, "r") as file:
            return json.load(file)
    except (FileNotFoundError, json.JSONDecodeError):
        return []

def save_expenses(expenses):
    with open(FILENAME, "w") as file:
        json.dump(expenses, file)

def add_expense(expenses):
    date = input("Enter the date (YYYY-MM-DD): ")
    name = input("Enter the name of the expense: ")
    amount = float(input("Enter the amount: "))
    expense = {"date": date, "name": name, "amount": amount}
    print(f"Expense added for {name} on {date} with amount ${amount:.2f}.")

def view_expenses(expenses):
    for expense in expenses:
        print(f"Date: {expense['date']}, Name: {expense['name']}, Amount: ${expense['amount']:.2f}")

def main():
    expenses = load_expenses()
    while True:
        print("\nExpense Tracker Menu:")
        print("1. Add Expense")
        print("2. View Expenses")
        print("3. Exit")
        choice = input("Enter your choice: ")
        if choice == "1":
        elif choice == "2":
        elif choice == "3":
            print("Invalid choice. Please select from the menu.")

if __name__ == "__main__":

How it Works

Loading and Saving Expenses: The load_expenses function reads the existing expenses from a file, and the save_expenses function writes the current expenses to a file. 

Adding an Expense: The add_expense function lets users input the details of an expense and appends it to our list of expenses. 

Viewing All Expenses: The view_expenses function displays all recorded expenses. 

Main Loop: The main function serves as our main program loop where users interact with our menu.


Expense Tracker Menu:
1. Add Expense
2. View Expenses
3. Exit
Enter your choice: 1
Enter the date (YYYY-MM-DD): 2023-05-25
Enter the name of the expense: Lunch
Enter the amount: 10.50
Expense added for Lunch on 2023-05-25 with amount $10.50.

Expense Tracker Menu:
1. Add Expense
2. View Expenses
3. Exit
Enter your choice: 2
Date: 2023-05-25, Name: Lunch, Amount: $10.50


Creating a basic expense tracker offers a glimpse into data handling, user interaction, and file operations in Python. As a next step, consider expanding functionalities—perhaps introduce categories for expenses, implement budgeting features, or build a graphical interface using Tkinter or PyQt. This foundational project opens doors to a myriad