Short Circuiting Techniques in Python:

Short circuiting means stopping the execution of Boolean operation when the truth value of the expression has been determined already. The evaluation will be done from left to right. In Python for Data Science, various Boolean operations and functions supports short ciruiting.

Short circuiting in Boolean operators:

Below we have provided insight into short circuiting in case of Boolean expressions. Boolean operators will be order by ascending priority.

Short Circuiting Techniques in Python for Data Science- PST Analytics

or: When Python interpreter scans the or expression, it will take the first statement and check to see if it is true. In case the first statement is true, Python will return the object’s value without checking the second statement. In case the first value is false, then Python will check the second value, and then it returns the result based on the second half.

and: In this case Python will use a short circuit technique for checking whether the first statement is false and then the whole statement must be false, then it will return the value. Only when the first value is true, it will check the second statement and then return the value.

When we achieve the true value of expression, the expression containing and and or will stop execution. Evaluation takes place from left to right.

Short Circuiting Techniques in Python for Data Science- PST Analytics

Short circuiting in all() and any():

In Python, we have in-built functions all() and any() which supports short-circuiting.

Short Circuiting Techniques in Python for Data Science- PST Analytics

Short circuiting in conditional operators:

Conditional operators will also follow short circuiting when expression result is obtained, but further execution is not required.

Short Circuiting Techniques in Python for Data Science- PST Analytics

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.