[Baypiggies] Baypiggies snippets

Stephen McInerney spmcinerney at hotmail.com
Thu Mar 22 09:57:42 CET 2007

Monte, about this nested compare "a <= x <= b"

Q1: Since what version has it been in? I never knew about it!

Q2: Can anyone comment on the efficiency of
"a <= x <= b" vs "x in xrange(a,b)" (obviously the latter is worse, and only 
good for integer a,b)
In particular, is Python smart enough to binary-search the xrange object? 
Just curious.

Q3: C++ people would balk at the idiom "a <= x <= b"
since it does not evaluate right-to-left. (The two <= subexpressions
cannot be separately evaluated from the other. So it has to be
parsed and evaluated all in one)

Q4: are the idioms "a == x == b", "a != x != b" and "a is x is b" legal?


>From: Monte Davidoff <davidoff56 at alluvialsw.com>
># Here are expressions that are idiomatic in many languages to check
># that a number is within an interval:
>	x >= a and x <= b
>	a <= x and x <= b
># But in Python, you can also say:
>	a <= x <= b
>Another simple one worth knowing early on.

Watch free concerts with Pink, Rod Stewart, Oasis and more. Visit MSN 
Presents today. 

More information about the Baypiggies mailing list