Section · 01
The operators you'll use
Python has seven arithmetic operators. You’ve probably seen five of them since middle school:
3 + 2 # 5 — addition
3 - 2 # 1 — subtraction
3 * 2 # 6 — multiplication
3 / 2 # 1.5 — division (always returns a float)
3 ** 2 # 9 — exponent (3 squared)And two that confuse newcomers:
7 // 2 # 3 — floor division (drops the decimal)
7 % 2 # 1 — modulo (the remainder)//is “divide and throw away the decimal.” %is “what’s left over.” They’re cousins: 7 // 2 == 3 and 7 % 2 == 1 because 7 = 3 × 2 + 1. You’ll use both surprisingly often.
Section · 02
Order of operations (PEMDAS, but Python)
Python uses the same rules you learned in algebra: parentheses first, then exponents, then multiply/divide, then add/subtract. Left to right within a tier.
2 + 3 * 4 # 14 — multiplication first
(2 + 3) * 4 # 20 — parentheses change the order
2 ** 3 * 2 # 16 — exponent first: 8 * 2
10 - 4 + 1 # 7 — left to right: (10 - 4) + 1Use parentheses even when you don’t need them, if it makes the intent clearer:
# Both produce the same result; the second one reads in one pass:
subtotal + subtotal * tax_rate
subtotal + (subtotal * tax_rate)Three minutes of extra parentheses can save your reviewer twenty minutes of double-checking.
Section · 03
Where modulo earns its keep
% looks niche at first but solves a surprising number of real problems. Four examples worth remembering:
1. “Is this number divisible by N?”
# Run cleanup every 10th request
if request_count % 10 == 0:
run_cleanup()2. “Is it even or odd?”
if year % 2 == 0:
print("even year")
else:
print("odd year")3. Time math (seconds → minutes and seconds)
total_seconds = 197
minutes = total_seconds // 60 # 3
seconds = total_seconds % 60 # 17
print(f"{minutes}:{seconds:02d}") # "3:17"4. Wrapping around a list
# Cycle through 5 colors for a chart
colors = ["red", "blue", "green", "purple", "orange"]
for i in range(12):
color = colors[i % 5] # 0,1,2,3,4,0,1,2,3,4,0,1
draw_bar(color)That last pattern shows up everywhere: rotating UI states, dealing cards, scheduling. Modulo is the answer to “wrap around when you hit the end.”
Section · 04
The floating-point trap
Try this in any Python prompt:
>>> 0.1 + 0.2
0.30000000000000004That’s not a Python bug. It’s how floats are stored in hardware on every modern computer. Some decimal numbers can’t be represented exactly in binary — same way 1/3can’t be written exactly in decimal (0.333...). For 90% of your code it won’t matter. For the other 10% — money, scientific measurements, anything where exact equality matters — you need a different tool.
For displaying floats: round()
>>> round(0.1 + 0.2, 2)
0.3For money: use Decimal or integer cents
# Bad: storing dollars as floats
total = 0.0
for item in cart:
total += item.price # rounding errors accumulate
# Better: store everything in cents (integers)
total_cents = 0
for item in cart:
total_cents += item.price_cents
dollars = total_cents / 100 # only convert at the end
# Or use Decimal for full precision
from decimal import Decimal
total = Decimal("0.00")
for item in cart:
total += Decimal(item.price_str)Don’t use == on floats. Use abs(a - b) < 0.0001if you need to ask “close enough?” instead.