1. Truthy vs. Falsy Values
Standard Boolean Testing Protocol
- In Python, every object has a boolean value defined by its __bool__() or __len__() method.
- Falsy values include: None, False, zero in any numeric type (0, 0.0, 0j), empty sequences and collections ("", (), [], {}, set(), range(0)).
- All other values are Truthy by default.
items = []
if not items:
print("Collection is empty! (Pythonic)")
# Avoid: if len(items) == 0: (redundant in Python)