3>0 is True

Y

Yingjie Lan

Hi,

I am not sure how to interprete this, in the interactive mode:
True

Why did I get the first 'False'? I'm a little confused.

Thanks in advance for anybody who shed some light on this.

YL
 
P

Peter Otten

Yingjie said:
I am not sure how to interprete this, in the interactive mode:

True

Why did I get the first 'False'? I'm a little confused.

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

"""
Unlike C, all comparison operations in Python have the same priority, which
is lower than that of any arithmetic, shifting or bitwise operation. Also
unlike C, expressions like a < b < c have the interpretation that is
conventional in mathematics:

comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"

Comparisons yield boolean values: True or False.
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).
"""

Peter
 
J

Jussi Piitulainen

Yingjie said:
I am not sure how to interprete this, in the interactive mode:

True

Why did I get the first 'False'? I'm a little confused.

It is interpreted as equivalent to this:
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>
 
M

Mel

Yingjie said:
I am not sure how to interprete this, in the interactive mode:

True

Why did I get the first 'False'? I'm a little confused.

Thanks in advance for anybody who shed some light on this.

This looks like comparison chaining. `is` is a comparison operator, like
`>`, and chains of comparisons are handled differently.

`a < b < c` is equivalent to `(a < b) and (b < c)`

Therefore the first expression is testing

(3 > 0) and (0 is True)


Mel.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top