Does Python have Short Circuit Evaluation?

In this article, we will learn what is short-circuit evaluation is in python. We have two conditions like or & and in a short-circuit evaluation in python. At the end of the article, you will completely learn about short circuit in python. This short-circuit will stops the execution when the truth value is determined. Let us start to learn.

The short-circuiting technique is the process of stopping the execution of the boolean operation. It stops the execution when the truth value of the expression is determined. Always the expression of evaluation is from left to right. 

Let us see the tabular column for a better understanding of the short-circuit techniques in python.

OperationResultDescription
x or yIf x is False, then y else xThis operation evaluates the second argument that is y. If the first argument, x is False.
x and yIf x is False, then x else yThis operation evaluates the second argument that is y. If the first argument, x is True.
not xIf x is False, then True, else FalseNot has a lower priority than non-boolean operators.

Short Circuit Evaluation in Python: or Operation

Python checks the expression based on the short circuit evaluation. In the table itself, we have learned that “evaluates the second argument that is y, if the first argument x is False”. That is, it takes the first argument if it is True. It will return the first argument as it is. It does not worry about the second argument. The second argument is only returned when the first argument becomes False. In and expression, the evaluation takes place from left to right. When the truth value of the expression is reached, the execution will be stopped.

Code 1:

def result():
    return "Python Pool"
print (False or result())

Creating a function named check. Here we are giving the first statement as False to evaluate the second argument and return the output.

Output

Python Pool

Code 2:

def result():
    return "Python Pool"
print (True or result())

Now we will see what the output is if the first argument is True.

Output

True

Short Circuit Evaluation in Python: and Operation

In and expression, we have a condition that is if the first argument is False, then the whole expression must be False. So it returns the argument as it is. It checks the second argument only when the first argument value is True. In and expression, the evaluation takes place from left to right. When the truth value of the expression is reached, the execution will be stopped.

Code 1:

def result():
    return "Python Pool"
print (True and result())

Creating a function named check. We have already learned that if the first condition is True, it will check the argument. So in this statement, we have given the first statement as True. So it will return the statement.

Output

Python Pool

Code 2:

def result():
    return "Python Pool"
print (False and result())

We have already learned in the above condition. If the first statement is false, it will return the statement as it is. So let us check the condition with False as a first argument.

Output

False

Conditional Operators using Short Circuit Evaluation in Python

Now let us see about conditional operators using short-circuit. When we get the expression result, the execution will be stopped to use the short-circuit for conditional operators.

Using and condition

def result():
    return "The given message is True"
print (11>3 and result())
print(10<1 and result())

First, it will check the first condition. If it is true, It will display the message given in the main function to the user. Otherwise, it will return False as a result.

Output

The given message is True
False

Using or condition

def result():
    return "The given message is False"
print (11>3 or result())
print(10<1 or result())

We know that if the first condition is True. It will return True as a result. If it becomes False, it will move to the second argument.

Output

True
The given message is False

Errors are not triggered due to short-circuit

x = True
print(x or y)
print(x and y)

Error is not triggered in or operator due to short-circuit but in and operator, it is triggered.

Output

True
NameError: name 'y' is not defined

1. In which direction will the evaluation take place?

The evaluation of expression takes place from left to right.

2. What are the two conditions in short circuit evaluation?

And & or the conditions in short circuit evaluation.

Conclusion

So far, we have completely learned about short circuit techniques in python. We hope this article is easy and understandable. Both the conditions are different from each other. Learn it clearly without any confusion. In case of any queries, do let us know in the comment section. We will be here to help you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments