473,549 Members | 3,012 Online

basic if stuff- testing ranges

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

Nov 25 '07 #1
12 1249
Donn Ingle 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?
if 0 < x < 20:
?
Mel.
Nov 25 '07 #2
Donn Ingle a écrit :
Sheesh, I've been going spare trying to find how to do this short-hand:
if 0 x < 20: print "within"
you mean : 0 < x < 20 ?

or

x in xrange(1,20) ?
>
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?
is that hard to check it at the repl ?
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...
<something smug, refrained/>
>

\d
Nov 25 '07 #3
On Nov 26, 5:49 am, Donn Ingle <donn.in...@gma il.comwrote:
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, "comparison s" 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).
"""
Nov 25 '07 #4
you mean : 0 < x < 20 ?
Yes. I had gotten the impression that there was some Python form of:
if NUMBER test VAR test NUMBER:

Part of the question was to discover if I was smoking my socks :)
x in range(1,20) ?
Sure, that's okay, but it has clarity issues, and is calling a func.
>but then again (imho) the Python docs are like a tangled jungle...
<something smug, refrained/>
Well, I said it was MHO and if it was easier (for me) to find answers in the
docs I'd have an easier time of it.

\d

Nov 26 '07 #5
Mel wrote:
if 0 < x < 20:
?
I take it I was tripping then. That's okay, it seemed a little too weird
anyway :)

\d

Nov 26 '07 #6
>if 0 x < 20: print "within"
That means "if x LESS THAN 0 and x < 20".
Oh, bugger. It's tricky.
So try
if 0 < x < 20:
Thanks. I was flipping signs in my tests, but I guess I flipped both and got
myself all confused.
Likely manuals: Tutorial & Reference
Tutorial: check contents, "if statement" looks possible, but no luck
Yes, I got that far.
Reference: check contents, "comparison s" looks possible, and
Thanks again. I find the reference is laid-out in a way that I don't find
intuitive and every time I look for something I fail. I even grep through
the folder to get a clue, which shows how poor the index is (to me)!

Many thanks for the help!
\d

Nov 26 '07 #7
On Nov 25, 6:49 pm, Donn Ingle <donn.in...@gma il.comwrote:
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_compari sons.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)
Nov 26 '07 #8
Donn Ingle wrote:
>x in range(1,20) ?
Sure, that's okay, but it has clarity issues, and is calling a func.
and it requires that x is integral (1.0 is in the range, 1.001 is not),
and becomes dog slow when the range gets larger. Not a good idea.

Peter
Nov 26 '07 #9
The output of the following program might help:

\d

Nov 26 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.