[Tutor] help with and

Steven D'Aprano steve at pearwood.info
Wed Mar 1 06:07:34 EST 2017


On Wed, Mar 01, 2017 at 09:29:04AM +0000, Alan Gauld via Tutor wrote:

> You need to be explicit:
> 
>    if hours_worked > 40 and hours_worked < 60:
> 
> You can also write what you intend in a slightly
> different form:
> 
>    if (40 < hours_worked < 60):
> 
> Note:
> The second form an unusual style that I've only
> ever seen in Python, most languages insist you
> use the first version.

That sort of chained comparison is standard mathematics notation which 
is hopefully familiar to anyone who has done maths in secondary school. 
I consider it a serious weakness of other languages that they don't 
support chained comparisons.

Perl6, Coffeescript and Julia are three modern languages which support 
chained comparisons:

https://en.wikipedia.org/wiki/Perl_6#Chained_comparisons

http://coffeescript.org/#comparisons

http://docs.julialang.org/en/latest/manual/mathematical-operations.html#Chaining-comparisons-1



-- 
Steve


More information about the Tutor mailing list