Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Freelance Finance Tracker | Automating Freelance Workflow
Python for Freelancers

bookChallenge: Freelance Finance Tracker

Managing your freelance finances efficiently is crucial for long-term success. In this challenge, you will build a Python script that acts as a simple finance tracker. This script will let you add income and expense entries (using hardcoded data for demonstration), categorize each transaction, and then generate a clear summary report. The report will show your total income, total expenses, net profit, and list all transactions grouped by category.

To start, you need to represent your transactions in a way that makes them easy to manage and analyze. You can use a list of dictionaries, where each dictionary holds the details of a single transaction. Each transaction will have an amount, a category, a type ("income" or "expense"), and a description.

123456789
# Hardcoded list of transactions transactions = [ {"amount": 1200, "category": "Web Development", "type": "income", "description": "Website for Client A"}, {"amount": 300, "category": "Graphic Design", "type": "income", "description": "Logo for Client B"}, {"amount": 100, "category": "Software", "type": "expense", "description": "Annual domain registration"}, {"amount": 50, "category": "Office Supplies", "type": "expense", "description": "Printer ink"}, {"amount": 200, "category": "Web Development", "type": "income", "description": "Landing page for Client C"}, {"amount": 75, "category": "Marketing", "type": "expense", "description": "Social media ads"}, ]
copy

With your data structured, you can now calculate the totals for income and expenses. Loop through each transaction, adding up the amounts for each type. The net profit is simply your total income minus your total expenses.

12345678
# Calculate totals total_income = sum(t["amount"] for t in transactions if t["type"] == "income") total_expenses = sum(t["amount"] for t in transactions if t["type"] == "expense") net_profit = total_income - total_expenses print("Total Income: $", total_income) print("Total Expenses: $", total_expenses) print("Net Profit: $", net_profit)
copy

To make your report more useful, you should also list all transactions grouped by category. You can use a dictionary to organize transactions by their category, then print each group with its transactions.

12345678910111213
# Group transactions by category from collections import defaultdict category_groups = defaultdict(list) for t in transactions: category_groups[t["category"]].append(t) print("\nTransactions by Category:") for category, items in category_groups.items(): print(f"\n{category}:") for item in items: sign = "+" if item["type"] == "income" else "-" print(f" {sign}${item['amount']} ({item['description']})")
copy

This approach gives you a simple but flexible way to track your freelance finances, categorize your earnings and expenses, and see your bottom line at a glance. You can expand this script with more features as your needs grow, but this foundation will help you stay organized and informed.

Tâche

Swipe to start coding

Create a Python script that tracks freelance income and expenses using hardcoded data. Your script should:

  • Store at least six transactions, with both income and expense entries, each having a category and description.
  • Calculate and print total income, total expenses, and net profit.
  • Print all transactions grouped by category, showing amount, type (income/expense), and description.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain how to add new transactions to the list?

How can I customize the categories for my own needs?

What are some ideas for expanding this script with more features?

close

bookChallenge: Freelance Finance Tracker

Glissez pour afficher le menu

Managing your freelance finances efficiently is crucial for long-term success. In this challenge, you will build a Python script that acts as a simple finance tracker. This script will let you add income and expense entries (using hardcoded data for demonstration), categorize each transaction, and then generate a clear summary report. The report will show your total income, total expenses, net profit, and list all transactions grouped by category.

To start, you need to represent your transactions in a way that makes them easy to manage and analyze. You can use a list of dictionaries, where each dictionary holds the details of a single transaction. Each transaction will have an amount, a category, a type ("income" or "expense"), and a description.

123456789
# Hardcoded list of transactions transactions = [ {"amount": 1200, "category": "Web Development", "type": "income", "description": "Website for Client A"}, {"amount": 300, "category": "Graphic Design", "type": "income", "description": "Logo for Client B"}, {"amount": 100, "category": "Software", "type": "expense", "description": "Annual domain registration"}, {"amount": 50, "category": "Office Supplies", "type": "expense", "description": "Printer ink"}, {"amount": 200, "category": "Web Development", "type": "income", "description": "Landing page for Client C"}, {"amount": 75, "category": "Marketing", "type": "expense", "description": "Social media ads"}, ]
copy

With your data structured, you can now calculate the totals for income and expenses. Loop through each transaction, adding up the amounts for each type. The net profit is simply your total income minus your total expenses.

12345678
# Calculate totals total_income = sum(t["amount"] for t in transactions if t["type"] == "income") total_expenses = sum(t["amount"] for t in transactions if t["type"] == "expense") net_profit = total_income - total_expenses print("Total Income: $", total_income) print("Total Expenses: $", total_expenses) print("Net Profit: $", net_profit)
copy

To make your report more useful, you should also list all transactions grouped by category. You can use a dictionary to organize transactions by their category, then print each group with its transactions.

12345678910111213
# Group transactions by category from collections import defaultdict category_groups = defaultdict(list) for t in transactions: category_groups[t["category"]].append(t) print("\nTransactions by Category:") for category, items in category_groups.items(): print(f"\n{category}:") for item in items: sign = "+" if item["type"] == "income" else "-" print(f" {sign}${item['amount']} ({item['description']})")
copy

This approach gives you a simple but flexible way to track your freelance finances, categorize your earnings and expenses, and see your bottom line at a glance. You can expand this script with more features as your needs grow, but this foundation will help you stay organized and informed.

Tâche

Swipe to start coding

Create a Python script that tracks freelance income and expenses using hardcoded data. Your script should:

  • Store at least six transactions, with both income and expense entries, each having a category and description.
  • Calculate and print total income, total expenses, and net profit.
  • Print all transactions grouped by category, showing amount, type (income/expense), and description.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
single

single

some-alt