Wednesday, 15 August 2012

python - How does interval comparison work? -



python - How does interval comparison work? -

somehow, works:

def in_range(min, test, max): homecoming min <= test <= max print in_range(0, 5, 10) # true print in_range(0, 15, 10) # false

however, can't quite figure out order of operations here. let's test false case:

print 0 <= 15 <= 10 # false print (0 <= 15) <= 10 # true print 0 <= (15 <= 10) # true

clearly, isn't resolving simple order of operations issue. interval comparing special operator, or else going on?

unlike languages, python supports chained comparing operators , evaluates them evaluated in normal mathematics.

this line:

return min <= test <= max

is evaluated python this:

return (min <= test) , (test <= max)

most other languages evaluate this:

return (min <= test) <= max

python syntax comparison

No comments:

Post a Comment