Determining whether a variable is less/greater than a range.

Scott David Daniels Scott.Daniels at Acm.Org
Mon Dec 8 14:55:30 EST 2008


simonharrison.uk at googlemail.com wrote:
> Hi. I'm having another go at learning Python so I'll probably be
> asking a few basic questions. Here is the first one.

I think nobody has pointed out to you that "a < b < c" has the
conventional mathematical definition (b is between a and c).

So, perhaps:
     b = 9
     c = 21
     print 'All inbounds', all(10 <= x <= 21 for x in (b, c))
     print 'None inbounds', all(not 10 <= x <= 21 for x in (b, c))

     print 'inbounds', [x for x in (b, c) if 10 <= x <= 21]
     print 'outabounds', [x for x in (b, c) if not 10 <= x <= 21]

I do advise working through the tutorial before asking.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list