Boolean Algebra - Why is not(True and False) True? - python

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

Related

Understanding some more complex boolean logic

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.

what exactly it means in python that: False==(False or True) is False?

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 don't understand operator precedence in python True and False or True

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

Why is `True is False == False`, False in Python? [duplicate]

This question already has an answer here:
Why does (1 in [1,0] == True) evaluate to False?
(1 answer)
Closed 7 years ago.
Why is it that these statements work as expected when brackets are used:
>>> (True is False) == False
True
>>> True is (False == False)
True
But it returns False when there are no brackets?
>>> True is False == False
False
Based on python documentation about operator precedence :
Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section.
So actually you have a chained statement like following :
>>> (True is False) and (False==False)
False
You can assume that the central object will be shared between 2 operations and other objects (False in this case).
And note that its also true for all Comparisons, including membership tests and identity tests operations which are following operands :
in, not in, is, is not, <, <=, >, >=, !=, ==
Example :
>>> 1 in [1,2] == True
False
Python has a unique transitive property when it comes to the comparison operators. It will be easier to see in a simpler case.
if 1 < x < 2:
# Do something
This does what it looks like. It checks if 1 < x and if x < 2. The same thing is happening in your non-parenthesized code.
>>> True is False == False
False
It is checking whether True is False and False == False, only one of which is true.
This is a double inequality which gets expanded as (True is False) and (False == False). See for instance What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)
Python interprets multiple (in)equalities the way you would expect in Math:
In Math a = b = c mean all a = b, b = c and a = c.
So True is False == False means True == False and False == False and True == False, which is False.
For boolean constants, is is equivalent to ==.
Python performs chaining if it encounters operators of same precedence when evaluating an expression.
comparisons, including tests, which all have the same precedence
chain from left to right
The below mentioned operators have the same precedence.
in, not in, is, is not, <, <=, >, >=, <>, !=, ==
So, when Python tries to evaluate the expression True is False == False, it encounters the operators is and == which have the same precedence, so it performs chaining from left to right.
So, the expression True is False == False is actually evaluated as:
(True is False) and (False == False)
giving False as the output.

not (not false) = True?

I'm currently on page Conditionals & Control Flow, Python, Code Academy.
I've made this thinking it will be False but it is wrong.
Make me false!
bool_three = not (not False) == True
Objects in parentheses are worked out first, so by my logic:
not (not False [which becomes True]) = True
not True [which is false] = True
not (not False [which becomes True]) = True
What makes you think "not not false" would be true? If a boolean value is negated, it becomes the opposite value. If it's negated again, it becomes the original value.
Let's derive it a step at a time...
not (not False) == True
not (True) == True
False == True
False
bool_three = not (not False) == True
Here that's goes :
not ( not False ) become not ( true ) became false.
Then False == True (which is false)
so then bool_three = false
Quick Python interpreter check:
>>> not not False == True
False

Categories