Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Boolean confusion

Reply
Thread Tools

Boolean confusion

 
 
Greg Corradini
Guest
Posts: n/a
 
      05-09-2007

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-confus...html#a10393362
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-09-2007
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
 
Reply With Quote
 
 
 
 
Antoon Pardon
Guest
Posts: n/a
 
      05-09-2007
On 2007-05-09, Greg Corradini <(E-Mail Removed)> 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
 
Reply With Quote
 
Greg Corradini
Guest
Posts: n/a
 
      05-09-2007

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-confus...html#a10393705
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
Greg Corradini
Guest
Posts: n/a
 
      05-09-2007



On 2007-05-09, Greg Corradini <(E-Mail Removed)> 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-confus...html#a10393765
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion regarding boolean operators. Kedar Mhaswade Ruby 3 01-11-2011 11:09 PM
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
Boolean confusion Frantisek Fuka Python 7 10-31-2003 10:10 PM
RE: Boolean confusion Batista, Facundo Python 0 10-31-2003 07:38 PM



Advertisments