Function to take the minimum of 3 numbers

Steve D'Aprano steve+python at pearwood.info
Mon Oct 10 10:11:42 EDT 2016


On Mon, 10 Oct 2016 10:56 am, Ian Kelly wrote:

> On Oct 9, 2016 2:57 PM, <breamoreboy at gmail.com> alleged:

> The Pythonic way
> 
> if b >= a <= c:
>     ...

I doubt that would be considered Pythonic by many people. Chained
comparisons are Pythonic, but not legal but weird combinations like 
`a == b < c is not d >= e <= f`.


> Better:
> 
> if a <= b <= c:
>     ...

That's more like it. Unfortunately it doesn't mean the same as Mark's
version:

b >= a <= c means b >= a and a <= c
which is True for a = 1, b = 3 and c = 2;


a <= b <= c means a <= b and b <= c
which is False for a = 1, b = 3 and c = 2.


> Using consistent operators is not required but is easier to read and less
> confusing.

Indeed.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list