Python Program to Calculate Profit or Loss
Beginner · Basics
When something is sold for more than it cost there is a profit, and when it is sold for less there is a loss. The cost price (CP) is what you paid and the selling price (SP) is what you sold it for, and this program works out the profit or loss and its percentage.
Program
Profit: Rs 300 Profit percentage: 25.00%
How it works
- If the selling price is higher than the cost price, the difference
selling_price - cost_priceis the profit. - If the cost price is higher, the difference the other way round is the loss.
- Profit and loss percentages are always worked out on the cost price:
profit / cost_price * 100. - When the two prices are equal, the
elsebranch reports no profit and no loss.
Another way to write it
Using a function for many sales
CP 1200, SP 1500: profit of Rs 300 (25.0%) CP 800, SP 650: loss of Rs 150 (18.8%) CP 500, SP 500: no profit, no loss
A single difference tells the whole story: positive means profit, negative means loss, and -diff turns a loss into a positive amount for printing.
Try it yourself
Change the program to find the selling price needed for a 20% profit on the same cost price.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
For a practical file: draw its flowchart or write its algorithm in steps.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises