# basic if stuff- testing ranges

John Machin sjmachin at lexicon.net
Sun Nov 25 20:50:17 CET 2007

```On Nov 26, 5:49 am, Donn Ingle <donn.in... at gmail.com> wrote:
> Sheesh, I've been going spare trying to find how to do this short-hand:
> if 0 > x < 20: print "within"

That means "if x LESS THAN 0 and x < 20".

>
> So that x must be > 0 and < 20.

So try
if 0 < x < 20:

>
> I usually do:
> if x > 0 and x < 20: print "within"
>
> What's the rule? Does it even exist?
> I read something like it recently on the list but can't find it, that's
> where I got the urge to try it from. I can't find anything in the docs, but
> then again (imho) the Python docs are like a tangled jungle...

Likely manuals: Tutorial & Reference
Tutorial: check contents, "if statement" looks possible, but no luck
Reference: check contents, "comparisons" looks possible, and
http://docs.python.org/ref/comparisons.html 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 false).
"""

```