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 to True only if both operands are True (in this case, the operands are expressions that evaluate to a Boolean value). If either one of them evaluates to False, the entire expression evaluates to False.
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 to True if either one of the operands evaluates to True. If they both evaluate to False, the whole expression evaluates to False.
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 is False, it evaluates to True and if the expression to its right is True, it evaluates to False.
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 is False, the entire expression will evaluate to False.
    So…. What happens if the first expression is FalseIt 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 be False.
    Let me illustrate this:
    If we have:
    >>> 5 > 6 and 8 < 9
    The expression would be evaluated from left to right.
    First, the value of 5 > 6 would be determined. It’s False.
    If we already know that the and operator returns False when either one of the expressions is False, there is no need to evaluate to second 
    expression if we already know that the first expression is False.
    >>> 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 is True, the or operator returns True.
    So… what happens if the first expression is True?
    The value True will be returned automatically because there is no need to determine if the second expression is True.
    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