15

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?

0

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.