0
0
PythonProgramBeginner · 2 min read

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 price
2
Compare selling price with cost price
3
If selling price > cost price, calculate profit as selling price minus cost price
4
If selling price < cost price, calculate loss as cost price minus selling price
5
If selling price equals cost price, declare no profit no loss
6
Print the result accordingly
💻

Code

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'

StepConditionCalculationOutput
1Inputcost_price=100, selling_price=120
2selling_price > cost_price120 > 100
3Calculate profitprofit = 120 - 100 = 20
4PrintProfit 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.

ApproachTimeSpaceBest For
Basic if-elseO(1)O(1)Simple scripts and beginners
Function-basedO(1)O(1)Reusable code and modular design
Ternary operatorO(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.