Boolean confusion

Discussion in 'Python' started by Greg Corradini, May 9, 2007.

  1. Hello all,
    I'm having trouble understanding why the following code evaluates as it
    does:

    >>> string.find('0200000914A','.') and len('0200000914A') > 10

    True
    >>> len('0200000914A') > 10 and string.find('0200000914A','.')

    -1

    In the 2.4 Python Reference Manual, I get the following explanation for the
    'and' operator in 5.10 Boolean operations:
    " The expression x and y first evaluates x; if x is false, its value is
    returned; otherwise, y is evaluated and the resulting value is returned."

    Based on what is said above, shouldn't my first expression (
    string.find('0200000914A','.') and len('0200000914A') > 10) evaluate to
    false b/c my 'x' is false? And shouldn't the second expression evaluate to
    True?

    Thanks for your help
    Greg

    --
    View this message in context: http://www.nabble.com/Boolean-confusion-tf3715438.html#a10393362
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    Greg Corradini, May 9, 2007
    #1
    1. Advertising

  2. Greg Corradini wrote:

    >
    > Hello all,
    > I'm having trouble understanding why the following code evaluates as it
    > does:
    >
    >>>> string.find('0200000914A','.') and len('0200000914A') > 10

    > True
    >>>> len('0200000914A') > 10 and string.find('0200000914A','.')

    > -1
    >
    > In the 2.4 Python Reference Manual, I get the following explanation for
    > the 'and' operator in 5.10 Boolean operations:
    > " The expression x and y first evaluates x; if x is false, its value is
    > returned; otherwise, y is evaluated and the resulting value is returned."
    >
    > Based on what is said above, shouldn't my first expression (
    > string.find('0200000914A','.') and len('0200000914A') > 10) evaluate to
    > false b/c my 'x' is false? And shouldn't the second expression evaluate to
    > True?


    The first evaluates to True because len(...) > 10 will return a boolean -
    which is True, and the semantics of the "and"-operator will return that
    value.

    And that precisely is the reason for the -1 in the second expression.

    y=-1

    and it's just returned by the and.

    in python, and is implemented like this (strict evaluation nonwithstanding):

    def and(x, y):
    if bool(x) == True:
    return y
    return x

    Diez
     
    Diez B. Roggisch, May 9, 2007
    #2
    1. Advertising

  3. On 2007-05-09, Greg Corradini <> wrote:
    >
    > Hello all,
    > I'm having trouble understanding why the following code evaluates as it
    > does:
    >
    >>>> string.find('0200000914A','.') and len('0200000914A') > 10

    > True
    >>>> len('0200000914A') > 10 and string.find('0200000914A','.')

    > -1
    >
    > In the 2.4 Python Reference Manual, I get the following explanation for the
    > 'and' operator in 5.10 Boolean operations:
    > " The expression x and y first evaluates x; if x is false, its value is
    > returned; otherwise, y is evaluated and the resulting value is returned."
    >
    > Based on what is said above, shouldn't my first expression (
    > string.find('0200000914A','.') and len('0200000914A') > 10) evaluate to
    > false b/c my 'x' is false? And shouldn't the second expression evaluate to
    > True?


    The find method doesn't return a boolean, but returns the index where
    the substring was found with -1 indicating it wasn't found. If you just
    want to check wether one string is a substring of an other, use the in
    operator.

    >>> '.' in '0200000914A' and len('0200000914A') > 10

    False
    >>> len('0200000914A') > 10 and '.' in '0200000914A'

    False

    --
    Antoon Pardon
     
    Antoon Pardon, May 9, 2007
    #3
  4. Thank you Diez and Antoon for demystifing this problem. I see where I've been
    going wrong.

    Diez B. Roggisch-2 wrote:
    >
    > Greg Corradini wrote:
    >
    >>
    >> Hello all,
    >> I'm having trouble understanding why the following code evaluates as it
    >> does:
    >>
    >>>>> string.find('0200000914A','.') and len('0200000914A') > 10

    >> True
    >>>>> len('0200000914A') > 10 and string.find('0200000914A','.')

    >> -1
    >>
    >> In the 2.4 Python Reference Manual, I get the following explanation for
    >> the 'and' operator in 5.10 Boolean operations:
    >> " The expression x and y first evaluates x; if x is false, its value is
    >> returned; otherwise, y is evaluated and the resulting value is returned."
    >>
    >> Based on what is said above, shouldn't my first expression (
    >> string.find('0200000914A','.') and len('0200000914A') > 10) evaluate to
    >> false b/c my 'x' is false? And shouldn't the second expression evaluate
    >> to
    >> True?

    >
    > The first evaluates to True because len(...) > 10 will return a boolean -
    > which is True, and the semantics of the "and"-operator will return that
    > value.
    >
    > And that precisely is the reason for the -1 in the second expression.
    >
    > y=-1
    >
    > and it's just returned by the and.
    >
    > in python, and is implemented like this (strict evaluation
    > nonwithstanding):
    >
    > def and(x, y):
    > if bool(x) == True:
    > return y
    > return x
    >
    > Diez
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >


    --
    View this message in context: http://www.nabble.com/Boolean-confusion-tf3715438.html#a10393705
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    Greg Corradini, May 9, 2007
    #4
  5. On 2007-05-09, Greg Corradini <> wrote:
    >
    > Hello all,
    > I'm having trouble understanding why the following code evaluates as it
    > does:
    >
    >>>> string.find('0200000914A','.') and len('0200000914A') > 10

    > True
    >>>> len('0200000914A') > 10 and string.find('0200000914A','.')

    > -1
    >
    > In the 2.4 Python Reference Manual, I get the following explanation for
    > the
    > 'and' operator in 5.10 Boolean operations:
    > " The expression x and y first evaluates x; if x is false, its value is
    > returned; otherwise, y is evaluated and the resulting value is returned."
    >
    > Based on what is said above, shouldn't my first expression (
    > string.find('0200000914A','.') and len('0200000914A') > 10) evaluate to
    > false b/c my 'x' is false? And shouldn't the second expression evaluate to
    > True?


    >The find method doesn't return a boolean, but returns the index where
    >the substring was found with -1 indicating it wasn't found. If you just
    >want to check wether one string is a substring of an other, use the in
    >operator.


    >>> '.' in '0200000914A' and len('0200000914A') > 10

    False
    >>> len('0200000914A') > 10 and '.' in '0200000914A'

    False

    Thank you Diez and Antoon for demystifing this problem. I see where I've
    been going wrong.
    --
    View this message in context: http://www.nabble.com/Boolean-confusion-tf3715438.html#a10393765
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    Greg Corradini, May 9, 2007
    #5
    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. Frantisek Fuka

    Boolean confusion

    Frantisek Fuka, Oct 31, 2003, in forum: Python
    Replies:
    7
    Views:
    328
    John Roth
    Oct 31, 2003
  2. Batista, Facundo

    RE: Boolean confusion

    Batista, Facundo, Oct 31, 2003, in forum: Python
    Replies:
    0
    Views:
    360
    Batista, Facundo
    Oct 31, 2003
  3. J Leonard
    Replies:
    4
    Views:
    12,693
    Mark Space
    Jan 19, 2008
  4. Kedar Mhaswade

    Confusion regarding boolean operators.

    Kedar Mhaswade, Jan 9, 2011, in forum: Ruby
    Replies:
    3
    Views:
    108
    Kedar Mhaswade
    Jan 11, 2011
  5. Metre Meter
    Replies:
    7
    Views:
    379
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page