Courses/Python Mastery/Module 3: Variables & Data Types
Module 3 · Lesson 220 minBeginner

Numbers: int, float, and Math Operators

Lesson goal
Do real math in Python — and understand integer division's famous gotcha.

Numbers: int, float, and Math Operators

Python was born to do math — its original users were scientists. Two number types cover everything you need, and five operators do everything you need to them.

The two number types

code
age = 21              # int — whole numbers
price = 99.5          # float — decimals (the "floating point")

That's it. No sizes to pick, no unsigned variants. Python's ints can even be *millions of digits long* without complaining — try print(2 ** 100) and watch a 31-digit number appear.

The seven arithmetic operators

code
print(10 + 3)    # 13   addition
print(10 - 3)    # 7    subtraction
print(10 * 3)    # 30   multiplication
print(10 / 3)    # 3.3333333333333335  division (ALWAYS float)
print(10 // 3)   # 3    floor division (drops the decimal)
print(10 % 3)    # 1    modulo (the REMAINDER)
print(10 ** 3)   # 1000 exponent (10 to the power 3)

Three of these deserve special attention:

`/` always gives a float — even for clean divisions:

code
print(20 / 4)    # 5.0  ← a float, not 5!
print(20 // 4)   # 5    ← floor division gives the int

`%` (modulo) gives the remainder — and it's secretly one of the most useful operators in programming:

code
print(17 % 5)     # 2  (17 = 5×3 + 2)
print(10 % 2)     # 0  → 10 is EVEN (no remainder)
print(7 % 2)      # 1  → 7 is ODD
print(125 % 10)   # 5  → the last digit of 125

Even/odd checks, "every 3rd item," last digits, wrapping around clocks — modulo does it all.

` is exponent** — 2 ** 10` is 2 to the power 10 (1024), not "2 XOR 10" and definitely not "2 and 10".

Order of operations (PEMDAS applies)

code
print(2 + 3 * 4)        # 14  — multiplication first
print((2 + 3) * 4)      # 20  — parentheses win
print(-3 ** 2)          # -9  — power binds tighter than the minus!
print((-3) ** 2)        # 9   — parentheses to the rescue

When in doubt, add parentheses. They're free.

Mixing ints and floats

code
print(5 + 2.0)     # 7.0 — int + float = float
print(type(5))     # <class 'int'>
print(type(5.0))   # <class 'float'>

Python promotes to the more precise type automatically. The type() function is your X-ray glasses — use it whenever you're unsure what you're holding.

Useful built-ins for numbers

code
print(round(3.14159, 2))   # 3.14
print(abs(-7))             # 7
print(max(3, 9, 4))        # 9
print(min(3, 9, 4))        # 3

Common Errors & Fixes

  • `TypeError: unsupported operand type(s) for +: 'int' and 'str'` — you tried math on text (usually from input()). Convert with int() or float() first.
  • `ZeroDivisionError: division by zero`10 / 0 breaks math itself. Guard it: check the divisor before dividing.
  • `0.1 + 0.2 == 0.3` printed False?! — floats have tiny precision limits (0.30000000000000004). For money, count in paise/cents as ints, or use round().

  • ✅ Checkpoint

  • What does 7 // 2 print? 7 % 2? *(3 and 1)*
  • How do you check if a number is even? *(number % 2 == 0)*
  • What does 2 ** 3 mean? *(2 to the power 3 = 8)*
  • What type does 10 / 5 produce? *(A float: 2.0)*
  • Next: strings — the type that holds every username, message, and URL on the planet.