Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 02 / Topic 5 of 10

Operators

An operator is a symbol that does something with values, like + (add) or >= (compare).

Real-world example: Decide if a user gets free delivery.

cart_total = 520
is_member = False
print(cart_total >= 499 or is_member)  # True

print("a" in "chai")        # True   (membership)
print(5 not in [1, 2, 3])   # True

stock = 10
stock -= 3                  # same as stock = stock - 3
print(stock)                # 7
Group Operators Example → result
Arithmetic + - * / // % ** 7 // 2 → 3
Comparison == != > < >= <= 5 != 3 → True
Logical and or not not True or True → True
Assignment = += -= *= /= //= %= **= x += 1
Membership in, not in "a" in "chai" → True
Identity is, is not x is None
Bitwise & | ^ ~ << >> 5 & 3 → 1

Precedence means which operator runs first, like BODMAS in school maths. High → low: () → ** → unary - → * / // % → + - → << >> → & → ^ → | → comparisons → not → and → or.

print(2 + 3 * 4)     # 14
print((2 + 3) * 4)   # 20
print(2 ** 3 ** 2)   # 512  (** goes right to left: 2 ** 9)
print(-3 ** 2)       # -9   (** before the minus sign)

Bitwise (brief): works on the binary bits of integers.

print(5 & 3, 5 | 3, 5 ^ 3)   # 1 7 6   (0101 & 0011 ...)
print(~5, 5 << 1, 5 >> 1)    # -6 10 2

🔍 Going deeper: Comparisons can be chained: 18 <= age < 60 means 18 <= age and age < 60. and / or are short-circuit: in a or b, b is never checked if a is truthy. ~x is always -x - 1.

Done here? The practice set for Part 2 — 8 exercises with solutions — is on the part page.

Search the guide