Short circuiting in Python with Logical Operators on NonBoolean values
Learned something new today in Python and so wanted to share here..
I am sure any python programmer would know how the logical operators work with Boolean Type. For example, I am sure we know the output of python statements is as shown below:
True and True: True
True and False: False
False and True: False
False and False: False
True or True: True
True or False: True
False or True: True
False or False: False
Now, did you try the logical operators with Non-Boolean? If so, what would be the output?
Let’s see a few python statements below and the output from python IDLE, and then try to understand why/how the output is determined:
Rationale:
Every string and non-string value in python has a boolean value, based on the data type:
Integer: 0 is False, anything else (either negative or positive integer) is True
Float: 0.0 is False, anything else is True
String: ‘’ i.e. empty string is False, anything else is True
Complex: 0j, 0+0j is False, anything else is True
Let’s try to interpret the output from commands above in P1:
1 and 2:
Here boolean value of 1 is True and, boolean value of 2 is True. True and True should return True. However, we get the output as 2. But, why???
To understand this, we need to understand the concept of Short circuiting technique.
Short Circuiting:
“By short-circuiting, we mean the stoppage of execution of boolean operation if the truth value of expression has been determined already”.
Let’s take 1 (True) and 2 (True) again:
When you think logically, here, the output (i.e. the truth value of the expression) is decided based on the 2nd value. i.e. when the first value is True, it still needs to look for the 2nd value to make a decision, which is ‘True’ here. So, the output is 2nd value (2) here.
1 (True) and 0 (False):
Again, the output is dependent on the 2nd value, which is False (0).
0 (False) and 1 (True):
Here the output is dependent on 1st value, because, when the first value is ‘False’, no matter what the 2nd value is, the output is going to be ‘False’ i.e. the truth value of expression is already determined, and hence there is no need to evaluate the 2nd value. Hence the output is False (0).
0 (False) and 0 (False):
Here, the output is first value ‘False’ (0), again, due to short circuiting.
Let’s try to understand the same with Logical Operator ‘or’:
With ‘or’, if one value is ‘True’, the output would be automatically ‘True’.
1 (True) or 2 (True):
Output is True (1). Again, short circuiting comes into play here.
1 (True) or 0 (False):
Output is True (1). Again, short circuiting here.
0 (False) or 1 (True):
Output is True (1). Here, first value is ‘False’ and so needs to look for 2nd value and if it is True, output would be ‘True’. If not, output would be ‘False’. So, output would be the 2nd value (1).
0 (False) or 0 (False):
Output is False (0). Here, again, output is dependent on 2nd value, False (0).
References:
https://www.geeksforgeeks.org/short-circuiting-techniques-python/
https://www.udemy.com/share/108qka3@ghyzA2t46lczZFEBH-u7PvzlRgNl9LwXUhJAZu-HcboaP1A85h8U8HxVYRmgDspyIg==/