- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Calculating profit or loss using Python

In this article, we will learn a Python Program to calculate Profit Or Loss.

The following are the different methods to accomplish this task −

Using If Conditional Statements

Using the abs() function.

## What are the selling price, cost price, and Profit and Loss?

The price a consumer pays to purchase a product or a commodity is known as the **selling price**. It is a price that is higher than the cost price and also includes a portion of the profit.

**Cost price** is the cost at which the seller buys the product or the commodity. He follows that by adding a portion of gain or profits.

The amount gained from selling an item for more than its cost price is known as the **profit**.

Profit = Selling Price – Cost Price.

**Loss** is the amount lost by selling an item for less than its cost price.

Loss = Cost Price - Selling Price

## Method 1: Using If Conditional Statements

### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a function to calculate

**Profit**that accepts the cost price(cp) and selling price(sp) as arguments.Use the mathematical formula

**sp - cp**to calculate profit and create a variable to store it.Return the above-calculated profit using the return keyword.

Create another function to calculate

**Loss**that accepts the cost price(cp) and selling price(sp) as arguments.Use the mathematical formula

**cp - sp**to calculate loss and create a variable to store it.Return the above-calculated loss using the return keyword.

Create two separate variables to store the input cost price and selling price.

Use the

**if conditional statement**and ‘==’ operator to check whether the input selling price is equal to the cost price.Print Neither profit nor Loss if the condition is true.

Use the

**elif conditional statement**to check whether the selling price is greater than the cost price.Print

**profit**if the condition is true by calling**calculateProfit**function by passing cost price and selling price as arguments to get the profit value.Else print Loss by calling

**calculateLoss**function by passing cost price and selling price as arguments to it to get the loss value.

### Example

The following program calculates the profit or loss using the if conditional statements −

# creating a function to calculate Profit that # accepts the cost price(cp) and selling price(sp) as arguments def calculateProfit(cp, sp): # formula for calculating profit resultProfit = (sp - cp) # returning the resulting profit return resultProfit # creating a function to calculate Loss that # accepts the cost price(cp) and selling price(sp) as arguments def calculateLoss(cp, sp): # formula for calculating loss resultLoss = (cp - sp) # returning the resultant loss. return resultLoss # input cost price cost_price = 500 # input selling price selling_price = 1000 # checking whether the selling price is equal to the cost price if selling_price == cost_price: # printing Neither profit nor loss if the condition is true print("Neither profit nor Loss") # checking whether the selling price is greater than the cost price elif selling_price > cost_price: # Calling calculateProfit function by passing cost price and selling price # as arguments and printing profit if the condition is true print("The Profit is", calculateProfit(cost_price, selling_price)) else: # Else calling calculateLoss function by passing cost price and # selling price as arguments and printing Loss print("The Loss is", calculateLoss(cost_price, selling_price))

### Output

On execution, the above program will generate the following output −

The Profit is 500

## Method 2: Using the abs() function

### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a function to calculate the

**difference**between the**selling price**and**cost price**that accepts the cost price(cp) and selling price(sp) as arguments.Use the

**abs()**function to calculate the difference between the selling price and the cost price.Return the difference between the selling price and the cost price.

Create two separate variables to store the input cost price and selling price.

Print the profit/loss like the previous method.

### Example

The following program calculates the profit or loss using the abs() function −

# creating a function to calculate the difference between sp and cp that # accepts the cost price(cp) and selling price(sp) as arguments def calculateDifference(cp, sp): # calculating the difference between the selling price and the cost price difference = abs(sp - cp) # returning the absolute difference of selling and cost price return difference # input cost price cost_price = 500 # input selling price selling_price = 1000 # checking whether the selling price is equal to the cost price if selling_price == cost_price: # printing Neither profit nor Loss if the condition is true print("Neither profit nor Loss") # checking whether the selling price is greater than the cost price elif selling_price > cost_price: # printing profit if the condition is true, by calling calculateDifference # function by passing cost price and selling price as arguments print("The Profit is", calculateDifference(cost_price,selling_price)) # Else this is the case of loss else: # Else printing Loss if the condition is true by calling calculateDifference # function by passing cost price and selling price as arguments to it print("The Loss is", calculateDifference(cost_price,selling_price))

### Output

On execution, the above program will generate the following output −

The Loss is 200

## Conclusion

In this article, we learned what selling and cost prices are as well as how to create a Python program to determine profit or loss when selling and cost prices are given. As an alternative to using two different functions, we learned how to calculate the absolute difference between selling and cost prices using the abs() function.