Operators
Prerequisites: Numbers, Boolean values
Arithmetic operators: + - * / // % **. Comparison operators:
== != > < >= <=. Logical operators are spelled out as words:
and, or, not (not &&/|| like some other languages).
7 / 2 # 3.5 — division always returns a float
7 // 2 # 3 — floor division, drops the remainder
7 % 2 # 1 — remainder (modulo)
2 ** 10 # 1024 — exponent
age = 20
age >= 18 and age < 65 # True
Common mistakes
- Using
/when integer (floor) division was intended —/always returns afloat, even4 / 2gives2.0, not2. - Confusing
=(assignment) with==(comparison) —if age = 18:is a syntax error in Python (unlike some languages, it won't silently do the wrong thing).