basic if stuff- testing ranges

Discussion in 'Python' started by Donn Ingle, Nov 25, 2007.

  1. Donn Ingle

    Donn Ingle Guest

    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
    Donn Ingle, Nov 25, 2007
    #1
    1. Advertising

  2. Donn Ingle

    Mel Guest

    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.
    Mel, Nov 25, 2007
    #2
    1. Advertising

  3. 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
    >
    Aurélien Campéas, Nov 25, 2007
    #3
  4. Donn Ingle

    John Machin Guest

    On Nov 26, 5:49 am, Donn Ingle <> 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).
    """
    John Machin, Nov 25, 2007
    #4
  5. Donn Ingle

    Donn Ingle Guest

    > 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
    Donn Ingle, Nov 26, 2007
    #5
  6. Donn Ingle

    Donn Ingle Guest

    Mel wrote:
    > if 0 < x < 20:
    > ?

    I take it I was tripping then. That's okay, it seemed a little too weird
    anyway :)

    \d
    Donn Ingle, Nov 26, 2007
    #6
  7. Donn Ingle

    Donn Ingle Guest

    >> 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, "comparisons" 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
    Donn Ingle, Nov 26, 2007
    #7
  8. Donn Ingle

    Paddy Guest

    On Nov 25, 6:49 pm, 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?
    > 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.
    Paddy, Nov 26, 2007
    #8
  9. Donn Ingle

    Peter Otten Guest

    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
    Peter Otten, Nov 26, 2007
    #9
  10. Donn Ingle

    Donn Ingle Guest

    > The output of the following program might help:
    Hey, nifty! Thanks Paddy.

    \d
    Donn Ingle, Nov 26, 2007
    #10
  11. Donn Ingle

    Erik Jones Guest

    On Nov 25, 2007, at 9:49 PM, Donn Ingle wrote:

    >>> 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, "comparisons" 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)!


    Then use one of the quick references here: http://rgruet.free.fr/.

    Erik Jones

    Software Developer | Emma®

    800.595.4401 or 615.292.5888
    615.292.0777 (fax)

    Emma helps organizations everywhere communicate & market in style.
    Visit us online at http://www.myemma.com
    Erik Jones, Nov 26, 2007
    #11
  12. Donn Ingle

    Erik Jones Guest

    On Nov 26, 2007, at 2:29 AM, Peter Otten wrote:

    > 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.


    That is because range() is not a range in the abstract sense (i.e.
    simply defining bounds that can be tested for set membership) but are
    used to create lists (or, in the case of xrange(), successive values)
    between the bounds given in the params. So, saying x in range(1,20)
    is not asking if x is between 1 and 20 but, rather, if x is a member
    of the values genereated by the range function with params 1 and 20.
    So, yes, using range()

    Erik Jones

    Software Developer | Emma®

    800.595.4401 or 615.292.5888
    615.292.0777 (fax)

    Emma helps organizations everywhere communicate & market in style.
    Visit us online at http://www.myemma.com
    Erik Jones, Nov 26, 2007
    #12
  13. Donn Ingle

    John Machin Guest

    On Nov 27, 3:01 am, Erik Jones <> wrote:
    > On Nov 25, 2007, at 9:49 PM, Donn Ingle wrote:
    >
    >
    >
    > >>> 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, "comparisons" 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)!

    >
    > Then use one of the quick references here:http://rgruet.free.fr/.
    >


    Generally excellent references, but "X < Y < Z < W has expected
    meaning, unlike C" is not much help to people who have not been
    exposed to similar notation (e.g. 0 <= angle < 2 * pi) in other
    disciplines and/or know C merely as a member of the same set as X, Y,
    Z and W.
    John Machin, Nov 26, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sparko

    Basic stuff

    Sparko, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    639
    Hans Kesting
    Apr 20, 2005
  2. Tom
    Replies:
    3
    Views:
    293
    Rolf Magnus
    Dec 14, 2003
  3. wink.co.nr

    Basic Stuff

    wink.co.nr, May 16, 2007, in forum: C++
    Replies:
    4
    Views:
    292
  4. richard

    Re: Basic array stuff

    richard, Oct 5, 2008, in forum: HTML
    Replies:
    1
    Views:
    340
    Dr J R Stockton
    Oct 5, 2008
  5. Thomas 'PointedEars' Lahn

    Re: Basic array stuff

    Thomas 'PointedEars' Lahn, Oct 6, 2008, in forum: HTML
    Replies:
    0
    Views:
    364
    Thomas 'PointedEars' Lahn
    Oct 6, 2008
Loading...

Share This Page