By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Loops Control with Python

P: n/a

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
Share this Question
Share on Google+
6 Replies


P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.