Short-Circuit Evaluation in Python: Behind the Scenes
Welcome to this short tutorial on Short-Circuit Evaluation, an important aspect of Python Boolean operators that will be key for your future in Computer Science.
Let’s begin! 👍
🎓 A Quick Recap
In the tutorial: Boolean Expressions and Operators, you learned about truth tables. Here is a quick recap:
- With the
and
operator, an expression evaluates toTrue
only if both operands areTrue
(in this case, the operands are expressions that evaluate to a Boolean value). If either one of them evaluates toFalse
, the entire expression evaluates toFalse
.
For example:
>>> 5 > 6
False
>>> 7 < 9
True
>>> 5 > 6 and 7 < 9 # Equivalent to -> False and True
False
>>> 5 < 6
True
>>> 7 < 9
True
>>> 5 < 6 and 7 < 9 # Equivalent to -> True and True
True
- With the
or
operator, an expression evaluates toTrue
if either one of the operands evaluates toTrue
. If they both evaluate toFalse
, the whole expression evaluates toFalse
.
For example:
>>> 5 > 6 or 7 < 9 # The second expression is True
True
>>> 5 > 6 or 7 < 4 # Both are False
False
- The
not
operator returns the opposite truth value of the expression located to its right. If the expression to its right isFalse
, it evaluates toTrue
and if the expression to its right isTrue
, it evaluates toFalse
.
For example:
>> not 5 > 6
>> not False
>> True
🎉 Now… Short-Circuit Evaluation!
This is where the most interesting part begins. Don’t you notice a pattern in the truth tables that could make the evaluations much more efficient?
Think about it for a moment…
- For the
and
operator: if either one of the expressions isFalse
, the entire expression will evaluate toFalse
.So…. What happens if the first expression isFalse
? It is worth it to evaluate the second expression?The answer is “No”, because we already know that, independently of the result of the second expression, the final result will beFalse
.Let me illustrate this:If we have:>>> 5 > 6 and 8 < 9
The expression would be evaluated from left to right.First, the value of5 > 6
would be determined. It’sFalse
.If we already know that theand
operator returnsFalse
when either one of the expressions isFalse
, there is no need to evaluate to second
expression if we already know that the first expression isFalse
.>>> False and … # No need to evaluate the second one
False
🔔 This makes the process much more efficient, and later when you learn how to use functions and recursion, you will see that this can improve performance tremendously.
- Now let’s see an example with the
or
operator. If either one of the expressions isTrue
, theor
operator returnsTrue
.So… what happens if the first expression isTrue
?The valueTrue
will be returned automatically because there is no need to determine if the second expression isTrue
.This is an example:>>> 5 < 6 or 7 > 9
>>> True or … # No need to evaluate the second one
True
⭐ Quick Tip: Short-Circuit Evaluation with Values
A very interesting feature of short-circuiting is that it can be used to return other values, not just True and False. In the previous examples, Boolean values were returned because the expressions evaluated to Boolean values.
But what do you think will happen if you have this expression?
>>> 0 and 4
Think about this for a moment… what value do you think would be returned?
Well… this example uses two concepts: short-circuit evaluation and Truthy and Falsy values (for more info on Truthy and Falsy values, please read this short tutorial).
Basically, 0 is a Falsy value because it evaluates to False. 4 is a Truthy value because it evaluates to True. Numbers different than 0 evaluate to True.
>>> 0 and 4 # Equivalent to False and True
Now, in Python, using short-circuiting, if the first value before the
and
operator evaluates to False
, that value is automatically returned.
Therefore, the value returned would be 0
Yes, I know this was very surprising! 🎉
Note: The actual value is returned, not the truth value False.
In contrast, if the first value evaluates to
True
, the second value is evaluated and returned.>>> 4 and 5 # Equivalent to True and True
5
With the
or
operator, if the first value evaluates to True
, that value is returned automatically through short-circuiting.>>> 5 or 0 # Equivalent to True and False
5
If the first value evaluates to False, the second value is returned.
>>> 0 or 5
5
This is the basic functionality of Short-Circuit Evaluation in Python! 🎉 I really hope that you found this tutorial helpful. 😃
Please do not hesitate to ask on the discussion forums or right below this post if you have any questions. Your classmates and community TAs will be very glad to help you.
Estefania.
Comments
Post a Comment