Operators, Truthiness, Identity (is) vs Equality (==) & Walrus Operator (:=)
1Concept
== tests value equality; is tests memory object identity (id(a) == id(b)). The Walrus operator := (Python 3.8+) allows assignment expressions inside conditions.
2Architecture Diagram
a == b : Checks if values are equal (invokes __eq__) a is b : Checks if both point to the EXACT same memory address (id(a) == id(b)) (n := len(data)) > 0 : Assigns n and evaluates condition simultaneously!
3Code Example
Stage 0 Language Foundations
# Identity vs Equality
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(f"list1 == list2: {list1 == list2}") # True
print(f"list1 is list2: {list1 is list2}") # False (Different memory addresses)
# Walrus Operator :=
sample_text = "CareerAI Professional Platform"
if (length := len(sample_text)) > 20:
print(f"Text is long ({length} characters).")4Expected Output
list1 == list2: true list1 is list2: false Text is long (30 characters).
5Key Takeaways
- ✓Always use 'is None' to check for None rather than '== None'.
- ✓Python caches small integers from -5 to 256 for identity reuse.
- ✓The walrus operator := avoids redundant function calls in if/while loops.