Python Basics
Python Arithmetic Operators
1. Key Terminology
| Term | Meaning | Example |
|---|---|---|
| Operator | The operation being applied | +, -, *, / |
| Operand | The values the operator acts on | x and y in x + y |
| Expression | Operator + operands combined | x + y |
| Evaluate | Computing an expression to get a result | x + y → 5 |
x = 2
y = 3
result = x + y # x and y are operands, + is operator, x+y is expression
2. The 7 Arithmetic Operators
① Addition +
2 + 3 # 5 (int)
3.4 + 2 # 5.4 (float)
1.0 + 2 # 3.0 (float — not int!)
② Subtraction -
5 - 3 # 2
-2 - (-3) # 1 — wrap negatives in parentheses for clarity
4 - (-8) # 12 ← cleaner to write as 4 - (-8)
③ Multiplication *
4 * 3 # 12 (int)
4 * 2.0 # 8.0 (float)
④ Division /
10 / 2 # 5.0 ← always returns float, even if result is whole
10 / 2.5 # 4.0
10 / 0 # ❌ ZeroDivisionError
Division always returns a float, regardless of operand types
⑤ Exponentiation **
10 ** 2 # 100
2 ** 0 # 1
2 ** -1 # 0.5
⑥ Integer Division //
10 // 3 # 3 (chops off decimals, no rounding)
11 // 3 # 3 (not 4 — always floors, never rounds up)
3.5 // 4 # 0.0 (float operand → float result)
10 // 0 # ❌ ZeroDivisionError
Gives how many times the right operand fits evenly into the left
⑦ Modulus %
11 % 3 # 2 (remainder: 3×3=9, 11-9=2)
11 % 2 # 1
11 % 1 # 0 (no remainder)
10 % 0 # ❌ ZeroDivisionError
Returns the remainder after division
3. Float Contagion Rule
Whenever any operand is a float, the result is always a float
1.0 + 2 # 3.0 (not 3)
10 / 2 # 5.0 (division always float)
3.5 // 4 # 0.0 (float operand → float result)
4. Order of Operations (Operator Precedence)
From highest to lowest priority:
| Priority | Operator(s) | Symbol |
|---|---|---|
| 1 | Parentheses / Brackets | () |
| 2 | Exponentiation | ** |
| 3 | Multiplication, Division, Modulus | *, /, //, % |
| 4 | Addition, Subtraction | +, - |
- Operators at the same priority level execute left to right
- Use parentheses liberally to make complex expressions easier to read
Worked Example
x, y, z = 11, 2, 4
result = (x + y - z) ** (x % z) - 7 / 2 // 4
# (11+2-4) ** (11%4) - 3.5 // 4
# 9 ** 3 - 0.0
# 729 - 0.0
# = 729.0
5. Using Operators with Wrong Types
| Combination | Result |
|---|---|
int + int |
✅ works |
float + int |
✅ works (returns float) |
str + str |
✅ concatenation (e.g. "a" + "b" → "ab") |
str + int |
❌ TypeError |
str / int |
❌ TypeError |
Special Case — Booleans
Trueis treated as1,Falseas0
4.5 / True # 4.5 (True == 1)
4.5 / False # ❌ ZeroDivisionError (False == 0)
6. Key Takeaways & Recap
- 7 operators:
+,-,*,/,**,//,% - Division
/always returns a float — even10 / 2gives5.0 - Any float operand makes the whole result a float
//floors the result (never rounds up);%gives the remainder- Never divide by zero — any division-based operator will crash
- Precedence:
()→**→* / // %→+ -(left to right within same level) - Mixing
strwith numeric operators causes a TypeError