Courses/Python Mastery/Module 4: Operators
Module 4 · Lesson 115 minBeginner

Arithmetic Operators

Lesson goal
+ - * / // % ** — each one, with the gotchas that surprise beginners.

Arithmetic Operators

Seven symbols do all the math in Python. You've already used several — now let's make each one precise, including the three that surprise beginners.

The full set

code
print(10 + 3)    # 13   addition
print(10 - 3)    # 7    subtraction
print(10 * 3)    # 30   multiplication
print(10 / 3)    # 3.3333333333333335   true division
print(10 // 3)   # 3    floor division
print(10 % 3)    # 1    modulo (remainder)
print(10 ** 3)   # 1000 exponent

The division family — know all three

Python is unusual in having three division-related operators, and mixing them up is the classic beginner bug:

OperatorNameWhat it givesExample
/true divisionAlways a float, decimals included10 / 33.3333...
//floor divisionRounds down to a whole number10 // 33
%moduloJust the remainder10 % 31

Watch // with negatives — "floor" means round *down*, not "cut off":

code
print(7 // 2)     # 3
print(-7 // 2)    # -4  ← floors DOWN, not toward zero
print(int(-7 / 2))  # -3  ← int() truncates toward zero

Modulo — the secret weapon

% looks obscure until you see its greatest hits:

code
# Even or odd?
number = 17
print(number % 2 == 0)     # False → odd

# Last digit of a number
print(4826 % 10)           # 6

# Every Nth item
for minute in range(1, 61):
    if minute % 15 == 0:
        print(f"Minute {minute}: take a break!")

# Wrapping around (a clock!)
print((10 + 5) % 12)       # 3  → 10 o'clock + 5 hours = 3 o'clock

Even/odd checks, cycles, digits, schedules — % is everywhere once you can see it.

Exponent: **

code
print(2 ** 10)     # 1024
print(9 ** 0.5)    # 3.0  ← square root via half-power!
print(2 ** -2)     # 0.25 ← negative powers too

Order of operations

Python follows standard math precedence — and parentheses always win:

code
print(2 + 3 * 4)       # 14
print((2 + 3) * 4)     # 20
print(2 ** 3 ** 2)     # 512 — powers chain RIGHT to left: 2**(3**2)

That last one is exotic; the practical rule is simpler: when unclear, parenthesize.

Working with floats — the honest truth

code
print(0.1 + 0.2)        # 0.30000000000000004  ← what?!
print(0.1 + 0.2 == 0.3) # False

Computers store decimals in binary, and some decimals can't be represented exactly — a tiny error creeps in. This is not a Python bug; it's how all languages work. Practical responses:

code
print(round(0.1 + 0.2, 2))    # 0.3 — round for display
# For money: work in paise/cents as ints (1850 paise, not 18.50 rupees)

Common Errors & Fixes

  • `ZeroDivisionError` — division by zero. Check before dividing: if divisor != 0:.
  • `TypeError` on `"5" * "3"` — strings can't multiply strings ("5" * 3 is fine — it repeats!). Convert to numbers first.
  • Confused about `5.0` vs `5`/ always floats; use // when you want whole numbers.

  • ✅ Checkpoint

  • 9 % 4 = ? *(1)*
  • How do you get the last digit of 7392? *(7392 % 10 → 2)*
  • 2 ** 3 ** 2 = ? *(512 — powers chain right-to-left: 2**(9))*
  • Is 0.1 + 0.2 == 0.3? *(False — float precision; use round())*
  • Next: comparison operators — how numbers turn into True and False.