Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AND operator
3.
OR operator
4.
Short-circuiting in Python
4.1.
Short-circuiting in OR operation
4.2.
Short-circuiting in AND operation
5.
Frequently asked questions
6.
Key takeaways
Last Updated: Mar 27, 2024

# Short-Circuiting in Python

ANKIT KUMAR
0 upvote
Basics of Python
Free guided path
7 chapters
99+ problems
Earn badges and level up

## Introduction

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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.

You can also read about the Multilevel Inheritance in Python and Convert String to List Python.

## Short-circuiting in Python

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:

The output of the above code is:

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.

The output of the above code is:

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:

The output of the above python code is:

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.

Check out Escape Sequence in Python

### Short-circuiting in AND operation

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:

The output of the above code is:

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:

The output of the above code is:

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.

You can try it on online python compiler.

Must Read Python List Operations

## Frequently asked questions

1.What is short-circuiting in Python?

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.

Check out this article - String slicing in PythonFibonacci Series in Python

Never stop learning. Explore more here.

Happy learning!

Guided path
Free
Basics of Python
7 chapters
127+ Problems
Earn badges and level up
Live masterclass