True of False - Python
This is a discussion on True of False - Python ; 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 ...
-
True of False
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?
Thanks.
Kou
-
Re: True of False
On Sep 27, 11:33 am, kou...@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?
> Thanks.
>
> Kou
,
You may want to include paren around ("c" in a) and a lower case p for
Print, i.e. print, and it should work
so eg:
a = ["a", "b", "c", "d", "e", "f"]
if ("c" in a) == True:
print "Yes"
-
Re: True of False
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?
Ciao,
Marc 'BlackJack' Rintsch
-
Re: True of False
On 9/27/07, koutoo@hotmail.com <koutoo@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>.
--
Cheers,
Simon B.
simon@brunningonline.net
-
Re: True of False
kou...@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?
> Thanks.
>
> Kou
Hello,
Just try :
a = ["a","b","c","d","e","f"]
if "c" in a:
print "yes"
That is going to work as the statement '"c" in a' itself is true. You
could try that by typing "c" in a at the interpreter.
regards,
Shriphani Palakodety
-
Re: True of False
On Sep 27, 12:48 pm, "Simon Brunning" <si...@brunningonline.net>
wrote:
> On 9/27/07, kou...@hotmail.com <kou...@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
-
Re: True of False
Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
> 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?
See http://docs.python.org/ref/comparisons.html
> Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
> to x < y and y <= z, except that y is evaluated only once (but in both
> cases z is not evaluated at all when x < y is found to be false).
In exactly the same way:
'c' in a == True
is equivalent to:
'c' in a and a == True
which is False.
-
Re: True of False
koutoo@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?
> Thanks.
You are unnecessarily adding a comparison with True. The correct way to
write that is
if "c" in a:
print "yes"
Bu of course you haven't actually told us what you really did, because
the code you represent has syntax errors.
>>> a = ["a", "b", "c", "d", "e", "f"]
>>> "c" in a
True
>>> if "c" in a == True:
.... print "found it"
....
>>> if ("c" in a) == True:
.... print "At last!"
....
At last!
>>>
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Sorry, the dog ate my .sigline
-
Re: True of False
On 27/09/2007, Casey <Caseyweb@gmail.com> wrote:
> On Sep 27, 12:48 pm, "Simon Brunning" <si...@brunningonline.net>
> wrote:
> > On 9/27/07, kou...@hotmail.com <kou...@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
>
-
Re: True of False
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
Similar Threads
-
By Application Development in forum RUBY
Replies: 10
Last Post: 03-29-2008, 02:24 PM
-
By Application Development in forum PHP
Replies: 5
Last Post: 12-13-2007, 12:23 PM
-
By Application Development in forum labview
Replies: 1
Last Post: 09-28-2007, 01:10 PM
-
By Application Development in forum labview
Replies: 0
Last Post: 09-28-2007, 12:40 PM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 05-08-2007, 06:39 PM