Fun fact: boolean operators can be chained together!
It's important to know that boolean operators are not evaluated straight across from left to right all the time; just like with arithmetic operators, where /
and *
are evaluated before +
and -
(remember Please Excuse My Dear Aunt Sally?), there is anorder of precedence or order of operations for boolean operators. The order is as follows:
not
is evaluated first;and
is evaluated next;or
is evaluated last.
This order can be changed by including parentheses (()
). Anything in parentheses is evaluated as its own unit.
For instance, True or not False and False
returns True
. Can you see why? If not, check out the Hint.
Best practice: always use parentheses (()
) to group your expressions to ensure they're evaluated in the order you want. Remember: explicit is better than implicit!
# Assign True or False as appropriate on the lines below!
# False or not True and True
bool_one = False or ((not True) and True)
#False
# False and not True or True
bool_two = (False and (not True)) or True
#True
# True and not (False or False)
bool_three = True and not (False or False)
#True
# not not True or False and not True
bool_four = (not (not True)) or (False and (not True))
#True
# False or not (True and True)
bool_five = False or (not (True and True))
#False
print bool_one,bool_two,bool_three,bool_four,bool_five
Go ahead and assign True
or False
as appropriate for bool_one
throughbool_five
. No math in this one!