Boolean confusion

G

Greg Corradini

Hello all,
I'm having trouble understanding why the following code evaluates as it
does:
-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
 
D

Diez B. Roggisch

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

-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
 
A

Antoon Pardon

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

-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.
False
 
G

Greg Corradini

Thank you Diez and Antoon for demystifing this problem. I see where I've been
going wrong.
 
G

Greg Corradini

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

-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.
False

Thank you Diez and Antoon for demystifing this problem. I see where I've
been going wrong.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top