Skip to main content
Albero Academy

Operators

Arithmetic, comparison, logical, assignment and membership operators — the verbs of Python expressions.

Fundamentals12 min

Operators combine values into expressions. Python's set is small and readable — most of them do exactly what you'd expect.

Arithmetic

maths operators
7 + 2 # 9 addition
7 - 2 # 5 subtraction
7 * 2 # 14 multiplication
7 / 2 # 3.5 division (always a float)
7 // 2 # 3 floor division (drops the remainder)
7 % 2 # 1 modulo (the remainder)
7 ** 2 # 49 exponent (7 squared)

Comparison

comparisons return booleans
5 == 5 # True equal
5 != 3 # True not equal
5 > 3 # True
5 <= 5 # True
# Python allows chaining:
1 < x < 10 # True if x is between 1 and 10

Logical

and / or / not
(age >= 18) and (has_id) # both must be True
(is_weekend) or (is_holiday) # either is enough
not is_active # inverts the boolean

Assignment & membership

shortcuts and containment
x = 10
x += 3 # x = x + 3 → 13 (also -=, *=, /=, //=, %=, **=)
"a" in "cat" # True membership
3 in [1, 2, 3] # True
"z" not in "cat" # True

Key takeaways

  1. 01Arithmetic: + - * / // % ** — note / is float division, // floors, % is remainder.
  2. 02Comparisons (==, !=, <, >, <=, >=) return booleans and can be chained (1 < x < 10).
  3. 03Logical operators and/or/not combine or invert booleans.
  4. 04Augmented assignment (+=, -=…) updates in place; in / not in test membership.

Finished this chapter?

Mark it complete to track your progress through Python.