473,396 Members | 1,655 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,396 software developers and data experts.

Loops Control with Python


Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?

--
Edward WIJAYA
SINGAPORE

------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------
Oct 13 '06 #1
6 1558

Wijaya Edward wrote:
Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?
Literally.

for si in range(100 + 1):
for s2 in some_array:
if s1 == s2: break

Same thing, but nicer.

for si in range(100 + 1):
if si in some_array:
# Do something here.....

Cheers,

Jon.

Oct 13 '06 #2
Wijaya Edward wrote:
Can we make loops control in Python? What I mean is that whether
we can control which loops to exit/skip at the given scope.
For example in Perl we can do something like:
OUT:
foreach my $s1 ( 0 ...100) {
IN:
foreach my $s2 (@array) {
if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}
}
}
How can we implement that construct with Python?
If you are not willing to determine the problem the code is
written to solve, you are doomed to working with "Perl in Python".
While I think Python is a far better language than Perl, I remain
convinced that Perl is a better Perl than Python. Describe an
actual problem, don't simply give an example from another language.

If you don't know about break, continue, and for ... else, go
study the Python language.

--
--Scott David Daniels
sc***********@acm.org
Oct 13 '06 #3

Wijaya Edward wrote:
Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?
Python does not use Labels. If you want to quit a single loop then look
up the Python break statement. If you want to exit deeply nested
execution then Python has exceptions. this maybe new to a Perl
programmer so please take time to understand Python exceptions.

There follows a function that you can call from an interactive session
to explore one type of use for exceptions that is rather like your use
of Perl labels shown.

==========================
class Outer(Exception):
pass
class Inner(Exception):
pass

def skip_loops(y1 = -1, y2 = -1, y3 = -1):
''' Shows how to skip parts of nested loops in Python'''
try:
for x0 in range(3):
try:
for x1 in range(3):
for x2 in range(3):
if x2 == y2:
raise Inner
if x2 == y3:
break
print (x0,x1,x2)
if x1 == y1:
raise Outer
print (x0,x1)
except Inner:
print "Raised exception Inner"
print (x0,)
except Outer:
print "Raised exception Outer"

==========================
>>skip_loops(y1=2)
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 1)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
Raised exception Outer
>>skip_loops(y2=2)
(0, 0, 0)
(0, 0, 1)
Raised exception Inner
(0,)
(1, 0, 0)
(1, 0, 1)
Raised exception Inner
(1,)
(2, 0, 0)
(2, 0, 1)
Raised exception Inner
(2,)
>>>

- Paddy.
P.S. Welcome to Python!

Oct 13 '06 #4
hg
Paddy wrote:
Wijaya Edward wrote:
>Can we make loops control in Python?
What I mean is that whether we can control
which loops to exit/skip at the given scope.

For example in Perl we can do something like:

OUT:
foreach my $s1 ( 0 ...100) {

IN:
foreach my $s2 (@array) {

if ($s1 == $s2) {
next OUT;
}
else {
last IN;
}

}
}

How can we implement that construct with Python?

Python does not use Labels. If you want to quit a single loop then look
up the Python break statement. If you want to exit deeply nested
execution then Python has exceptions. this maybe new to a Perl
programmer so please take time to understand Python exceptions.

There follows a function that you can call from an interactive session
to explore one type of use for exceptions that is rather like your use
of Perl labels shown.

==========================
class Outer(Exception):
pass
class Inner(Exception):
pass

def skip_loops(y1 = -1, y2 = -1, y3 = -1):
''' Shows how to skip parts of nested loops in Python'''
try:
for x0 in range(3):
try:
for x1 in range(3):
for x2 in range(3):
if x2 == y2:
raise Inner
if x2 == y3:
break
print (x0,x1,x2)
if x1 == y1:
raise Outer
print (x0,x1)
except Inner:
print "Raised exception Inner"
print (x0,)
except Outer:
print "Raised exception Outer"

==========================
>>>skip_loops(y1=2)
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 1)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
Raised exception Outer
>>>skip_loops(y2=2)
(0, 0, 0)
(0, 0, 1)
Raised exception Inner
(0,)
(1, 0, 0)
(1, 0, 1)
Raised exception Inner
(1,)
(2, 0, 0)
(2, 0, 1)
Raised exception Inner
(2,)
- Paddy.
P.S. Welcome to Python!
How about a thread on GOTOs ? ;-)

Oct 13 '06 #5

hg wrote:
Paddy wrote:
P.S. Welcome to Python!
How about a thread on GOTOs ? ;-)
I'm trying to be nice on c.l.p.
- Mind you, I do have that rant as part of my blog:
http://paddy3118.blogspot.com/2006/0...with-perl.html

;-)

- Paddy.

Oct 13 '06 #6
hg wrote:
How about a thread on GOTOs ? ;-)
A thread? No need! There's a module:

http://entrian.com/goto/

;-)

STeVe
Oct 13 '06 #7

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

Similar topics

10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
by: Derek Martin | last post by:
On Fri, Jul 18, 2008 at 12:21:49PM -0700, mark floyd wrote: One wonders why... :) Even if Python didn't offer a way to write a for loop in a similar fashion (someone else replied about...
17
by: Gandalf | last post by:
how can I do width python a normal for loop width tree conditions like for example : for x=1;x<=100;x+x: print x thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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...

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.