🕯️ Magic Note
Python operators are actually special methods in disguise. When you write a + b, Python calls a.__add__(b) behind the scenes. This means you can define how operators work for your own classes. This is called operator overloading, and it is one of Python’s powerful features.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| – | Subtraction | 10 – 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (float) | 10 / 3 | 3.333… |
| // | Floor division | 10 // 3 | 3 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation (power) | 10 ** 3 | 1000 |
Python
a = 10
b = 3
print(a + b) # 13
print(a – b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
| Operator | Name | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 5 > 3 | True |
| < | Less than | 5 < 3 | False |
| >= | Greater than or equal to | 5 >= 5 | True |
| <= | Less than or equal to | 5 <= 3 | False |
Python
x = 10
y = 20
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= 10) # True
print(y <= 10) # False
| Operator | What It Does | Example (a=True | b=False) | Result |
|---|---|---|---|---|
| and | True only if both are True | a and b | False | |
| or | True if at least one is True | a or b | True | |
| not | Reverses the value | not a | False |
Python
is_weekend = True
has_time = False
print(is_weekend and has_time) # False
print(is_weekend or has_time) # True
print(not is_weekend) # False
🕯️ Magic Note
and and or short-circuit. and stops at the first False. or stops at the first True. This can be used for conditional execution.
| Operator | Example | Equivalent to |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x – 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| //= | x //= 3 | x = x // 3 |
| %= | x %= 3 | x = x % 3 |
| **= | x **= 3 | x = x ** 3 |
Python
counter = 0
counter += 1 # counter becomes 1
counter += 1 # counter becomes 2
score = 10
score *= 2 # score becomes 20
value = 100
value //= 3 # value becomes 33
| Operator | Example | Result |
|---|---|---|
| in | 5 in [1, 2, 3, 4, 5] | True |
| not in | 5 not in [1, 2, 3, 4] | True |
Python
fruits = [“apple”, “banana”, “cherry”]
print(“banana” in fruits) # True
print(“grape” in fruits) # False
print(“grape” not in fruits) # True
text = “Hello, World!”
print(“World” in text) # True
person = {“name”: “Ali”, “age”: 25}
print(“name” in person) # True (checks keys)
print(“Ali” in person) # False (checks keys, not values)
| Operator | Example | Result |
|---|---|---|
| is | a is b | True if same object |
| is not | a is not b | True if different objects |
Python
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True (same values)
print(a is b) # False (different objects)
print(a is c) # True (same object)
x = 5
y = 5
print(x is y) # True (small integers are cached, but don’t rely on this)
| Operator | Name | Example (x=5=0b0101 , y=3=0b0011) | Result |
|---|---|---|---|
| & | Bitwise AND | x & y | 1 (0b0001) |
| | | Bitwise OR | x | y | 7 (0b0111) |
| ^ | Bitwise XOR | x ^ y | 6 (0b0110) |
| ~ | Bitwise NOT | ~x | -6 |
| << | Left shift | x << 1 | 10 (0b1010) |
| >> | Right shift | x >> 1 | 2 (0b0010) |
Python
x = 5 # 0b0101
y = 3 # 0b0011
print(x & y) # 1 (0b0001)
print(x | y) # 7 (0b0111)
print(x ^ y) # 6 (0b0110)
print(~x) # -6
print(x << 1) # 10 (0b1010)
print(x >> 1) # 2 (0b0010)
| Precedence (High to Low) | Operators |
|---|---|
| 1 (highest) | () (parentheses) |
| 2 | ** (exponentiation) |
| 3 | ~ (bitwise NOT), + (unary), – (unary) |
| 4 | *, /, //, % |
| 5 | +, – (binary) |
| 6 | <<, >> |
| 7 | & |
| 8 | ^ |
| 9 | | |
| 10 | in, not in, is, is not, <, <=, >, >=, != , == |
| 11 | not (logical) |
| 12 | and |
| 13 (lowest) | or |
Python
# Without parentheses (follows precedence)
result = 10 + 3 * 2 # 10 + (3 * 2) = 16
# With parentheses (overrides precedence)
result = (10 + 3) * 2 # 13 * 2 = 26
# Complex example
a = 5
b = 10
c = 15
print(a < b and b < c) # True (and has lower precedence than comparison)
🕯️ Magic Note
When in doubt, use parentheses. They make your code more readable and remove any ambiguity about operator precedence. There is no performance penalty for extra parentheses.
Python
# Without walrus (compute twice)
data = [1, 2, 3, 4, 5]
if len(data) > 3:
print(f”List has {len(data)} items”) # len() called again
# With walrus (compute once)
if (n := len(data)) > 3:
print(f”List has {n} items”)
# In a while loop
while (line := input()) != “quit”:
print(f”You said: {line}”)
| Category | Operators |
|---|---|
| Arithmetic | +, –, *, /, //, %, ** |
| Comparison | ==, != , >, <, >=, <= |
| Logical | and, or, not |
| Assignment | =, +=, -=, *=, /=, //=, %=, **= |
| Membership | in, not in |
| Identity | is, is not |
| Bitwise | &, |, ^, ~, <<, >> |
| Walrus | := |
- Confusing = with ==
- Using is instead of == for value comparison
- Forgetting operator precedence and getting unexpected results
- Using and and or with non-boolean values (works but may confuse)
- Misunderstanding // with negative numbers
- Using in with dictionaries and expecting value check (checks keys)
- What is the difference between = and ==?
- What does // do? Give an example with negative numbers.
- What is the result of 5 in [1, 2, 3, 4, 5]?
- When should you use is instead of ==?
- What is operator precedence?
- What does the walrus operator := do?
⚡ Whisper
Operators are the small symbols that hold great power. A plus sign adds. An equals sign assigns. Two equals signs compare. A single ampersand reaches into the binary heart of a number. Each operator is a verb, an action, a tiny spell. Learn them one by one. The arithmetic operators are your first friends. The comparison operators become your judges. The logical operators become your decision-makers. The membership and identity operators become your seekers. And the assignment operator becomes your memory. Together, they form the language of computation. You do not need to memorize the precedence table. Parentheses are free. Use them. Write clearly. The computer will understand. More importantly, other humans will understand. That is the real magic of operators: not what they do, but how clearly they express your intent.