basic if stuff- testing ranges

Paddy paddy3118 at googlemail.com
Mon Nov 26 02:33:16 EST 2007


On Nov 25, 6:49 pm, 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"
>
> So that x must be > 0 and < 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...
>
> \d

The output of the following program might help:

# chained_comparisons.py
complist = '< <= == != >= >'.split()
for lhs in complist:
  for rhs in complist:
    print "\n1 %2s  x  %2s 3:" % (lhs, rhs)
    for x in range(5):
      chain = "  1 %2s  %i  %2s 3" % (lhs, x, rhs)
      print chain," is ", eval(chain)


- Paddy.



More information about the Python-list mailing list