Numbers and useful calculations
Chapter 13 of 132 · Level 1 · Beginner Python textbook · 6 real coding demonstrations + 2 worked code answers. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Start here: simple explanation
Calculations are not just school exercises: they help with prices, savings, time, measurements, and program decisions.
Python uses + to add, - to subtract, * to multiply, / to divide, // for floor division, % for remainder, and ** for powers. Parentheses make the calculation order explicit.
The expression 7 / 2 produces 3.5, whereas 7 // 2 produces 3. The remainder 7 % 2 produces 1. These differences matter when grouping items into boxes or checking whether a number is even.
Floats are convenient approximations, not exact representations of every decimal fraction. For introductory display, formatting to two decimal places is useful. For real financial systems, use Decimal or integer cents as appropriate.
Whenever you calculate, give the result a meaningful name. Instead of a long expression hidden inside print, separate subtotal, tax, and total to make each step inspectable.
02 · Learning objectives and technical words
By the end you can…
- Use arithmetic operators correctly
- Understand division, integer division, and remainder
- Avoid confusing rounding with exact money arithmetic
Words you should know
- expression
- code that evaluates to a value
- operator
- a symbol that performs an operation
- remainder
- what is left after division into whole groups
- float
- a numeric type with limited decimal precision
03 · Point-by-point: understand the idea
Locate the input, identify the Python instruction that changes it, and compare the printed result. Make a prediction before each run so you understand what the program does—not only how to copy it.
The mathematical result 0.1 + 0.2 is not stored as exactly 0.3 in a binary float. Avoid using == between arbitrary computed floats when exact equality matters.
04 · Coding examples with real outputs
Example 1: Arithmetic toolbox
Python · runnableprint(8 + 3)
print(8 - 3)
print(8 * 3)
print(8 / 2)
11 5 24 4.0
Why it works: Read each operator aloud while checking the result.
Example 2: Division, groups, and leftovers
Python · runnableprint(7 / 2)
print(7 // 2)
print(7 % 2)
3.5 3 1
Why it works: / gives a float, // gives the number of whole groups rounded down, and % gives leftovers.
Example 3: Calculate an order total
Python · runnableprice = 19.99
quantity = 2
subtotal = price * quantity
print(f"Subtotal: ${subtotal:.2f}")
Subtotal: $39.98
Why it works: :.2f displays a number using two digits after the decimal point.
Example 4: Calculate sales tax
Python · runnablesubtotal = 40
tax_rate = 0.13
tax = subtotal * tax_rate
total = subtotal + tax
print(f"Tax: ${tax:.2f}")
print(f"Total: ${total:.2f}")
Tax: $5.20 Total: $45.20
Why it works: Separate the tax and total calculations; this makes errors easier to spot.
Example 5: Work out minutes and seconds
Python · runnabletotal_seconds = 145
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes, "min", seconds, "sec")
2 min 25 sec
Why it works: Use floor division for whole minutes and remainder for extra seconds.
Example 6: Order of operations
Python · runnableprint(2 + 3 * 4)
print((2 + 3) * 4)
14 20
Why it works: Multiplication runs before addition unless parentheses change the order.
05 · Exercises, debugging and worked answers
Try without looking
- Find the price for 4 pens costing $1.25 each.
- Calculate how many full boxes of 6 fit into 29 apples.
- Find the leftover apples after filling those boxes.
Worked answers
Worked solution 1: Worked answer: pens
Python · runnableunit_price = 1.25
count = 4
print(f"${unit_price * count:.2f}")
$5.00
Why it works: The multiplication gives the order price.
Worked solution 2: Worked answer: boxes
Python · runnableapples = 29
print("Full boxes:", apples // 6)
print("Leftover:", apples % 6)
Full boxes: 4 Leftover: 5
Why it works: Use // for complete boxes and % for the remainder.
06 · Build a mini-project and review
Mini-build: A receipt that calculates subtotal, 13% sample tax, and total for three products.
Can you explain the output before running the code? Can you change the inputs without breaking the program? Revisit the examples until you can do both.
