473,785 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python IF THEN chain equivalence

I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
Nov 13 '08 #1
15 3238
jzakiya schrieb:
I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
Of course not. After "do a", it would stop.

You need to use

if x1 < limit:
do a
if x2 < limit:
do b
...
Alternatively, and depending on the nature of "do X", you can do

for x, todo in ((x1, do_a), (x2, do_b), ...):
if x < limit:
todo()
else:
break

This implies though that the "dos" are pure functions without (variable)
arguments.

Diez
Nov 13 '08 #2
I think you should rethink your post. The first case you posted makes no sense in any language I know. Also, a whole lot of nested IF's is a bad idea in any language. In Python, you will end up with code indented 40+ characters if you keep going.

----- Original Message ----
From: jzakiya <jz*****@mail.c om>
To: py*********@pyt hon.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence

I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
--
http://mail.python.org/mailman/listinfo/python-list

_______________ _______________ _______________ _______________ ______
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com
Nov 13 '08 #3
On Nov 13, 5:21*pm, Alan Baljeu <alanbal...@yah oo.comwrote:
I think you should rethink your post. The first case you posted makes no sense in any language I know. *Also, a whole lot of nested IF's is a bad idea in any language. *In Python, you will end up with code indented 40+ characters if you keep going.

----- Original Message ----
From: jzakiya <jzak...@mail.c om>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence

I'm translating a program in Python that has this IF Then chain

IF *x1 < limit: * --- do a ---
* * IF *x2 < limit: *--- do b ---
* * * * IF x3 < limit: *--- do c ---
* * * * * * * * * * * *.-----
* * * * * * * * * * * * ------
* * * * * * * * * * IF *x10 < limt: --- do j ---
* * * * * * * * * * THEN
* * * * * * * * *THEN
* * * * * * * -----
* * * * * THEN
* * *THEN
THEN

In other words, as long as * *'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF *x1 < limit:
* * * * --- do a *---
elif x2 < limit:
* * * * --- do b ---
----
----
elif x10 < limit:
* * * *--- do j ---

--http://mail.python.org/mailman/listinfo/python-list

* * * _______________ _______________ _______________ _______________ ______
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yaho o.com

In the code the 'xi's and 'limit' are variables and the --- do letters
---
phrases are simply writes to any array: an_array[xi]=0

Actually, the code makes perfectly good sense, and is a necessity of
the algorithm I'm implementing, and works perfectly good in Forth, and
can be
written quite nicely within a normal page width.

I was just hoping I could perform the equivalent chain in Python
without
having to grossly indent the source code past the normal width of a
printed page.
But if that's the only way to do it in Python, then so be it.
Nov 13 '08 #4
On 2008-11-13, jzakiya <jz*****@mail.c omwrote:
I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN
The placement of the THEN statements makes absolutely no sense
in any language I've ever seen.
In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
No. That's not the same at all.
Here's one solution:

while True:
if x1 limit: break
do a
if x2 limit: break
do b
if x3 limit: break
do c
...
if x10 limit: break
do j
break

--
Grant Edwards grante Yow! Eisenhower!! Your
at mimeograph machine upsets
visi.com my stomach!!
Nov 13 '08 #5
On 2008-11-13, Grant Edwards <invalid@invali dwrote:
On 2008-11-13, jzakiya <jz*****@mail.c omwrote:
>I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

The placement of the THEN statements makes absolutely no sense
in any language I've ever seen.
>In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---

No. That's not the same at all.
Here's one solution:

while True:
if x1 limit: break
do a
if x2 limit: break
do b
if x3 limit: break
do c
...
if x10 limit: break
do j
break
Oops. I botched the case where xN == limit. Each of the tests
should be xN >= limit.

--
Grant Edwards grante Yow! ... I see TOILET
at SEATS ...
visi.com
Nov 13 '08 #6
On 2008-11-13 23:31, jzakiya wrote:
On Nov 13, 5:21 pm, Alan Baljeu <alanbal...@yah oo.comwrote:
>I think you should rethink your post. The first case you posted makes no sense in any language I know. Also, a whole lot of nested IF's is a bad idea in any language. In Python, you will end up with code indented 40+ characters if you keep going.

