TLDR; Boolean Expressions in Python return Python Objects and not True or False values. and expressions return left-most False-valued object or right-most True-valued object. or expressions return left-most True-valued object or right-most False-valued object.
Python has this unique behavior where it returns constituent objects when Boolean operators are used in an expression, instead of a True or False value. In this post, I have illustrated why Python does that. A little context before we dive in...
1. Boolean Expression Syntax
Boolean operators in Python are and and or. A typical Boolean expression looks like:
X <operator> Y
where, the operands X and Y are two Python objects and <operator> is either and or or. A few examples of Boolean expressions are:
True and False
False or True
3 or 39
3 or ['a','b','c']
'dev' and {'first': 'John', 'second': 'Doe'}
'dev' or 0
[] and 'hack'
([] and 'hack') and 'win')
2. Boolean Expression Evaluation
Generally, a Boolean expression containing the and operator evaluates to True only if both the operands are True, and evaluates to False otherwise. Whereas, a Boolean expression containing the or operator evaluates to False only if both the operands are False, and evaluates to True otherwise.
Truth Tables of and and or Boolean Operators are as follows:
And now, we dive in...
3. Boolean Expressions In Python
Every object in python has an inherent True or False value. All non-zero and non-empty objects are evaluated as True. For example, 10, 'abc', [1,2,3,4], (1, 'xyz', False), {'name': 'John', 'age': 34}. The number zero i.e. 0 and empty objects such as '', [], {}, () and more are evaluated as False.
In Python, when a Boolean expression is evaluated, it does not return True or False value. Instead, it returns either X or Y i.e. a Python object. The object is returned based on two parameters:
- The inherent value of the objects (
True/False). - The operator used (
and/or).
4. Boolean Expressions Containing and Operator
For expressions containing the and operator, if atleast one object in the expression has an inherent value of False, then the expression returns the first such object that is encountered. The expression will always evaluate to False because False and anything is always False.
In examples 2 and 3, the final result can be determined by evaluating just the first few objects. The rest of the objects are skipped and the result of the expression is returned. This is called Short-Circuit Evaluation. The evaluation takes a shortcut to the end because the final result could be determined before reaching the end.
If the expression contains only True-valued objects, the last such object is returned by the expression. Python evaluates all the operands from left to right in search of a False object and stops at the last operand. Python evaluates and returns the last object, whether true or false, since it determines the result of the expression.
5. Boolean Expressions Containing or Operator
For expressions containing the or operator, if atleast one object in the expression has an inherent value of True, then the expression returns the first such object that is encountered. The expression will always evaluate to True because True or anything is always True.
Short-Circuit Evaluation can be observed in examples 1 and 2.
If the expression contains only False-valued objects, the last such object is returned by the expression. Python evaluates all the operands from left to right in search of a True object and stops at the last operand. Python evaluates and returns the last object, whether true or false, since it determines the result of the expression.
6. Practical Use-Cases
- Initialize a variable.
a = P or Q or R or None
where, a is the variable being initialized and P, Q, R and None are Python objects. Variable a would be initialized to None only if P, Q, and R are zero or empty objects. None can be replaced with a default, non-empty object.
- Filter non-zero and non-empty objects
Filtering out of zero and empty objects is also possible with a slight change in code.
Thank you for reading!! Comment your thoughts...





Top comments (0)