Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the purpose of the "else" clause following a "for" or "while" loop? [duplicate]

I am a Python beginner. I find that the else in for-else and while-else 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 for-else and while-else?

like image 460
Jimmy Avatar asked Jul 20 '12 02:07

Jimmy


2 Answers

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

like image 95
Ned Batchelder Avatar answered Nov 13 '22 07:11

Ned Batchelder


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.

like image 32
shrewmouse Avatar answered Nov 13 '22 06:11

shrewmouse