print(not 42 == "any_string")
click here for image
not 42 evaluates to False and "any_string" would evaluate to a boolean True.
So, not 42 == "Answer" should evaluate to False but it evaluates to True. How is this possible?
it's order of preference
first this condition will be executed 42 == "any string" and the output of this is False
then comes not so not False is True
this is why the output is True
The true order is:
First: 42 == "any_string" --> false
Second: not false --> true
So,the input is true
42 == "any_string" is a conditional statement.
So,not 42 == "any_string" will judge the 42 == "any_string"firstly,
just like if 42 == "any_string".
Related
I've just started learning Python and i was trying this
[] == False #False
but :
bool([]) #False
from what i got values like [],0 .. are False what did i missed exactly and thanks!
The operator == is very literal. If the 2 things you are comparing are not exactly the same (this includes types, like "2" == 2 is False) then the result will always be False. So the boolean False is not literally the same thing as an empty list [] which is why [] == False is False.
An empty list is just treated as "False" when converted to a boolean, which you did with bool([]). So the output of bool([]) is False which is literally the same as False. Thus bool([]) == False is True.
[] != False, but bool([]) == bool(False).
In my attempt to learn Python I have been writing code from tutorials and my own. I am using Python 3.
The piece of code that is doing me in:
>>>print(not 1 == 1 or 6 == 6 and 9 == 9)
True
I've run the separate parts of this code. It seems the OR operator is negating itself with a double True. (not True or True) outputs True instead of False? Isn't the or operator suppose to conclude upon the first True and the not operator returns True as False?
You should look in to operator precedence.
Let's examine this expression and handle each operator according to their precedence:
not 1 == 1 or 6 == 6 and 9 == 9
First, the == operators are executed, so we get:
not True or True and True
Then, the not operator:
False or True and True
Then, the and operator:
False or True
Then, the or operator, producing the result you're seeing:
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'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