While trying to debug an issue, I hit some weird boolean evaluation happening in python. Below is the sample shell result.
In [4]: True or False and False
Out[4]: True
In [5]: False or True and False
Out[5]: False
What exactly is happening here. Shouldn't both evaluate to the same result?
and binds tighter than or, so what you're writing is actually
In [4]: True or (False and False)
Out[4]: True
In [5]: False or (True and False)
Out[5]: False
and takes precedence over or. See what happens when we evaluate the and first?
>>> (True or False and False) == (True or False)
True
>>> (False or True and False) == (False or False)
True
Based on the python order of precedence AND has higher precedence than OR hence AND will get evaluated first followed by OR.
So it evaluates to something like below
(True or (False and False)) = True
(False or (True and False)) = False
Related
Can someone explain, step by step why True or not False and False resolves to True?
I understand that True or not False resolves to True or True and false,
but why does True or True and False resolve to True or False?
This expression:
True or not False and False
Corresponds to the following fully-parenthesized expression:
(True or ((not False) and False))
This evaluates as follows:
(True or (True and False))
(True or False)
True
The reason whey the parentheses are like they are is that the precedence in Boolean logic goes like this:
not (like negation)
and (like multiplication)
or (like addition)
I'd expect this to evaluate essentially the same way in any mainstream programming language.
Operator precedence in boolean-logic in python
print(False==True or False) #answer is True
print(False==(False or True))# is it True or False if either why?
print((False==False)or True) # answer is True`
It is False.
print(False==(False or True))
You need to evaluate the boolean value in paranthesis first. In paranthesis (False or True) evaluates True because of or then False==True evaluates False obviously .
print(False==True or False) #answer is **False**
print(False==(False or True))# Answer is **False**
print((False==False)or True) # answer is **True`**
For print(False==(False or True))
step1: print(False==(False or True))
step 2: step 1 become print(False==True) **because (0+1=1)**
step 3: print(False)
Evaluate the expression step-by-step:
False == (False or True)
=
False == True
=
False
The or operator has a lower precedence than the equality operator ==. And () has the highest precedence among the three.
So in the case:
print(False==True or False), the right side of the expression True or False is evaluated last as == has a higher precedence than or. The first expression False==True is evaluated to False.
Now the expression becomes False or False so it results in False.
print(False==(False or True)) the second part of the expression (False or True)results in True as () has the highest precedence and False==True is False.
print((False==False)or True) the expression (False==False) is True due to the () precedence then the or is applied to the resulting expression, True or True it is True.
I'd expect the following two code blocks to be evaluated the same way, but it would seem that is not the case. For example, with this:
if True or False and False:
print('True')
else:
print('False')
True is printed. But with this:
if (True or False) and False:
print('True')
else:
print('False')
False is printed. Here's my breakdown of the logic:
True or False = True
True and False = False
By substitution, (True or False) and False = True and False = False.
Why does this happen?
This is because of operator precedence. Per the Python 2.x and 3.x docs, the and operator has higher precedence than the or operator. Also, have a look at the boolean truth table:
That means in your expression:
if True or False and False:
In the expression, False and False is grouped together because of precedence. That means Python evaluates it as:
if True or (False and False):
Now, it's evaluated left to right. Since the first condition is True, it short-circuits and skips the second condition and evaluates to True printing 'True'. (This short-circuits because if the first side is true, it has to be true.)
Now in your second example:
if (True or False) and False:
This makes True or False evaluate first, which gives True. Then it does True and False which is False, printing 'False'.
>>> print(True or False)
True
>>> print(True and False)
False
(True or False) evaluates to True. Therefore (True or False) and False evaluates to True and False, which evaluates to False.
This answer explains the rules for boolean evaluation quite well: https://stackoverflow.com/a/16069560/1470749
Standard Order of operations gives and precedence over or, so the first statement True or False and False is logically equivalent to
True or (False and False)
This evaluates to True.
The second statement is
(True or False) and False
This evaluates to False.
It says in python 2.7 docs that or has lower precedence than and. But when I type in idle this:
>>> True and True or False
True
>>> True and False or True
True
>>> True and False
False
Why is the result of this True and False or True expression True?
Higher precedence means that an operator would be evaluated before an operator with lower precedence, like, e.g., in arithmetic, multiplication should evaluated before addition, so 1 + 2 * 3 will result in 7 and not 9.
In your usecase, True and False is evaluated first, giving False. This result is then evaluated with the or operator (i.e., False or True), resulting in True.
In fact, operator precedence has nothing to do with this result; it would be the same wherever you put the parentheses, since or always returns True if either of its arguments are true. So:
True and (False or True) == True and (True) == True
(True and False) or True == (False) or True == True
You statement is asking to do the following
First python evaluates the expression on the left;
Evaluation 1: True and false (Since this evaluates to false python then looks to the or expression)
Evaluation 2: True or false
Which then evaluates to true
You may also want to take a look at Boolean logic and truth tables to assist with understanding how this works.
Highest precedence means where you will put the parentheses
((True and True) or False) # True
((True and False) or True) # True
(True and False) # False
I cant seem to grasp Bootlean operators. I'm using an example from Code Academy. At first I read it wrong and was putting True or not False and False Instead of True or False.
Can someone explain this a bit more clearly for me so I can get more of an understanding.
Assign True or False as appropriate for bool_one through bool_five.
Set bool_one equal to the result of False or not True and True
Set bool_two equal to the result of False and not True or True
Set bool_three equal to the result of True and not (False or False)
Set bool_four equal to the result of not not True or False and not True
Set bool_five equal to the result of False or not (True and True)
You have three boolean operations and some rules:
not, which just inverses (aka not True => False, not False => True)
or, which works on two operands x or y and returns True if either is True and False if they are both False
and, which works on two operands x and y and returns True if both are True and False otherwise
They are evaluated from left to right
not has the highest precedence, and after that, lastly or
You can use () to change the precedence of operations, just as in everyday math
5.15 Operator Precendence (listed lowest to highest, and I trimmed many operators)
or
and
not
(expressions...)
So you can see that from low to high precedence it is or then and then not then (). Example
False or not True and True
So if I add parentheses to emphasize order
(False or (not True)) and (True)
becomes
(False or False) and (True)
False and True
False
You can follow this process with the other lines
A boolean variable can have one of two values - True or False. There are several operators that can help you manipulate boolean values:
not inverts the value - True becomes False and vise-versa
and returns True if and only if both operands evaluate to True
or returns True if either or the operands evaluate to True
This exercise asks you to translate the English statements to python:
bool_one = False or not True and True
bool_two = False and not True or True
bool_three = True and not (False or False)
bool_four = not not True or False and not True
bool_five = False or not (True and True)