Operators
Operators are the buttons between the numbers: + adds, - subtracts, * multiplies. But the interesting buttons are the ones a pocket calculator does not have. / always gives a decimal (7 / 2 is 3.5), while // is "divide and throw away the remainder" (7 // 2 is 3), and % is its partner that keeps ONLY the remainder (7 % 2 is 1) โ the single most useful button you have never pressed: it tells you if a number is even, wraps a clock past 12, and later powers every hash map you will ever use. Comparison buttons (==, <, >) do not compute a number at all โ they answer a yes/no question. And the word-buttons and/or/not glue yes/no answers together.
The calculator buttons that matter
step 1 / 5| result | what it means | |
|---|---|---|
| 7 / 2 | 3.5 | true division |
| 7 // 2 | ? | floor: throw away leftover |
| 7 % 2 | ? | keep ONLY the leftover |
| 2 ** 10 | ? | power |
| 14 % 12 | ? | the clock wraps: 14:00 -> 2 |
Division with / always gives the decimal answer, even when it divides evenly.7 / 2
Precedence โ who computes first (top binds tightest)
| level | operators | example |
|---|---|---|
| 1 | () parentheses | (2 + 3) * 4 โ 20 |
| 2 | ** (Python) / ** (JS) | 2 ** 3 ** 2 = 2 ** 9 = 512 โ right-to-left! |
| 3 | unary -x, not/! | -3 ** 2 is -9 (power first) |
| 4 | * / // % | 7 + 3 * 2 โ 13 |
| 5 | + - | |
| 6 | comparisons == != < <= > >= | 1 + 2 == 3 โ True |
| 7 | not / ! | |
| 8 | and / && | short-circuits: right side skipped if left is False |
| 9 | or / || | short-circuits: right side skipped if left is True |
Python specifics
- / ALWAYS returns float (6 / 2 is 3.0); // floors; % takes the sign of the RIGHT operand: -7 % 3 == 2 โ exactly right for circular indexing.
- Chained comparisons are real syntax: 0 <= x < 10 means (0 <= x) and (x < 10).
- and/or return the deciding OPERAND, not a bool: name = user or "guest" is the idiomatic default.
- Divmod in one call: q, r = divmod(17, 5) โ (3, 2).
- No ++ in Python. x += 1 is the only increment.
print(7 / 2) # 3.5 true division print(7 // 2) # 3 floor division print(7 % 2) # 1 remainder -> odd! print(2 ** 10) # 1024 power hour = (11 + 3) % 12 # 2 o'clock โ wrapping is_even = (n % 2 == 0)
What is 17 % 5?
wrap_hour(h) wraps any hour onto a 12-hour clock face using the remainder operator: 14 -> 2, 12 -> 0, 5 -> 5.