True of False

Casey Caseyweb at gmail.com
Thu Sep 27 12:58:44 EDT 2007


On Sep 27, 12:48 pm, "Simon Brunning" <si... at brunningonline.net>
wrote:
> On 9/27/07, kou... at hotmail.com <kou... at hotmail.com> wrote:
>
> > I tried writing a true and false If statement and didn't get
> > anything?  I read some previous posts, but I must be missing
> > something.  I just tried something easy:
>
> > a = ["a", "b", "c", "d", "e", "f"]
>
> > if "c" in a == True:
> >      Print "Yes"
>
> > When I run this, it runs, but nothing prints.  What am I doing wrong?
>
> Just use
>
> if "c" in a:
>
> and all will be well. The True object isn't the only truthy value in
> Python - see <http://docs.python.org/lib/truth.html>.

I would recommend the OP try this:

run the (I)python shell and try the following:

>>> a = [x for x in "abcdefg"]
>>> a
['a','b','c','d','e','f','g']
>>> "c" in a
True
>>> "c" in a == True
False
>>> ("c" in a) == True
True

The reason your conditional failed is that it was interpreted as "c"
in (a == True) which is False.
the "==" operator binds at a higher precedence level than the "in"
operator, just as multiplication
binds higher than addition





More information about the Python-list mailing list