Weird expression result

cokofreedom at gmail.com cokofreedom at gmail.com
Mon Aug 18 12:18:14 EDT 2008


On Aug 18, 5:57 pm, George Sakkis <george.sak... at gmail.com> wrote:
> I'm probably missing something obvious but I can't put my finger on
> it:
>
> >>> (3 in [3]) == True
>
> True
>
> >>> 3 in ([3] == True)
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: argument of type 'bool' is not iterable
>
> >>> 3 in [3] == True
>
> False
>
> How/why does the last one evaluate to False ?
>
> George

> >>> (3 in [3]) == True

The list holds an integer value of 3, you check if 3 is in that list,
that is true, you then check it your answer is true...

> >>> 3 in ([3] == True)

True and False are keywords now and do not correspond the way you
think. [3] == True or [3] == False will both answer False.

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = list()
>>> a.append(3)
>>> 3 in a
True
>>> if a:
...     print "yay"
...
yay
>>> if a == True:
...     print "yay"
... else:
...     print "nay"
...
nay

> >>> 3 in [3] == True

http://docs.python.org/ref[3/summary.html

>>> 3 in [3] == True
False
>>> 3 in [3] == False
False

However I come a bit stuck myself, since this should just tell you
that you cannot do a membership test on 3 being in a bool...but it
doesn't...It should be doing the [3] == True, returning false then
trying to see if 3 is in false...



More information about the Python-list mailing list