[Tutor] trouble with function-- trying to check
Isaac
hyperneato at gmail.com
Wed Mar 14 17:19:25 CET 2007
Howdy,
> This has already been discussed on this thread. == and 'in' are
comparisons.
I did not get that thread somehow. I thought I looked for the original
thread but maybe I did not go back far enough. This thread is great for me -
I am learning much.
http://docs.python.org/ref/comparisons.html
says
" The operators <, >, ==, >=, <=, and != compare the values of two objects.
The objects need not have the same type. If both are numbers, they are
converted to a common type. Otherwise, objects of different types
*always*compare unequal,"
later on:
" The operators in and not in test for set membership"
>(c == c in 'crab')
>means
>(c == c) and (c in 'crab')
(c == c in 'crab') returns a true/false.
The statement (c == c), and the statement (c in 'crab') also returns
true/false,
but the [ and ] operator that joins (c == c) and (c in 'crab') returns a
value based on the values that precede and follow the [ and ] operator:
" 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."
http://docs.python.org/ref/Booleans.html
[code]
python >c = 'c'
python >d = 'c'
python >c == d in 'crab'
True
python >(c == d) and 'crab'
'crab'
[/code]
But if I put parentheses around one term like this:
> (c == (c in "crab"))
c in 'crab' returns a true/false first because parentheses are more binding
than comparisons. Then the outer parentheses is evaluated- c in this case is
a <type 'str'> and it compares this value/type to the statement returned by
the innermost set which previously returned a <type 'bool'>
cheers
-Isaac
Kent wrote:
This has already been discussed on this thread. == and 'in' are comparisons.
(c == c in 'crab')
means
(c == c) and (c in 'crab')
http://docs.python.org/ref/comparisons.html
Kent
Re: [Tutor] trouble with function-- trying to check
To: Terry Carroll <carroll at tjc.com>
Cc: tutor at python.org
Message-ID: <45F7D0C5.3020107 at tds.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Terry Carroll wrote:
> On Wed, 14 Mar 2007, Isaac wrote:
>
>> a, b, c, or d is a type('str') not boolean which is what (c in "crab")
is.
>> The [in] operator takes presedence, the first 3 times (c in "crab")
returns
>> true and the last returns false; but the strings a, b, c, or d do not ==
>> true or false - therefore the test (c == (c in "crab")) always returns
>> false.
>
> The thing is, if the in operator takes precedence than why do
>
>
> (c == c in "crab")
>
> and
>
> (c == (c in "crab"))
>
> return different results?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070314/65bc0d0a/attachment.html
More information about the Tutor
mailing list