Confusion about x < y <= z in python -
this question has answer here:
- why expression 0 < 0 == 0 return false in python? 9 answers
- is `a<b<c` valid python? 3 answers
i'm newbie in python. have 3 variables x
, y
, z
int
. have comparison 3 variables in if
condition. i'm confused following code result.
the expression x < y <= z
evaluates false.
let's assume x = 10
, y = 5
, z = 0
. if x < y
become false, false <= 0
become true. but output false. why?
my python script:
#!/usr/bin/python x = 10 y = 5 z = 0 if (x < y < z): print"true" else: print"false"
the document say:
comparisons can chained arbitrarily; example, x < y <= z equivalent x < y , y <= z, except y evaluated once (but in both cases z not evaluated @ when x < y found false).
x < y <= z
neither means (x < y) <= z
nor x < (y <= z)
. x < y <= z
equivalent x < y , y <= z
, , evaluates left-to-right.
logical and not have associativity in python unlike c , c++. there separate rules sequences of kind of operator , cannot expressed associativity.
x < y , y <= z
evaluates second argument if first 1 true because and
short-circuit operator.
Comments
Post a Comment