Python3

Python script: Personal Finance Tracker (with budgeting and expense tracking)


import datetime

class PersonalFinanceTracker:
def __init__(self, budget):
self.budget = budget
self.expenses = []

def add_expense(self, amount, category):
self.expenses.append((amount, category, datetime.datetime.now()))

def get_total_expenses(self):
return sum([expense[0] for expense in self.expenses])

def get_expenses_by_category(self, category):
category_expenses = [expense[0] for expense in self.expenses if expense[1] == category]
return sum(category_expenses)

def remaining_budget(self):
return self.budget - self.get_total_expenses()

# Example usage
tracker = PersonalFinanceTracker(1000)

tracker.add_expense(50, 'Groceries')
tracker.add_expense(30, 'Eating Out')
tracker.add_expense(20, 'Transportation')

print("Total expenses: $", tracker.get_total_expenses())
print("Remaining budget: $", tracker.remaining_budget())
print("Groceries expenses: $", tracker.get_expenses_by_category('Groceries'))

A Python script for a Personal Finance Tracker has been created. The script includes a class `PersonalFinanceTracker` that allows users to track their budget and expenses. Users can add expenses, calculate total expenses, get expenses by category, and check the remaining budget.

– The `PersonalFinanceTracker` class has methods for adding expenses, calculating total expenses, getting expenses by category, and calculating the remaining budget.
– Expenses are stored as tuples of amount, category, and timestamp.
– An example usage section demonstrates how to create a tracker, add expenses, and retrieve information about expenses and budget.

This script provides a simple yet effective way to track personal finances, helping users stay within their budget and monitor their spending habits.