3>0 is True

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Sep 15 08:47:04 EDT 2010


Yingjie Lan writes:

> I am not sure how to interprete this, in the interactive mode:
> 
> >>> 3>0 is True
> False
> >>> (3>0) is True
> True
> >>> 3> (0 is True)
> True
> 
> Why did I get the first 'False'? I'm a little confused.

It is interpreted as equivalent to this:

    >>> 3 > 0 and 0 is True
    False

>From the language reference at python.org (section 5.9 Comparisons):

    expressions like a < b < c have the interpretation that is
    conventional in mathematics
    ...

    Comparisons can be chained arbitrarily, e.g., x < y <= z is
    equivalent to x < y and y <= z, except that y is evaluated only
    once (but in both cases z is not evaluated at all when x < y is
    found to be false).

<URL:http://docs.python.org/reference/expressions.html#notin>



More information about the Python-list mailing list