
Case 2: Python evaluates false_func(), which returns False.You can confirm this by seeing both functions’ output. To determine the final result, Python evaluates false_func() and gets False. Case 1: Python evaluates true_func(), which returns True.> true_func () and false_func () # Case 1 Running true_func() Running false_func() False > false_func () and true_func () # Case 2 Running false_func() False > false_func () and false_func () # Case 3 Running false_func() False > true_func () and true_func () # Case 4 Running true_func() Running true_func() True To demonstrate the short-circuiting feature, take a look at the following examples: If it’s true, then the whole expression is true. In this case, the final result depends on the right operand’s truth value. In contrast, the and operator evaluates the operand on the right only if the first operand is true. Python prevents this by short-circuiting the evaluation.

It would be a waste of CPU time to evaluate the remaining operand. Having a false left operand automatically makes the whole expression false. In this situation, there’s no need to evaluate the operand on the right. If it’s false, then the whole expression is false. To determine the final result of an and expression, Python starts by evaluating the left operand. In other words, Python evaluates the operand on the right only when it needs to. Python’s logical operators, such as and and or, use something called short-circuit evaluation, or lazy evaluation. Remove ads Short-Circuiting the Evaluation Even if you don’t use all the features of and, learning about them will allow you to write better and more accurate code.
#BOOLEAN OPERATORS RCODE HOW TO#
You’ll also code a few practical examples that will help you understand how to use the and operator to approach different problems in a Pythonic way.

Watch Now This tutorial has a related video course created by the Real Python team.