----- Original Message ----
From: jzakiya <jzak...@mail.c om>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence

I'm translating a program in Python that has this IF Then chain

IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---

--http://mail.python.org/mailman/listinfo/python-list

_______________ _______________ _______________ _______________ ______
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yaho o.com


In the code the 'xi's and 'limit' are variables and the --- do letters
---
phrases are simply writes to any array: an_array[xi]=0

Actually, the code makes perfectly good sense, and is a necessity of
the algorithm I'm implementing, and works perfectly good in Forth, and
can be
written quite nicely within a normal page width.

I was just hoping I could perform the equivalent chain in Python
without
having to grossly indent the source code past the normal width of a
printed page.
But if that's the only way to do it in Python, then so be it.
You should probably consider using a function and then
convert the conditions to define return points:

def do_something(.. .args...):

if x1 >= limit:
return
...do a...
if x2 >= limit:
return
...do b...
etc.

That is provided I understand the flow of control in your
example... it's kind of strange to have THEN mark the *end*
of the then-branch in an if-then-else construct.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Nov 13 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.D atabase.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
_______________ _______________ _______________ _______________ ____________
2008-11-12: Released mxODBC Connect 0.9.3 http://python.egenix.com/

:::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Nov 13 '08 #7
On Nov 13, 5:48*pm, "M.-A. Lemburg" <m...@egenix.co mwrote:
On 2008-11-13 23:31, jzakiya wrote:
On Nov 13, 5:21 pm, Alan Baljeu <alanbal...@yah oo.comwrote:
I think you should rethink your post. The first case you posted makes no sense in any language I know. *Also, a whole lot of nested IF's is a bad idea in any language. *In Python, you will end up with code indented 40+ characters if you keep going.
----- Original Message ----
From: jzakiya <jzak...@mail.c om>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence
I'm translating a program in Python that has this IF Then chain
IF *x1 < limit: * --- do a ---
* * IF *x2 < limit: *--- do b ---
* * * * IF x3 < limit: *--- do c ---
* * * * * * * * * * * *.-----
* * * * * * * * * * * * ------
* * * * * * * * * * IF *x10 < limt: --- do j ---
* * * * * * * * * * THEN
* * * * * * * * *THEN
* * * * * * * -----
* * * * * THEN
* * *THEN
THEN
In other words, as long as * *'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.
Is this the equivalence in Python?
IF *x1 < limit:
* * * * --- do a *---
elif x2 < limit:
* * * * --- do b ---
----
----
elif x10 < limit:
* * * *--- do j ---
--http://mail.python.org/mailman/listinfo/python-list
* * * _______________ _______________ _______________ _______________ ______
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yaho o.com
In the code the 'xi's and 'limit' are variables and the --- do letters
---
phrases are simply writes to any array: * an_array[xi]=0
Actually, the code makes perfectly good sense, and is a necessity of
the algorithm I'm implementing, and works perfectly good in Forth, and
can be
written quite nicely within a normal page width.
I was just hoping I could perform the equivalent chain in Python
without
having to grossly indent the source code past the normal width of a
printed page.
But if that's the only way to do it in Python, then so be it.

You should probably consider using a function and then
convert the conditions to define return points:

def do_something(.. .args...):

* * if x1 >= limit:
* * * * return
* * ...do a...
* * if x2 >= limit:
* * * * return
* * ...do b...
* * etc.

That is provided I understand the flow of control in your
example... it's kind of strange to have THEN mark the *end*
of the then-branch in an if-then-else construct.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source *(#1, Nov 13 2008)>>Python/Zope Consulting and Support ... * * * *http://www.egenix.com/
>mxODBC.Zope.Da tabase.Adapter ... * * * * * *http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... * * * *http://python.egenix.com/

_______________ _______________ _______________ _______________ ____________
2008-11-12: Released mxODBC Connect 0.9.3 * * *http://python.egenix..com/

:::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::

* *eGenix.com Software, Skills and Services GmbH *Pastor-Loeh-Str.48
* * D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
* * * * * *Registered at Amtsgericht Duesseldorf: HRB 46611
It's interesting to see people think it's strange to have code that
has multiple nested levels of IF THEN. Apparently you haven't seen
any Forth, assembly, et al code. All you're doing is having the branch
point for each conditional be the end of the chain, otherwise it falls
through to the code after the conditional. This is done all the time
in languages that let you actually manipulate the hardware.

Just as a suggestion :-) a little humility would go a long way toward
being open minded and receptive to different paradigms. I've written
this program I'm doing now in Python in 3 other languages (including
Python, which I'm trying to make more efficient) and I seek to be
flexible in my software linguistic capabilities.

