True of False
Carsten Haese
carsten at uniqsys.com
Thu Sep 27 13:14:13 EDT 2007
On Thu, 2007-09-27 at 16:47 +0000, Marc 'BlackJack' Rintsch wrote:
> On Thu, 27 Sep 2007 09:33:34 -0700, koutoo 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?
>
> Wow that's odd:
>
> In [265]: a = list('abcdef')
>
> In [266]: a
> Out[266]: ['a', 'b', 'c', 'd', 'e', 'f']
>
> In [267]: 'c' in a
> Out[267]: True
>
> In [268]: 'c' in a == True
> Out[268]: False
>
> In [269]: ('c' in a) == True
> Out[269]: True
>
> In [270]: 'c' in (a == True)
> ---------------------------------------------------------------------------
> <type 'exceptions.TypeError'> Traceback (most recent call last)
>
> /home/bj/<ipython console> in <module>()
>
> <type 'exceptions.TypeError'>: argument of type 'bool' is not iterable
>
>
> What's going on there?
What's going on here is that both 'in' and '==' are comparison
operations, and Python allows you to chain comparisons. Just like "a < x
< b" is evaluated as "a < x and x < b", "'c' in a == True" is evaluated
as "'c' in a and a == True". Obviously, since a==True is false, the
chained comparison is False.
--
Carsten Haese
http://informixdb.sourceforge.net
More information about the Python-list
mailing list