Operator Precedence/Boolean Logic

Erik python at lucidity.plus.com
Wed Jun 22 15:43:46 EDT 2016


On 22/06/16 04:40, Elizabeth Weiss wrote:
> I am a little confused as to how this is False:
>
> False==(False or True)

Other people have explained why the expression evaluates as it does - 
the sub-expression "False or True" evaluates to True (as one of the 
operands is truthy). Your expression then becomes "False == True", which 
is of course false.

To get the construct you were expecting (is the thing on the left hand 
side equal to one of the things on the right hand side), you can use 
Python's "in" keyword to "search" a collection (list, tuple, set, 
dictionary etc) that contains the things you are trying to match:

 >>> False in (False, True) # tuple

 >>> False in [False, True] # list

 >>> False in {False, True} # set

 >>> False in {False: None, True: None} # dict

HTH,
E.



More information about the Python-list mailing list