Concept
Lazy Logic: Short-Circuiting
Python evaluates conditions lazily from left to right. In an `and` chain, if the first check is `False`, Python stops immediately without touching the second half:
python
x = 0
if x != 0 and (10 / x > 1):
print("Safe!")# 50. Lazy Logic: Short-Circuiting Python evaluates conditions lazily from left to right. In an `and` chain, if the first check is `False`, Python stops immediately without touching the second half: ```python x = 0 if x != 0 and (10 / x > 1): print("Safe!") ``` `(10 / x)` would explode with `ZeroDivisionError`, but because `x != 0` is `False`, Python stops early and saves your life.
Checkpoint
Why doesn't `False and (10 / 0)` crash?
Practice
Write an `if` that checks `ready = False`. Check `if ready and (100 / 0 == 0):`. Notice it doesn't crash!
01
Tap code blocks below to assemble
Available Blocks