True of False

Casey Caseyweb at gmail.com
Thu Sep 27 13:21:38 EDT 2007


On Sep 27, 1:12 pm, "Richard Thomas" <R.W.Thomas... at cantab.net> wrote:
> On 27/09/2007, Casey <Casey... at gmail.com> wrote:
>
>
>
> > 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
>
> Actually it evaluates '("c" in a) and (a == True)'. You can check like so:
>
> import dis
> a = list("abcdef")
> dis.dis(lambda: "c" in a == True)
>
> And just follow the bytecode operations.
>
> -- Richard.
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list

Doh, I forgot about operator chaining here.  I'm so used to just
seeing a < b < c that I forget about arbitrary operator chaining and
think like a C++ programmer!




More information about the Python-list mailing list