PYTHON PROJECT 07

Expense Tracker

Learn how to build an Expense Tracker using Python lists, dictionaries, functions, loops and calculations.

1. Project Introduction

An Expense Tracker helps users record their income and expenses and calculate their current balance.

In this project, we will learn how to store transaction information and calculate financial totals.

This is an educational project for learning Python. It is not intended to replace professional accounting or financial software.

2. Understanding a Transaction

Every transaction can contain information such as:

transaction = {
    "description": "Salary",
    "amount": 30000,
    "type": "income"
}

3. Creating a Transaction List

Multiple transactions can be stored inside a list.

transactions = []

We can add transaction dictionaries to this list.

4. Adding a Transaction

The append() method adds an item to a list.

transactions.append({
    "description": "Salary",
    "amount": 30000,
    "type": "income"
})

5. Recording an Expense

transactions.append({
    "description": "Electricity Bill",
    "amount": 2500,
    "type": "expense"
})

The transaction type tells us whether money was received or spent.

6. Calculating Total Income

We can loop through all transactions and add the amounts where the type is income.

income = 0

for transaction in transactions:

    if transaction["type"] == "income":

        income += transaction["amount"]

7. Calculating Total Expenses

expenses = 0

for transaction in transactions:

    if transaction["type"] == "expense":

        expenses += transaction["amount"]

8. Calculating Balance

Balance can be calculated by subtracting expenses from income.

balance = income - expenses

9. Creating an Add Transaction Function

def add_transaction(
    description,
    amount,
    transaction_type
):

    transactions.append({
        "description": description,
        "amount": amount,
        "type": transaction_type
    })

10. Displaying Transactions

A loop can be used to display every transaction.

for transaction in transactions:

    print(
        transaction["description"],
        transaction["amount"],
        transaction["type"]
    )

11. Deleting a Transaction

The remove() method can remove an item from a list.

transactions.remove(transaction)

In a complete application, each transaction should have a unique identifier so that the correct item can be removed.

12. Using Functions for Calculations

def calculate_balance():

    income = 0
    expenses = 0

    for transaction in transactions:

        if transaction["type"] == "income":
            income += transaction["amount"]

        else:
            expenses += transaction["amount"]

    return income, expenses, income - expenses

13. Taking User Input

description = input(
    "Enter description: "
)

amount = float(
    input("Enter amount: ")
)

transaction_type = input(
    "Enter type (income/expense): "
)

14. Input Validation

Amount should normally be greater than zero.

if amount <= 0:

    print("Amount must be greater than zero.")

The transaction type should also be restricted to the supported values.

if transaction_type not in [
    "income",
    "expense"
]:

    print("Invalid transaction type.")

15. Complete Project Flow

01 Create List
02 Add Transaction
03 Store Details
04 Validate Input
05 Calculate Income
06 Calculate Expenses
07 Calculate Balance
08 Display Transactions
09 Delete Transaction
10 Update Summary

16. Concepts You Practiced

17. Run Complete Project 🚀

You have learned the main concepts behind an Expense Tracker. Now open the complete project.

Run Complete Project