Python Tutorial

Python Project: Expense Tracker

Record amounts by category, print a table, and compute totals and the largest spend.

What you are building

You record expenses as pairs: a category and an amount. The program prints a table of every row, a total for each category, the grand total, and the single largest spend.

Work in the Python editor at /try. Leave HTML, C, and C++ for their own tracks. The numbers are listed in the script so Run prints the report at once.

Skills used

  • A list of (category, amount) tuples
  • A loop that prints a table of rows
  • A dict that sums amounts per category
  • sum for the grand total
  • max with a key to find the largest row

Store rows and print a table

A tuple is enough for one spend: the category does not change, the amount does not change. Keep all rows in one list. Print a header, then each pair, then a count of rows.

Example

expenses = [
    ("Food", 12.50),
    ("Transit", 3.20),
    ("Food", 8.75),
    ("Books", 24.00),
]

print(f"{'category':<12} {'amount':>8}")
for category, amount in expenses:
    print(f"{category:<12} {amount:>8.2f}")
print("Rows:", len(expenses))

Two Food rows stay two rows. Do not merge them yet. The table is the ledger. Totals come next, from the same list.

Totals by category

Walk the list once. For each category, add the amount to a dict. Missing keys start at 0. The grand total is the sum of the amounts, not a second pass over the dict, unless you prefer that.

Example

expenses = [
    ("Food", 12.50),
    ("Transit", 3.20),
    ("Food", 8.75),
    ("Books", 24.00),
]

by_cat = {}
for category, amount in expenses:
    by_cat[category] = by_cat.get(category, 0) + amount

print(by_cat)
print("Grand total:", sum(amount for _, amount in expenses))

Use amount as a float, not a string. If you later read text, wrap it withfloat(...) once when you build the list. Adding strings would concatenate, and the total would be nonsense.

Complete program

The full report prints the ledger, the category totals, the largest single row, and the grand total. Run it at /try and check the Books line: 24.00 is the largest spend.

Example

expenses = [
    ("Food", 12.50),
    ("Transit", 3.20),
    ("Food", 8.75),
    ("Books", 24.00),
    ("Transit", 3.20),
    ("Food", 15.10),
]

def print_table(rows):
    print(f"{'category':<12} {'amount':>8}")
    print("-" * 21)
    for category, amount in rows:
        print(f"{category:<12} {amount:>8.2f}")

by_cat = {}
for category, amount in expenses:
    by_cat[category] = by_cat.get(category, 0) + amount

largest = max(expenses, key=lambda row: row[1])
grand = sum(amount for _, amount in expenses)

print("Expense tracker")
print()
print_table(expenses)
print()
print("Totals by category")
print(f"{'category':<12} {'total':>8}")
print("-" * 21)
for category in sorted(by_cat):
    print(f"{category:<12} {by_cat[category]:>8.2f}")
print()
print("Largest spend:", largest[0], f"{largest[1]:.2f}")
print("Grand total:  ", f"{grand:.2f}")
print("Row count:    ", len(expenses))
CategoryTotal
Books24.00
Food36.35
Transit6.40
Grand total66.75

Food is 12.50 + 8.75 + 15.10. Transit is 3.20 twice. Sorted category names keep the second table stable from run to run.

Common mistakes

  • Using max(expenses) without a key. Tuples compare the category string first, so Books would not always win.
  • Rounding each row before you add. Round when you print. Sum the original amounts.
  • Updating by_cat with = instead of +. The second Food row would replace the first instead of adding to it.
  • Printing money with print(amount) only. 8.75 is fine; 3.2 looks better as 3.20 with .2f.
  • Mixing currencies in one list with no tag. This project assumes one unit, such as dollars.

How to extend / Practice tasks

Keep the six demo rows until the 66.75 total matches.

  1. Add ("Food", 4.15) and confirm the Food total becomes 40.50 and the grand total becomes 70.90.
  2. Print the category with the highest total, not only the highest single row. Usemax(by_cat, key=by_cat.get).
  3. Filter the ledger to one category before printing: a helperonly(rows, name) that returns matching tuples, then print Food only.

📘 Real-World Deep Dive

An expense tracker is where you first feel the payoff of grouping and aggregation: raw rows in, totals-per-category out. That "reduce a list of records to a summary" move is the heart of every report and dashboard.

What to build

Read a list of expenses, total them per category, and print a sorted summary with the grand total — the CLI ancestor of every budgeting app.

Real-Life Example

from collections import defaultdict

expenses = [
    ("food", 12.50), ("transport", 3.00), ("food", 8.25),
    ("rent", 900.00), ("transport", 2.75),
]

totals = defaultdict(float)
for category, amount in expenses:
    totals[category] += amount

for category, total in sorted(totals.items(), key=lambda kv: -kv[1]):
    print(f"{category:10} {total:8.2f}")
print(f"{'TOTAL':10} {sum(totals.values()):8.2f}")

defaultdict(float) removes the "is this the first time I have seen this category?" check entirely.

Expected Output

rent         900.00
food          20.75
transport      5.75
TOTAL        926.50

Common mistakes

  • Storing money as float accumulates rounding errors; for real accounting use decimal.Decimal or store integer cents.
  • dict[key] += amount throws KeyError on a new key — that's exactly what defaultdict (or Counter) prevents.
  • Sorting by category name when the user wants "biggest spend first" — sort by value, as above.

🚀 Performance & Best Practices

  • One pass builds every total (O(n)); no need to scan the list once per category.
  • collections.Counter can sum too, and its .most_common() gives the sorted ranking for free.
  • Keep parsing (reading CSV rows) separate from aggregating so you can test the math on hand-made data.

🧪 Try It Yourself

  1. Load the expenses from a CSV file with csv.reader instead of a hard-coded list.
  2. Add a month filter so the report only totals rows in a chosen month.
  3. Switch the totals to Decimal and prove the grand total is exact to the cent.

FAQ: Python Project: Expense Tracker

Common questions about this page.

What is Python Project: Expense Tracker?

Python Project: Expense Tracker is a Python Projects lesson that explains python expense project in Python. Record amounts by category, print a table, and compute totals and the largest spend. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run python expense project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn python expense project in this Python Projects Python lesson (Python Project: Expense Tracker).

How do I use python expense project in Python?

To use python expense project in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of python expense project?

This Python Project: Expense Tracker tutorial shows python expense project syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Python Project: Expense Tracker example for beginners

Yes. This page includes a beginner python expense project example you can copy and run. It is designed for searches such as "python expense project for beginners", "python expense project example", and "how to use python expense project".

What are common mistakes with python expense project?

Common python expense project mistakes include wrong syntax, mixing types, and skipping practice. Work through this Python Projects chapter in order, run every example, and check the output before moving on.

Why should I learn python expense project?

Python Project: Expense Tracker is used in real Python work. Learning python expense project helps you write clearer programs and continue the Python Projects tutorial on StudyGrid.

Is Python Project: Expense Tracker free to learn online?

Yes. You can learn python expense project free on StudyGrid (studygrid.in). This chapter is part of the Python Projects path and includes examples, syntax, and next-step links.