MOD-08 · Sequence #8 · Python Logic

Python Operators

Understanding operator categories: arithmetic, bitwise, comparison, logical, identity (`is` vs `==`), and membership (`in`). The distinction between object equality (==) and identity (is) is one of the most frequently tested topics in undergraduate exams.

Live Interactive Code Snippet (Python 3.12 CPython WASM) Open in Practice Mode →
x = [1, 2]
y = [1, 2]
print(x == y)  # True (equal values)
print(x is y)  # False (different heap identities)

Core Principles & Mental Models

1. Identity (`is`) vs. Equality (`==`)

Memory Pointer vs. Value Equivalence

  • Equality operator (==) checks whether two objects have equivalent contents by delegating to `__eq__()`.
  • Identity operator (`is`) checks whether two references point to the exact same memory address: `id(a) == id(b)`.
  • CPython pre-allocates and caches small integers (-5 to 256) and interned string literals for memory efficiency.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print("list1 == list2:", list1 == list2)  # True (equal contents)
print("list1 is list2:", list1 is list2)  # False (distinct heap objects!)

# Comparing to None must always use 'is'
x = None
print("x is None:", x is None)
💡 Key Takeaway: Always use `==` for comparing values, and reserved `is` strictly for singleton checks like `x is None`.

2. Membership Operators (`in` and `not in`)

Time Complexity Differences Across Data Structures

  • The `in` operator checks if an item exists within a container.
  • In lists and tuples: `item in my_list` takes O(N) linear time search.
  • In sets and dictionaries: `key in my_dict` takes O(1) average constant time hash table lookup.
valid_roles = {"admin", "instructor", "ta"}
print("'student' in valid_roles:", "student" in valid_roles)  # O(1) lookup!
💡 Key Takeaway: Choose sets over lists when performing frequent membership checks in large datasets.

Interactive Walkthrough Examples

Chained Comparison Operators (Basic)

Using mathematical chained comparisons unique to Python.

x = 15
# In C/Java: (10 < x) && (x < 20)
# In Python: clean mathematical chain
is_in_range = 10 < x < 20
print("10 < 15 < 20:", is_in_range)

Explanation: Python chains comparisons seamlessly: `a < b < c` translates to `a < b and b < c`.

Common Misconceptions & Exam Traps

Common Pitfall: Using `== None` Instead of `is None`

Custom classes can override __eq__ to return unexpected booleans. `is None` is guaranteed and faster.

Buggy Code
if result == None:  # Flawed: classes can override __eq__ to return True deceptively!
    pass
Correct Solution
if result is None:  # PEP 8 standard: guaranteed identity check
    pass

Exam Trap Note: PEP 8 explicitly mandates checking singletons with `is None` or `is not None`.

Conceptual Knowledge Self-Check Quiz

Q1: What does `a is b` evaluate in Python?

  1. Checks if a and b have the same data values
  2. Checks if id(a) == id(b) (same memory address)
  3. Checks if a is a subclass of b
  4. Checks if a and b have the same length

The `is` operator checks memory identity: whether both references point to the same physical object in heap.

Algorithmic Practice Challenges

Idiomatic Best Practices

  • ✅ Never compare numbers or strings with `is` — always use `==`.
  • ✅ Leverage chained comparisons (0 <= grade <= 100) for readable condition statements.

All 26 Curriculum Modules