473,549 Members | 3,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

2
4220
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 Visual Basic.Net as a springboard for studying VBA. I use Office 2000 and would like to use VBA as a tool to customize it. I have zero...
9
1757
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: file.py: --------------------- import file2 def hello(): print "Hello" file2.hello2()
7
9268
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. # No warranty express or implied for the accuracy, fitness to purpose
54
3932
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 but *NOT* mystring (5th element). Many people don't like idea that 5th element is not invited. (BTW, yes I'm aware of the explanation where slicing
9
2229
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, FILE *in, FILE *out, FILE *err); It returns 0 on success or -1 on fail.
3
1405
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 following Web Method: public string HelloWorldDemo(string stuff) {
7
4458
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
8
2848
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 elements of VB.net....so that you were confident to write a application from scratch. This wouldnt necessarily
28
3552
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 business basic. I need to re-write it, bring it kicking and screaming to run on Windows XP. This is for a video rental place, tracks movie and game...
14
1832
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 logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start,...
0
7560
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7758
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7844
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6098
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.