True of False

Discussion in 'Python' started by koutoo@hotmail.com, Sep 27, 2007.

  1. Guest

    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
     
    , Sep 27, 2007
    #1
    1. Advertising

  2. Guest

    On Sep 27, 11:33 am, 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"
     
    , Sep 27, 2007
    #2
    1. Advertising

  3. 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
     
    Marc 'BlackJack' Rintsch, Sep 27, 2007
    #3
  4. On 9/27/07, <> 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 Brunning, Sep 27, 2007
    #4
  5. Shriphani Guest

    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
     
    Shriphani, Sep 27, 2007
    #5
  6. Casey Guest

    On Sep 27, 12:48 pm, "Simon Brunning" <>
    wrote:
    > On 9/27/07, <> 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
     
    Casey, Sep 27, 2007
    #6
  7. Duncan Booth Guest

    Marc 'BlackJack' Rintsch <> 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.
     
    Duncan Booth, Sep 27, 2007
    #7
  8. Steve Holden Guest

    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
     
    Steve Holden, Sep 27, 2007
    #8
  9. On 27/09/2007, Casey <> wrote:
    > On Sep 27, 12:48 pm, "Simon Brunning" <>
    > wrote:
    > > On 9/27/07, <> 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
    >
     
    Richard Thomas, Sep 27, 2007
    #9
  10. 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
     
    Carsten Haese, Sep 27, 2007
    #10
  11. Casey Guest

    On Sep 27, 1:12 pm, "Richard Thomas" <> wrote:
    > On 27/09/2007, Casey <> wrote:
    >
    >
    >
    > > On Sep 27, 12:48 pm, "Simon Brunning" <>
    > > wrote:
    > > > On 9/27/07, <> 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!
     
    Casey, Sep 27, 2007
    #11
  12. Erik Jones Guest

    On Sep 27, 2007, at 11:47 AM, 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?


    That is weird. Given 270, what's happening in 268.

    Erik Jones

    Software Developer | Emma®

    800.595.4401 or 615.292.5888
    615.292.0777 (fax)

    Emma helps organizations everywhere communicate & market in style.
    Visit us online at http://www.myemma.com
     
    Erik Jones, Sep 27, 2007
    #12
  13. Gary Herron Guest

    Richard Thomas wrote:
    > On 27/09/2007, Casey <> wrote:
    >
    >> On Sep 27, 12:48 pm, "Simon Brunning" <>
    >> wrote:
    >>
    >>> On 9/27/07, <> 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.
    >

    Yikes. So I did that and you're correct. I've always looked at
    chained comparisons with mild suspicion. Now I guess that suspicion is
    justified. Interpreting
    a<b<c
    as
    a<b and b<c
    make perfect sense to me, but interpreting
    "c" in a == True
    as
    ("c" in a) and (a == True)
    is not at all natural.

    Gary Herron

    > -- Richard.
    >
    >
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>
    >>
     
    Gary Herron, Sep 27, 2007
    #13
  14. Erik Jones Guest

    On Sep 27, 2007, at 12:29 PM, Erik Jones wrote:

    >
    > On Sep 27, 2007, at 11:47 AM, 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?

    >
    > That is weird. Given 270, what's happening in 268.
    >
    > Erik Jones


    Cool, Richard Thomas answered this one for me.

    Erik Jones

    Software Developer | Emma®

    800.595.4401 or 615.292.5888
    615.292.0777 (fax)

    Emma helps organizations everywhere communicate & market in style.
    Visit us online at http://www.myemma.com
     
    Erik Jones, Sep 27, 2007
    #14
  15. Casey <> wrote:
    > 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.


    That was my first thought, too. But watch:
    >>> a = list("abcde")
    >>> a

    ['a', 'b', 'c', 'd', 'e']
    >>> "c" in (a == True)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: argument of type 'bool' is not iterable

    Then it dawned on me (is this the right phrase?): It's the same situation
    as e.g. x < y >= 1, which means the same as "x < y and y >= 1" (except
    that y is only evaluated once). So '"c" in a == True' gets evaluated as
    '"c" in a and a == True'.

    > the "==" operator binds at a higher precedence level than the "in"
    > operator, just as multiplication binds higher than addition


    Operator precedence plays no role in this case. It is a case of
    'chained' comparisons.

    Hope that clears it up
    Marc
     
    Marc Christiansen, Sep 27, 2007
    #15
  16. On Thu, 27 Sep 2007 17:06:30 +0000, Duncan Booth wrote:

    > Marc 'BlackJack' Rintsch <> 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.


    Aaah *enlightenment*, I'm using this for range checks like in the docs,
    but it wasn't obvious to me in this case. Thanks.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 27, 2007
    #16
  17. a écrit :
    > 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?


    See other answers for the details. Anyway, since '"c" in a' is already a
    boolean expression, testing the result of the evaluation of this
    expression against a boolean is a pure waste of time. The usual idiom -
    and this is definitively not Python-specific - is:

    if "c" in a:
    print "Yes"

    Also, in Python (and in some other languages too), there's a notion of
    "something" vs "nothing" - where, in a boolean context, "something"
    evals to true and "nothing" to false. wrt/ Python, the empty string, an
    empty container (list, tuple, dict, set etc), numerical zero's (int or
    float), and of course the None object all eval to false, and most other
    objects eval to true.
     
    Bruno Desthuilliers, Sep 27, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    669
    Chris Theis
    Jun 18, 2004
  2. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,035
    Grant Edwards
    Dec 16, 2005
  3. André
    Replies:
    3
    Views:
    1,597
  4. bdb112
    Replies:
    45
    Views:
    1,350
    jazbees
    Apr 29, 2009
  5. Shea Martin

    false or true == true .... WTF?

    Shea Martin, Apr 5, 2007, in forum: Ruby
    Replies:
    4
    Views:
    108
    Bertram Scharpf
    Apr 5, 2007
Loading...

Share This Page