Why is the purpose of the “else” clause following a “for” or “while” loop?

I am a Python beginner. I find that the else in forelse and whileelse is completely unnecessary. Because for and while will finally run to else, and we can use the usual lines instead.

For example:

for i in range(1, 5):
    print i
else:
    print 'over'

and

for i in range(1, 5):
    print i
print 'over'

are the same.

So why does Python have else in forelse and whileelse?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are wrong about the semantics of for/else. The else clause runs only if the loop completed, for example, if a break statement wasn’t encountered.

The typical for/else loop looks like this:

for x in seq:
    if cond(x):
        break
else:
    print "Didn't find an x I liked!"

Think of the “else” as pairing with all of the “if’s” in the loop body. Your samples are the same, but with “break” statements in the mix, they are not.

A longer description of the same idea: http://nedbatchelder.com/blog/201110/forelse.html

Method 2

The for ... else statement is used to implement search loops.

In particular, it handles the case where a search loop fails to find anything.

for z in xrange(10):
    if z == 5:
        # We found what we are looking for
        print "we found 5"
        break # The else statement will not execute because of the break
else:

    # We failed to find what we were looking for
    print "we failed to find 5"
    z = None

print 'z = ', z

output:

we found 5
z =  5

That search is the same as

z = None
for z in xrange(10):
    if 5 == z:
        # We found what we are looking for
        break

if z == None:
    print "we failed to find 5"
else:
    print "we found 5"

print 'z = ', z

Remember that for doesn’t initialize z if the search list is empty (i.e. []). That’s why we have to ensure that z is defined when we use it after the search. The following will raise an exception because z is not defined when we try to print it.

for z in []:
    if 5 == z:
        break

print "z = ",z

output

    print "z = ",z
NameError: name 'z' is not defined

In summary, the else clause will execute whenever the for loop terminates naturally. If a break or an exception occurs in the for loop the else statement will not execute.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x