I asked a very narrow question about a very specific language
mechanism, and I know exactly what and why I'm doing what I'm doing.

I'll try some of the suggestions and see if they make the routine
faster in Python.
Nov 13 '08 #8
On 2008-11-14 00:19, jzakiya wrote:
On Nov 13, 5:48 pm, "M.-A. Lemburg" <m...@egenix.co mwrote:
>>>From: jzakiya <jzak...@mail.c om>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence
I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN
You should probably consider using a function and then
convert the conditions to define return points:

def do_something(.. .args...):

if x1 >= limit:
return
...do a...
if x2 >= limit:
return
...do b...
etc.

That is provided I understand the flow of control in your
example... it's kind of strange to have THEN mark the *end*
of the then-branch in an if-then-else construct.

It's interesting to see people think it's strange to have code that
has multiple nested levels of IF THEN.
Not at all, that's fairly common in a lot of languages. It's not
common to mark the end of the then-part using the THEN keyword,
though. You'd normally expect the body of the then-part after the
keyword.
Apparently you haven't seen
any Forth, assembly, et al code. All you're doing is having the branch
point for each conditional be the end of the chain, otherwise it falls
through to the code after the conditional. This is done all the time
in languages that let you actually manipulate the hardware.

Just as a suggestion :-) a little humility would go a long way toward
being open minded and receptive to different paradigms.
Without giving any hint as to what the quoted snippet
of code is written in, how do you expect people to make
any sense of it ? Especially when using an RPN stack oriented
language in a Python forum.

There's a reason why we hide Python byte code running on the
VM stack machine from Python users ;-)

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Nov 14 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.D atabase.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
_______________ _______________ _______________ _______________ ____________
2008-11-12: Released mxODBC Connect 0.9.3 http://python.egenix.com/

:::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Nov 14 '08 #9
jzakiya:
I asked a very narrow question about a very specific language
mechanism, and I know exactly what and why I'm doing what I'm doing.
You are of course free to use Python as you want. And probably some of
the answers weren't fully polite.
But it's interesting to see why they have given such answers. While
your code can be fit for Forth or assembly, it may be unfit, in its
current form, for the usual practices of Python programming (or C#/
Java programming, etc). In high-level languages large trees of very
nested ifs are seen as error-prone, not much readable, brittle... So
some alternative solutions are often used (even if they may be a
little slower).
If you need speed you may use Psyco too.

Bye,
bearophile
Nov 14 '08 #10

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

Similar topics

467
21698
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
176
8189
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? Thank you, -- greetz tom
41
3558
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
10
2283
by: Xah Lee | last post by:
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of n elements, we want to return a list that is a
12
6836
by: Jay | last post by:
ok, i thought for 2 seconds i might have created a Keylogger in python but i still have one major think stopping me... PYTHON. when i run the program i have python create a file named keylog2.log and it then logs all keys pressed/typed in the python IDE into that file. All i want to know now is how do i hide or background python so that it will log all the keys pressed outside of Python. feel free to play around with my program... but...
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
26
2659
by: hide1713 | last post by:
HI I'm currently using Python. I find that a instance variable must confined with self, for example: class a: def __init__(self): self.aa=10 def bb(self): print self.aa # See .if in c++,I could use aa to change that variable
4
1395
by: neptundancer | last post by:
Hi, to extend my skills, I am learning python. I have written small program which computes math expression like "1+2*sin(y^10)/cos(x*y)" and similar, so far only + - * / ^ sin con tan sqrt are supported. But my program is quite inextensible, I have to change the code to add new functions... Could some fellow experienced pythonista give me some tips how to make my program shorter, and more extensible? to use it, try something like...
0
10357
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.