Cases & Exceptions

This topic covers all important exceptions, edge cases, and unexpected behaviors related to Python operators. These are situations where operators behave differently than beginners expect and often cause bugs, wrong logic, or interview traps.

What Do We Mean by “Exceptions” in Operators?

Here, exceptions do not only mean runtime errors, but also:
  • Unexpected outputs
  • Tricky behavior
  • Special rules
  • Python-specific shortcuts
  • Cases where operators don’t behave mathematically

1. Division by Zero (Arithmetic Exception)

python
print(10 / 0)
Raises:
text
ZeroDivisionError

But This Works

python
print(10 // 0)
print(10 % 0)
Both also raise ZeroDivisionError.

2. Integer Division vs Floor Division (Negative Numbers)

python
print(5 // 2)
python
print(-5 // 2)
Explanation:
  • Floor division rounds down, not towards zero
This surprises many learners.

3. Modulus with Negative Numbers

python
print(5 % 2)
python
print(-5 % 2)
Rule:
text
a % b = a - (a // b) * b

4. / Always Returns Float

python
print(10 / 5)
Even though result is whole number, output is:
text
2.0
To get integer:
python
print(10 // 5)

5. Comparison Between Different Data Types

python
print(10 == "10")
Valid comparison → returns False
But this raises error:
python
print(10 > "10")
Reason: Python does not allow ordering between incompatible types.

6. Chained Comparison Trap

python
print(5 < 10 > 3)
This means:
python
(5 < 10) and (10 > 3)
Not:
python
5 < (10 > 3)

7. and / or Do NOT Return Boolean Always

python
print(10 and 20)
python
print(0 or 50)
Rules:
  • and → returns first falsy or last value
  • or → returns first truthy value

8. Short-Circuiting Skips Errors

python
print(False and (10 / 0))
No error occurs because second expression is never evaluated.

9. Boolean Arithmetic Exception

python
print(True + True)
python
print(False * 10)
Why?
  • True → 1
  • False → 0

10. is vs == (Identity Exception)

python
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)
print(a is b)
Rule:
  • == → value
  • is → memory reference

11. Small Integer Caching Confusion

python
a = 256
b = 256
print(a is b)
python
x = 1000
y = 1000
print(x is y)
Small integers may share memory, large ones usually don’t.

12. Dictionary Membership Checks Keys Only

python
data = {"a": 1, "b": 2}

print("a" in data)
print(1 in data)
To check values:
python
print(1 in data.values())

13. Bitwise vs Logical Operator Confusion

python
print(True & False)
python
print(True and False)
Bitwise works on bits, logical works on truth values.

14. Operator Precedence Confusion

python
print(10 > 5 and 5 > 20 or 2 > 1)
Always use parentheses:
python
print((10 > 5 and 5 > 20) or (2 > 1))

15. Assignment Inside Conditions (Walrus Exception)

python
if (n := len("Python")) > 3:
    print(n)
Without walrus:
python
# if n = len("Python"):  ❌ SyntaxError

16. Immutable vs Mutable Operator Side Effects

python
a = [1, 2]
b = a
b += [3]

print(a)
But:
python
a = (1, 2)
b = a
b += (3,)

print(a)
print(b)
Lists mutate, tuples create new objects.

17. not Has Lower Precedence Than You Think

python
print(not 10 > 5)
Equivalent to:
python
print(not (10 > 5))

18. Set Removes Duplicates Automatically

python
print({1, 1.0, True})
Because:
python
1 == 1.0 == True

19. in with Strings vs Lists

python
print("app" in "apple")
python
print("app" in ["apple", "banana"])
Substring vs element check.

20. Assignment Has Lowest Precedence

python
x = 10 + 5 * 2
Equivalent to:
python
x = (10 + (5 * 2))

Summary

  • Python operators have many hidden rules
  • and / or return values, not booleans
  • is checks identity, not equality
  • Division, modulus, and floor division behave differently
  • Operator precedence can silently change logic
  • Mutable objects cause side effects
  • Short-circuiting can prevent errors

Key Advice

If an expression:
  • Looks confusing
  • Mixes multiple operators
  • Uses and, or, not, is
Always add parentheses.
This topic alone can prevent most beginner and intermediate Python bugs.