Operators
Arithmetic operators
| Operator | Meaning | 7 ? 2 |
|---|---|---|
+ - * | add, subtract, multiply | 9, 5, 14 |
/ | division, always a float | 3.5 |
// | floor division (rounds down) | 3 |
% | remainder (modulo) | 1 |
** | power | 49 |
⚠️ // rounds down, so -7 // 2 is -4, not -3.
Comparison operators → True / False
== != > < >= <=. You can chain them: 0 < x < 10.
Logical operators
and, or, not. They short-circuit: in a and b, b is never checked if a is false.
Assignment shortcuts
x += 1 means x = x + 1; the same works for -= *= /= //= %= **=.
Membership and identity
in/not in:"a" in "cat"→Trueischecks if two names point to the same object. Use it forNone:if x is None:
Precedence (highest first)
** → * / // % → + - → comparisons → not → and → or. Use brackets to make intent clear.
▶️ Runs in the app.
Example
a, b = 7, 2
print(a / b) # 3.5
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 49
print(-7 // 2) # -4 (rounds down!)
x = 5
print(0 < x < 10) # True (chained comparison)
print(a > b and b > 0) # True
print("py" in "python") # True
value = None
print(value is None) # True
total = 10
total += 5
print(total) # 15