Short-circuiting in computer science is completely different from what we usually think about it. Today many programming languages account for when a two-part boolean expression in a program using the AND or OR operator is evaluated, and only the first part of the expression needs to be considered, completely skipping the second part.
Before we discuss short-circuiting in Python in detail, it is important to understand the AND and OR operators and observe their truth table.
AND operator
The AND operator returns TRUE only when both the relations or operands in the expression evaluate to true. Consider the below truth table for more detail.
It can be observed that the AND operator evaluates to TRUE only when both the inputs are true; otherwise, the output is false.
OR operator
The OR operator returns TRUE when either of the relations or operands in the expression evaluates to true. Consider the below truth table for more detail.
It can be observed that the OR operator evaluates to FALSE only when both the inputs are false. In all other cases, the output is true.
Now that we are familiar with the way AND and OR operators behave, we can discuss the concept of short-circuiting in Python.
Short-circuiting in OR operation
We know that the OR operator evaluates to FALSE only when both the inputs are false. Whenever we are evaluating an expression involving OR operator, we can stop checking the condition as soon as we encounter the first operand that is true. This is known as short-circuiting. This speeds up our calculation and greatly improves system performance. If there was no short-circuiting, then the interpreter would continue to check the expression if even it found that one of the operands is true.
Consider the code below:
print(2/0)
The output of the above code is:
ZeroDivisionError: division by zero
Since we cannot divide a number by zero, therefore the output was correctly displayed as an error. Now let us see short-circuiting in 'or' in Python.
if (True or 2/0):
print("I checked only the first operand and did not check the second one.")
else:
print("I also checked the second operand")
The output of the above code is:
I checked only the first operand and did not check the second one.
But why is this so? Since we tried dividing by zero, it should throw an error just like it did in the previous example. The reason is short-circuiting. As soon as the interpreter found that the first operand is true, it skipped checking any other operand because OR operation evaluates to true if at least one of the operands is true.
We can guarantee that the second condition, i.e., 2/0 was not checked because if it had been checked, we would see an error at the console.
The same is not true if the first statement is false. The interpreter will continue to check the rest of the statements until it finds a true value. If, after checking all the conditions, no true value is found, then the expression is finally evaluated to false.
Example:
if (False or 2/0):
print("I checked only the first operand and did not check the second one.")
else:
print("I also checked the second operand")
The output of the above python code is:
ZeroDivisionError: division by zero
Surprisingly neither the statement inside the if or else block is displayed in the output. Rather we have an error. The reason is that in the first if expression, the first statement is false. Therefore the interpreter continues to evaluate the 'or' expression. The second statement is 2/0, which gives an error. So we showed here that short-circuiting occurs in 'or' operation only when the statement is true. If the statement is false, we cannot guarantee that the entire expression is false.
We are already aware that the AND operation evaluates to true only when both the operands involved turn out to be true. Even if one of the statements is false, then the result of the AND operation is false. It means that if even one of the statements turns out to be false, the interpreter can stop examining the other statements in the expression and conclude that the overall result of the expression is false.
Consider the code below:
if (False and 3/0):
print("both the statements is checked")
else:
print("only the first statement has been checked")
The output of the above code is:
only the first statement has been checked
As we can see that the statement inside the else block is executed, this means that the if expression was checked and turned out to be false. Now, if the "if" statement was checked completely, then it must display an error since 3/0 is not possible. Therefore we can claim that only the first statement in the if the expression was checked, and since it was false, then the interpreter did not check the other statement. In other words, the expression was short-circuited.
Example 2:
if (True and 3/0):
print("both the statements is checked")
else:
print("only the first statement has been checked")
The output of the above code is:
ZeroDivisionError: division by zero
As expected, we had an error. This is because when the first statement in the if the expression is checked, it turns out to be true. However, we still cannot conclude that the entire expression will be true hence the interpreter continues to check the expression and finally gives an error because of 3/0.
Short-circuiting in Python is a technique by which execution of boolean expression containing 'or' and 'and' operator in Python is halted as soon as the truth value of the expression is determined.
2. How does short-circuiting occur for 'or' operator?
As soon as the interpreter finds a statement that is true, it will short circuit and no longer check the other statements in the expression.
3. How does short-circuiting occur for 'and' operator?
As soon as the interpreter finds a statement that is false, it will short circuit and no longer check the other statements in the expression.
4. Name two Python in-built functions that support short-circuiting.
any() and all() are two functions that support short-circuiting in Python.
5. What is the use of short-circuiting in Python?
It makes the computation faster by not checking the other statements once the truth value is established.
Key takeaways
Today many programming languages account for when a two-part boolean expression in a program using the AND or OR operator is evaluated, and only the first part of the expression needs to be considered, completely skipping the second part.
The AND operator returns TRUE only when both the relations or operands in the expression evaluate to true.
The OR operator returns TRUE when either of the relations or operands in the expression evaluates to true.
Short-circuiting in Python is a technique by which execution of boolean expression containing 'or' and 'and' operator in Python is halted as soon as the truth value of the expression is determined.
Whenever we are evaluating an expression involving OR operator, we can stop checking the condition as soon as we encounter the first operand that is true.
Even if one of the statements is false, then the result of the AND operation is false. It means that if even one of the statements turns out to be false, the interpreter can stop examining the other statements.