473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1241
Mel
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...@gmail.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, "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).
"""
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, "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

Nov 26 '07 #7
On Nov 25, 6:49 pm, Donn Ingle <donn.in...@gmail.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_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.
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:
Hey, nifty! Thanks Paddy.

\d

Nov 26 '07 #10

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®
er**@myemma.com
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
Nov 26 '07 #11
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®
er**@myemma.com
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
Nov 26 '07 #12
On Nov 27, 3:01 am, Erik Jones <e...@myemma.comwrote:
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.
Nov 26 '07 #13

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

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
9
by: Frantisek Fuka | last post by:
This thing keeps bugging me. It's probably some basic misunderstanding on my part but I am stumped. Let's say I have two Python files: file.py and file2.py. Their contents is as follows: ...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
3
by: Jon Maz | last post by:
Hi All, I'm at the "Hello World" stage of learning about web services (just to warn you), and have what is I think a simple question. I have created a basic HelloWorld.asmx, which has the...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
8
by: Chris Asaipillai | last post by:
Hi there I have some questions for those experienced Visual Basic 6 programmers out there who have made the transition from VB6 to Vb.net. How long did it take you to learn at least the basic...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.