Calculate Commission from Revenue and Expenses with Python

2023

In this project, we will explore how to calculate commission based on revenue and expenses using Python. We have two dictionaries representing revenue and expenses for different individuals. We'll explain each step of the code and provide the final working code at the end.


Revenue and Expenses Data

We start by defining the revenue and expenses data as dictionaries:

revenue = {
    'Johnver': [190, 325, 682, 829],
    'Vanston': [140, 19, 14, 140],
    'Danbree': [1926, 293, 852, 609],
    'Vansey': [14, 1491, 56, 120],
    'Mundyke': [143, 162, 659, 87]
}

expenses = {
    'Johnver': [120, 300, 50, 67],
    'Vanston': [65, 10, 299, 254],
    'Danbree': [890, 23, 1290, 89],
    'Vansey': [54, 802, 12, 129],
    'Mundyke': [430, 235, 145, 76]
}

The revenue dictionary contains the revenue data for each individual, and the expenses dictionary contains the corresponding expense data.

Calculating Total Revenue

We calculate the total revenue for each individual and store the results in a new dictionary called total_revenue:

total_revenue = {}
for key in revenue:
    total_revenue[key] = [r - e for r, e in zip(revenue[key], expenses[key])]
    total_revenue[key] = [x if x > 0 else 0 for x in total_revenue[key]]

We iterate over the keys in the revenue dictionary and subtract the corresponding expenses from the revenue values using a list comprehension. We also ensure that the resulting total revenue values are non-negative.

Calculating Commission

We calculate the commission for each individual based on the total revenue and store the results in a new dictionary called commission:

commission = {}
for key in total_revenue:
    commission[key] = round(sum(total_revenue[key]) * 0.062)

We iterate over the keys in the total_revenue dictionary and calculate the commission by multiplying the sum of total revenue by 0.062 (6.2%).

Displaying the Commission

Finally, we display the commission for each individual:

for key, value in commission.items():
    print(f"{key}: {value}")

We iterate over the items in the commission dictionary and print the individual's name and their corresponding commission value.


Final Code

Here's the complete Python code for calculating commission based on revenue and expenses:

revenue = {
    'Johnver': [190, 325, 682, 829],
    'Vanston': [140, 19, 14, 140],
    'Danbree': [1926, 293, 852, 609],
    'Vansey': [14, 1491, 56, 120],
    'Mundyke': [143, 162, 659, 87]
}

expenses = {
    'Johnver': [120, 300, 50, 67],
    'Vanston': [65, 10, 299, 254],
    'Danbree': [890, 23, 1290, 89],
    'Vansey': [54, 802, 12, 129],
    'Mundyke': [430, 235, 145, 76]
}

total_revenue = {}
for key in revenue:
    total_revenue[key] = [r - e for r, e in zip(revenue[key], expenses[key])]
    total_revenue[key] = [x if x > 0 else 0 for x in total_revenue[key]]

commission = {}
for key in total_revenue:
    commission[key] = round(sum(total_revenue[key]) * 0.062)

for key, value in commission.items():
    print(f"{key}: {value}")

That's it! You now have a Python code snippet that allows you to calculate commission based on revenue and expenses. Feel free to modify the code or use it as a starting point for your own projects. Enjoy calculating commissions!

Back