Check if a given value is out of certain range

Laura Creighton lac at openend.se
Thu Oct 1 03:40:58 EDT 2015


In a message of Wed, 30 Sep 2015 14:46:48 -0600, Ian Kelly writes:
>Thought mirrors language. In English, we typically would say "x is not
>between 0 and 10", not "x is either less than 0 or greater than 10".

I wonder if that is regional.

I think you may have stacked things with the 'either' and the 'greater
than'.  'Either' doesn't get used much unless you are heading for
'either x or y but not both'.  And everybody says 'more' even when
'greater than' is arguably more precise.

Around here, I would expect
"x is less than 0 or more than 10"

more often than either of your proposed forms.

But yesterday I hit up the students with the 'what do you find
more readable' question.

There was broad consensus that:

if 0 < x < 10 :
   do_something()
else:
   do_something_else()

was plenty more readable than anything using 'not'

so that the class favourite for readability was:

if 0 < x < 10:
   pass
else:
  complain_out_of_range()

though refactoring the whole lot so that you could have an
out_of_range Exception instead of an if had a large number of
supporters.

Laura





More information about the Python-list mailing list