[Tutor] "x and y" means "if x is false, then x, else y"??

Adam Bark adam.jtm30 at gmail.com
Mon Jul 5 14:23:48 CEST 2010


On 5 July 2010 13:21, Adam Bark <adam.jtm30 at gmail.com> wrote:

> On 5 July 2010 12:53, Richard D. Moores <rdmoores at gmail.com> wrote:
>
>> On Mon, Jul 5, 2010 at 04:09, Stefan Behnel <stefan_ml at behnel.de> wrote:
>> > Richard D. Moores, 05.07.2010 11:37:
>> >>
>> >> I keep getting hung up over the meaning of "the return
>> >> value" of an expression. I am of course familiar with values returned
>> >> by a function, but don't quite grasp what the return value of, say,
>> >> the y of "x and y" might mean.
>> >
>> > Think of a different expression, like "1+1". Here, the return value (or
>> > maybe a better wording would be the result value) is 2.
>> >
>> >
>> >> Also, you distinguish between a return value of True and and the value
>> >> of y being such (say 5, and not 0) that it makes y true (but not
>> >> True). So another  thing I need to know is the difference between True
>> >> and true.  Also between False and false. And why the difference is
>> >> important.
>> >
>> > "True" is the value "True" in Python, which is a singleton. You can test
>> for
>> > it by using
>> >
>> >    x is True
>>
>> Ah. But could you give me an x that would satisfy that? I can think of
>>
>> >>> (5 > 4) is True
>> True
>>
>> But how can (5 > 4) be an x? Could you show me some code where it could
>> be?
>>
>> >>> x = (5 > 4)
>> >>> x
>> True
>> >>> x is True
>> True
>>
>> So it can! That surprised me.  I was expecting   "x = (5 > 4)"  to be
>> absurd -- raise an exception? Still seems pretty weird.
>>
>
> Greater than (>) works like the mathematical operators in returning a
> value, it just happens that for comparison operators (>, <, ==, !=) the
> values can only be True or False.
>
> HTH,
> Adam.
>

I should add that this is how something like:

if x != y:
    do_something()

works, if expects a True or False (this isn't always true but works for
comparison operators expressions such as this).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100705/efc1de6e/attachment.html>


More information about the Tutor mailing list