How to calculate a discount in Python
A discount is a percentage of the original price, so a price after a discount takes one multiplication and one subtraction. With price = 150 and discount = 10, the discount is 10% of 150, which is 15, and the price after the discount is 150 - 15 = 135.
Python has no built-in discount function. You write the arithmetic yourself, with the operators +, -, * and /. There are two equivalent ways to write the formula.
The discount formula in Python
Way 1: pay the part you keep
A 15% discount means you still pay 100 - 15 = 85 percent of the price. Multiply the price by that percentage and divide by 100:
price = 70
discount = 15
print(price * (100 - discount) / 100) # 59.5
This is the lesson's solution. It needs only one expression and no helper variable.
Way 2: subtract the discount amount
You can also compute how much money the discount takes off, and subtract it:
price = 70
discount = 15
discount_amount = price * discount / 100
print(price - discount_amount) # 59.5
Both give the same result. The second version is easier to read when you also want to show the customer how much they saved, because discount_amount is already a variable.
Why the parentheses matter
Python follows the usual order of operations, so multiplication and division happen before subtraction. Without parentheses, 100 - discount is not calculated first:
price = 70
discount = 15
print(price * 100 - discount / 100) # 6999.85 (wrong)
print(price * (100 - discount) / 100) # 59.5 (right)
The wrong version multiplies price by 100, then subtracts discount / 100. Parentheses force 100 - discount to be calculated first.
The lesson's solution, step by step
The lesson gives you two variables, price and discount, and asks you to print the price after the discount.
- Turn the discount percentage into the share you still pay. The expression
100 - discountgives 85 for a discount of 15. - Multiply the price by that share.
price * (100 - discount)is 70 * 85 = 5950. - Divide by 100 to turn the percentage back into a plain amount: 5950 / 100 = 59.5.
- Print the result with
print.
price = 70
discount = 15
print(price * (100 - discount) / 100)
It prints 59.5.
Integers, floats and rounding
The / operator always returns a float in Python, even when the division is exact. For the example in the statement, 150 * (100 - 10) / 100 prints 135.0, not 135. The value is the same, only the display differs, and 135.0 == 135 is True.
If you want to avoid the decimal point when the price is a whole number, floor division // would drop it, but it also discards cents when the result is not whole: 100 * (100 - 20) // 100 gives 80, while a price of 74.5 would lose its decimal part. For prices, keep / and format the result.
Prices usually show two decimals. Use round(value, 2) to round the number, or an f-string to format it for display:
final_price = 19.99 * (100 - 15) / 100
print(final_price) # 16.9915
print(round(final_price, 2)) # 16.99
print(f"{final_price:.2f}") # 16.99
The f-string version turns the number into text, which is fine for printing but not for further calculations. Keep the unrounded number for any calculation and round only when you display it.
A discount function
When the same calculation is needed more than once, put it in a function. This version returns the new price so that the caller can print it, round it or use it in another calculation:
def apply_discount(price, discount):
return price * (100 - discount) / 100
print(apply_discount(200, 50)) # 100.0
print(apply_discount(50, 0)) # 50.0
print(apply_discount(100, 100)) # 0.0
print(f"{apply_discount(9.99, 15):.2f}") # 8.49
A fuller version would check its inputs first: a price above 0 and a discount between 0 and 100. A discount over 100 would give a negative price, and a negative discount would raise the price.
Common mistakes
- Forgetting the parentheses.
price * 100 - discount / 100is not the discounted price. Wrap100 - discountin parentheses. - Printing the discount instead of the final price.
price * discount / 100is the amount taken off, not what the customer pays. - Using the percentage directly. A discount of 15 means 15%, so it must be divided by 100 somewhere.
price * (1 - discount)would be hugely negative. - Expecting exact decimals. Floats are binary, so some results show long tails, such as
0.1 + 0.2printing0.30000000000000004. Round for display. For exact money arithmetic, use thedecimalmodule or store prices in cents.
Key takeaways
- Price after a discount is
price * (100 - discount) / 100, orprice - price * discount / 100. - Parentheses control the order of operations, so use them around
100 - discount. /returns a float, so135can print as135.0.- Round with
round(value, 2)or format withf"{value:.2f}"when you show prices.
