Python Program to Calculate Profit or Loss

Write a Python Program to Calculate Profit or Loss with a practical example. As we all know, if we sell a product at a higher price than its actual cost to make, we make a profit. If we sell at a discounted price (less than the cost to make it), it’s a loss. The mathematical formula to calculate profit and loss based on the actual product cost and the sales amount is.

  • Loss = Actual Product Cost – Sales Amount
  • Profit = Sales Amount – Actual Product Cost

Python Program to Calculate Profit or Loss using elif Statement

This python program allows the user to enter the Sales amount and Actual cost of a Product. Next, Python calculates the Loss Amount or profit Amount based on those two values using Elif Statement.

# Python Program to Calculate Profit or Loss
 
actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))
 
if(actual_cost > sale_amount):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

Python profit or loss output

 Please Enter the Actual Product Price: 1500
 Please Enter the Sales Amount: 1200
Total Loss Amount = 300.0

Let me try different values

Python Program to Calculate Profit or Loss

The Python elif Statement is

if(actual_cost > sale_amount):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")
  • If condition checks whether the actual cost is greater than the sale amount. If True, then Python prints the loss amount as the actual cost-sales amount
  • Elif statement checks whether the sale amount is greater than the actual cost. If True, it prints the profit amount as the sales amount – the actual cost
  • If the above conditions fail, it means No Profit No Loss.

Python Program to find Profit or Loss using Arithmetic Operator

In this profit or loss Python program, we are using a Minus operator. Refer to the Python arithmetic operators

# Python Program to Calculate Profit or Loss
 
actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))
 
if(actual_cost - sale_amount > 0):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount - actual_cost > 0):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

Here, we tried all the possibilities and the Python profit or loss program output is

 Please Enter the Actual Product Price: 2200
 Please Enter the Sales Amount: 6500
Total Profit = 4300.0
>>> 
 Please Enter the Actual Product Price: 9800
 Please Enter the Sales Amount: 7200
Total Loss Amount = 2600.0
>>> 
 Please Enter the Actual Product Price: 1495
 Please Enter the Sales Amount: 1495
No Profit No Loss!!!

Calculate profit or loss using functions

def profit(cp, sp):
return sp - cp

def loss(cp, sp):
return cp - sp


ac = float(input('Enter the Actual Product Price = '))
sa = float(input('Enter the Final Sales Amount = '))
if sa > ac:
print(profit(ac, sa), "Profit")
elif sa < ac:
print(loss(ac, sa), "Loss")
else:
print('No Profit or Loss')
Enter the Actual Product Price = 2500
Enter the Final Sales Amount   = 3000
500.0 Profit

Enter the Actual Product Price = 100
Enter the Final Sales Amount   = 100
No Profit or Loss

Also Read