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.
Related
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
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'm doing the book "learn python the hard way". At exercise 27 (http://learnpythonthehardway.org/book/ex27.html) it starts with boolean algebra.
So my question is:
why is not(True and False) True?
How I understood it, it should be the same thing as False and True.
Your interpretation is incorrect, see the De Morgan's laws; specifically the negation of a conjunction is the disjunction of the negations.
not (True and False) (a negation of a conjunction == not(a and b)) is the equivalent of False or True (a disjunction of the negations == (not a) or (not b)); note the switch from and to or!
You can also work out the steps:
not(True and False)
work out the part in parentheses first, True and False -> False
substitute that outcome back into the expression: not(False) -> True.
not (True and False) simplifies first to not (False), which then further simplifies to True.
Let's do this step by step:
(True and False) evaluates to (False)
not (False) evaluates to True
Easy enough :)
By order of operations, what is inside the parenthesis is solved first, look at it this way with variables:
a == True and False == False
not(True and False) == not(a) == not(False) == True
What you were thinking of was:
not(True) and not(False)
Which would indeed be False because:
a == not(True) == False
b == not(False) == True
not(True) and not(False) == a and b == False and True == False
You should have a look at De Morgan's laws, which states that (informally):
"not (A and B)" is the same as "(not A) or (not B)"
also, "not (A or B)" is the same as "(not A) and (not B)".
So in your case not(True and False) is the same as not(True) or not(False) which is the same as False or True which is True
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)