[Baypiggies] What is happening here with true/false comparisons
Asher Langton
langton2 at llnl.gov
Mon Jan 25 22:27:30 CET 2010
On Jan 25, 2010, at 1:12 PM, Max Slimmer wrote:
> Can anyone explain the following:
>
> >>> a = 1
> >>> b = 2
> >>> alist = [5,6]
> >>> print a in alist
> False
>
> >>> a in alist == b in alist
> False
> >>> a in alist == a in alist
> False
> >>> bool(a in alist) == bool(b in alist) # this does what we
> expect
> True
> >>> c = 5
> >>> c in alist == c in alist
> False
> >>>
The '==' and 'in' operators have the same precedence, so the
expression 'a in alist == b in alist' is evaluated left-to-right as:
>>> ( (a in alist) == b) in alist
Since 'a in alist' is False, this is the same as
>>> ( False == b) in alist
which can be simplified to
>>> False in alist
which is False.
-Asher
More information about the Baypiggies
mailing list