Editor

Python Program to Calculate a Discount

Beginner · Basics

A discount takes a percentage off the marked price of an item. For a 20% discount on Rs 2500, the discount is 2500 x 20 / 100 = Rs 500, so you pay Rs 2000, and this program works out both amounts.

Program

discount_calculator.py
marked_price = 2500 # or: marked_price = float(input("Enter the price: "))
discount_percent = 20 # or: discount_percent = float(input("Enter the discount %: "))
discount = marked_price * discount_percent / 100
final_price = marked_price - discount
print(f"Marked price: Rs {marked_price:.2f}")
print(f"Discount ({discount_percent}%): Rs {discount:.2f}")
print(f"Price to pay: Rs {final_price:.2f}")
Output
Marked price: Rs 2500.00
Discount (20%): Rs 500.00
Price to pay: Rs 2000.00

How it works

  1. A percentage means "out of 100", so the discount is marked_price * discount_percent / 100.
  2. Taking the discount away from the marked price gives the price to pay.
  3. :.2f shows the amounts in rupees and paise, with two decimal places.

Other ways to write it

A discount that depends on the bill

A discount that depends on the bill
bill = 4200 # or: bill = float(input("Enter the bill amount: "))
if bill >= 5000:
percent = 20
elif bill >= 3000:
percent = 10
elif bill >= 1000:
percent = 5
else:
percent = 0
discount = bill * percent / 100
print(f"Bill: Rs {bill}, discount {percent}%: Rs {discount:.2f}")
print(f"Amount to pay: Rs {bill - discount:.2f}")
Output
Bill: Rs 4200, discount 10%: Rs 420.00
Amount to pay: Rs 3780.00

Shops often give a bigger discount on a bigger bill. The elif chain checks the largest bill first, so each bill gets the best rate it qualifies for.

Two discounts, one after the other

Two discounts, one after the other
price = 1000
first, second = 20, 10 # 20% off, then a further 10% off
after_first = price * (1 - first / 100)
after_second = after_first * (1 - second / 100)
overall = (price - after_second) / price * 100
print(f"After {first}% off: Rs {after_first:.2f}")
print(f"After a further {second}% off: Rs {after_second:.2f}")
print(f"Overall discount: {overall:.0f}%, not {first + second}%")
Output
After 20% off: Rs 800.00
After a further 10% off: Rs 720.00
Overall discount: 28%, not 30%

The second discount is taken from the already reduced price, so 20% and then 10% comes to 28% in total, not 30%.

Try it yourself

Add 18% GST to the discounted price and print the final amount to pay.

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.

All Python programs · CBSE class 12 practical file · Python exercises