Understanding Python’s Logical Operators: Values and Short‑Circuit Behavior
The article explains how Python’s and/or operators work with any expression type, why they may return non‑boolean values, and demonstrates short‑circuit evaluation through concrete print examples showing the actual results, including cases where the left operand determines the outcome without evaluating the right side.
Python logical operators and and or can be applied to expressions of any type, and the result of the operation is not limited to a Boolean value; it can be the actual value of one of the operands.
Both operators use short‑circuit evaluation: the interpreter may stop evaluating as soon as the final result is known, so the right‑hand expression is sometimes never executed.
Importantly, and and or return one of their operand values rather than the literal True or False. The returned value is the left operand when it determines the outcome, otherwise it is the right operand.
For and, if the left operand is falsy, the result is that left value and the right side is not evaluated. If the left operand is truthy, the right operand is evaluated and its value becomes the result.
For or, if the left operand is truthy, the result is that left value and the right side is skipped. If the left operand is falsy, the right operand is evaluated and its value is returned.
Example demonstrations:
print(100 and 200) # 200</code><code>print(45 and 0) # 0</code><code>print(False or 12) # 12These outputs show that the operators return the actual operand values rather than Boolean literals.
Further short‑circuit examples:
print(200 and 19) # 19</code><code>print(87 and 0) # 0</code><code>print(12 or False) # 12</code><code>print(0 or 87) # 87The corresponding outputs confirm the described evaluation rules: when the left side already determines the logical outcome, the right side is not computed, and the returned value is the operand that decides the result.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
