Python Program to Find Profit and Loss
You can find profit or loss in Python by comparing
selling_price and cost_price, then calculating profit = selling_price - cost_price if positive, or loss = cost_price - selling_price if negative.Examples
Inputcost_price = 100, selling_price = 120
OutputProfit of 20
Inputcost_price = 150, selling_price = 100
OutputLoss of 50
Inputcost_price = 200, selling_price = 200
OutputNo profit, no loss
How to Think About It
To find profit or loss, first get the cost price and selling price. If selling price is more than cost price, the difference is profit. If selling price is less, the difference is loss. If both are equal, there is no profit or loss.
Algorithm
1
Get input values for cost price and selling price2
Compare selling price with cost price3
If selling price > cost price, calculate profit as selling price minus cost price4
If selling price < cost price, calculate loss as cost price minus selling price5
If selling price equals cost price, declare no profit no loss6
Print the result accordinglyCode
python
cost_price = float(input('Enter cost price: ')) selling_price = float(input('Enter selling price: ')) if selling_price > cost_price: profit = selling_price - cost_price print(f'Profit of {profit}') elif selling_price < cost_price: loss = cost_price - selling_price print(f'Loss of {loss}') else: print('No profit, no loss')
Output
Enter cost price: 100
Enter selling price: 120
Profit of 20.0
Dry Run
Let's trace the example where cost price is 100 and selling price is 120 through the code
1
Input values
cost_price = 100, selling_price = 120
2
Compare prices
selling_price (120) > cost_price (100) is True
3
Calculate profit
profit = 120 - 100 = 20
4
Print result
Output: 'Profit of 20.0'
| Step | Condition | Calculation | Output |
|---|---|---|---|
| 1 | Input | cost_price=100, selling_price=120 | |
| 2 | selling_price > cost_price | 120 > 100 | |
| 3 | Calculate profit | profit = 120 - 100 = 20 | |
| 4 | Profit of 20.0 |
Why This Works
Step 1: Compare selling and cost price
We use if selling_price > cost_price to check if there is profit.
Step 2: Calculate profit or loss
If profit, calculate selling_price - cost_price; if loss, calculate cost_price - selling_price.
Step 3: Handle no profit no loss
If both prices are equal, print No profit, no loss.
Alternative Approaches
Using a function to return result
python
def find_profit_loss(cost, sell): if sell > cost: return f'Profit of {sell - cost}' elif sell < cost: return f'Loss of {cost - sell}' else: return 'No profit, no loss' cost_price = float(input('Enter cost price: ')) selling_price = float(input('Enter selling price: ')) print(find_profit_loss(cost_price, selling_price))
This approach separates logic from input/output, making code reusable.
Using ternary operator for concise output
python
cost_price = float(input('Enter cost price: ')) selling_price = float(input('Enter selling price: ')) result = (f'Profit of {selling_price - cost_price}' if selling_price > cost_price else f'Loss of {cost_price - selling_price}' if selling_price < cost_price else 'No profit, no loss') print(result)
This method uses a single expression for output but may be less readable for beginners.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables for input and calculations, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in constant time and space; using a function improves code reuse but does not affect speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic if-else | O(1) | O(1) | Simple scripts and beginners |
| Function-based | O(1) | O(1) | Reusable code and modular design |
| Ternary operator | O(1) | O(1) | Concise code, less readability |
Always convert input to float to handle decimal prices correctly.
Forgetting to convert input strings to numbers causes errors or wrong calculations.