- Related Questions & Answers
- JavaScript -Short-circuiting in boolean
- Looping Techniques in Python
- Looping Techniques in C#
- Looping Techniques in Java
- Biometric Identification Techniques
- Convert short primitive type to Short object in Java
- Verification techniques in testing domain
- Introduction to Sorting Techniques
- Techniques for Requirements Gathering
- Data Replication techniques in SAP HANA
- Parameter Passing Techniques in C/C++
- Useful Techniques to Conduct Procurement
- Estimation Techniques for Project Scheduling
- Techniques To Resolve Workplace Conflicts
- Short Circuit Assignment in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

A common mistake for people new to programming is a misunderstanding of the way that boolean operators works, which stems from the way the python interpreter reads these expressions. For example, after initially learning about "and " and "or" statements, one might assume that the expression X = (‘x’ or ‘y’) would check to see if the variable X was equivalent to one of the strings ‘a’ or ‘b’. This is not so. To understand, what i’m trying to say, start an interactive session with the interpreter and enter the following expressions:

>>> 'x' == ('x' or 'y') True >>> 'y' == ('x' or 'y') False >>> 'x' == ('x' and 'y') False >>> 'y' == ('x' and 'y') True

At this point, the and and or operator seem to be broken. It doesn’t make sense that, for the first two expression, ‘x’ is equivalent to ‘x’ or ‘y’ is not. Furthermore, it doesn’t make any sense that ‘y’ is equivalent to ‘x’ and ‘y’. After examining what the interpreter does with Boolean operators, these results do in fact exactly what you are asking of them, it’s just not the same as what you think you are asking.

Incase of an or expression, the python interpreter first takes the first statement and checks to see if is true. When the first statement is true, then python returns that object’s value without looking into the second argument. This is because for an or expression, the whole thing is true if one of the values is true; and the program doesn’t look into second statement. However, if the first object value evaluates to false, python checks the second statement and returns that value. The second half determines the truth value of the expression since the first half was false. This “laziness” on the part of the interpreter is called “short-circuiting” and is a common way of evaluating Boolean expression in many programming languages.

Similarly, for an and expression, python uses a short circuit technique to speed truth value evaluation. If the first statement is false then the whole thing must be false and it returns that object value (false) else, if the first value is true it checks the second and returns that value. Let’s take a look at what the interpreter “sees” as it goes through the code

'x' == ('x' or 'y') # Look at parentheses first, so evaluates "('x' or 'y")" # 'x' is a nonempty string, so the first value is True >>> 'x' == 'x' # the string 'x' is equivalent to the string 'x' , so our expression is True True

'y' == ('x' or 'y')# Look at parentheses first, so evaluates expression "('x' or 'y')" # 'x' is a nonempty string, so the first value is True #Return that first value : 'x' 'y' == 'x'# the string 'y' is not equivalent to the string 'x', so the expression is False

>>> 'x' == ('x' and 'y')# Look at parentheses first, so evaluate expression "('x' and 'y')" #'x' is a nonempty string, so the first value is True, examine second value # 'y' is a nonempty string, so second value is True #Return that second value as result of whole expression: 'y' >>> 'x' == 'y'# the string 'x' is not equivalent to the string 'y', so expression is False False

>>> 'y' == ('x' and 'y')# Look at parenthese first, so evaluates expression "('x' and 'y')" True # 'x' is a nonempty string, so the first value is True, examine second value # 'y' is a nonempty string, so second value is True # Return that second value as result of whole expression: 'y' >>> 'y' == 'y'# the string 'y' is equivalent to the string 'y', so expression is True True

Short-circuit evaluation means that when evaluating Boolean expression like AND and OR, you can stop as soon as you find the first condition which satisfies or negates the expression.

Short circuit explained by official documentation:

Operation | Result | Description |
---|---|---|

x or y | If x is false, then y else x | Only evaluates the second argument(y) if the firs one is false |

x and y | If x is false, then x else y | Only evaluates the second argument(y) if the first one(x) is True |

not x | If x is false, then True, else False | Not has a lower priority than non-boolean operators |

Advertisements