AW Dev Rethought

✂️ Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-Exupéry

🐍 Python From Zero — 03/23: Working with Numbers


Description:

You already know how to store values in variables. Now let Python calculate with them. Our example buys two items at ₹120 each, adds ₹30 delivery, subtracts a flat ₹20 discount and splits the bill between two people.


1. Store the numbers

Numbers stay outside quotation marks. These starting values are integers.

price = 120
quantity = 2
delivery = 30
discount = 20
people = 2

The discount is a fixed rupee amount, not a percentage. There is no tax in this example.


2. Calculate the bill

subtotal = price * quantity
total = subtotal + delivery - discount
per_person = total / people
Calculation Operator Result
Price × quantity * 240
Subtotal + delivery − discount +, - 250
Total ÷ people / 125.0

Python evaluates the expression to the right of = before assigning the result. Division of these ordinary numbers with / produces a float, which explains the .0 in 125.0. Keep people positive; zero would cause ZeroDivisionError.


3. Display the results

print("Subtotal:", subtotal)
print("Total:", total)
print("Per person:", per_person)

The comma separates the text label and numeric value passed to print(). Save the complete program as bill_calculator.py and choose Run → Run Module in IDLE.

Subtotal: 240
Total: 250
Per person: 125.0

All amounts here are in rupees. The full commented script is included with this lesson.


4. Group calculations with parentheses

Multiplication and division take precedence over addition and subtraction. Parentheses let you group the part to calculate first.

print(2 + 3 * 4)      # 14: 3 * 4 happens first
print((2 + 3) * 4)    # 20: 2 + 3 happens first

You could express the bill in one line, but the named intermediate results are easier to follow:

per_person = (price * quantity + delivery - discount) / people

The parentheses ensure the whole bill is calculated before it is split.


5. Recognise three more operators

print(11 // 3)    # 3: floor division
print(11 % 3)     # 2: remainder
print(3 ** 2)     # 9: three squared

// rounds the quotient down toward negative infinity, so -11 // 3 is -4, not -3. % means remainder, not percentage. Use ** for powers; ^ is a different operator.

These examples are also in operators_demo.py. Python's arithmetic behaviour is described in the official Numbers tutorial.


Your turn

Change quantity to 3. Keep everything else unchanged. Predict all three results, then save and run the entire file again. A separate solution is included so you can try it first.

This lesson uses deliberately simple amounts to teach arithmetic. Real billing needs explicit currency and rounding rules; ordinary floats cannot represent every decimal fraction exactly. Integer minor units or Decimal are options for a later lesson.

Next: Input and Strings — let someone enter values into your program.


Link copied!

Comments

Add Your Comment

Comment Added!