not (not false) = True? - python

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

Related

Why is bool([]) == False while [] == False is False and not True in python boolean logic

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).

How are conditional statements with multiple conditions evaluated?

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.

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

Python, Boolean - Conditionals & Control Flow - Not understanding

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)

True - False dilemma

I can't understand what is happening here?
True = False
False = True
print True, False
output
False False
Isn't the output be printed as False True?
You are setting True = False, and then False = True.
True = False
False = True # But "True" here is now False.
print True, False # True = False, because of the first line. As does False, because you set it equal to "True" which you have already made == False.
I don't know why you would ever want to do this, other than to mess with someone's code, as it's a readability nightmare - as you can see just from the difficulty in using the words to explain it.
If you really want to swap the vaules around, then do:
True, False = False, True
You seem to be using Python 2.
This wouldn't work in Python 3 as True and False were changed to keywords in order to make assignment to those impossible.
Refer to Core Language Changes:
Make True and False keywords. [6]
Reason: make assignment to them impossible.
Once you do True = False, True is no longer a boolean but rather a variable that has been assigned the boolean False. Therefore, the line False = True is actually assigning True's value (False) to the variable False.

Categories