[Tutor] Range of float value

Kent Johnson kent37 at tds.net
Thu Feb 8 17:41:40 CET 2007

Luke Paireepinart wrote:
> Kent Johnson wrote:

>> You can't generate all the float values in a range. (OK, you probably 
>> could, but it would not be practical or useful.) You can test for a 
>> value in a range, e.g.
>> if 48.35 <= a <= 48.45:
> Kent:
> Why does this work?

It is explicitly supported in Python. See

which says, "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 


> In C++ this would go from
> if (48.35 <= a <= 48.45)
> to (assuming the right condition is met)
> if (48.35 <= true)
> because it evaluates these things right to left, doesn't it?
> does python treat chained comparisons differently than C++ or is C++ 
> behaving differently than I think it does?
> Thanks for your help,
> -Luke

More information about the Tutor mailing list