I understand how this construct works:
for i in range(10):
print(i)
if i == 9:
print("Too big - I'm giving up!")
break;
else:
print("Completed successfully")
But I don't understand why else
is used as the keyword here, since it suggests the code in question only runs if the for
block does not complete, which is the opposite of what it does! No matter how I think about it, my brain can't progress seamlessly from the for
statement to the else
block. To me, continue
or continuewith
would make more sense (and I'm trying to train myself to read it as such).
I'm wondering how Python coders read this construct in their head (or aloud, if you like). Perhaps I'm missing something that would make such code blocks more easily decipherable?
It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:
But anytime you see this construct, a better alternative is to either encapsulate the search in a function:
Or use a list comprehension:
It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.
See also [Python-ideas] Summary of for...else threads
Let us suppose we have a function
Which means that only 5 is not broken. Now in case broken is never evaluated with 5:-
Will give output:-
Where when broken is evaluated with 5:-
It will not print anything. Thus indirectly telling there is at least something which is not broken.
However, in case you want to cheat and skip something you found was broken. That is, continue the loop even though you found 5 as broken, else statement will still be printed. That is :-
Will print
I hope it clears the confusion instead of creating a new one :-)
I think documentation has a great explanation of else, continue
Source: Python 2 docs: Tutorial on control flow
The
else
keyword can be confusing here, and as many people have pointed out, something likenobreak
,notbreak
is more appropriate.In order to understand
for ... else ...
logically, compare it withtry...except...else
, notif...else...
, most of python programmers are familiar with the following code:Similarly, think of
break
as a special kind ofException
:The difference is
python
impliesexcept break
and you can not write it out, so it becomes:Yes, I know this comparison can be difficult and tiresome, but it does clarify the confusion.
Codes in
else
statement block will be executed when thefor
loop was not be broke.From the docs: break and continue Statements, and else Clauses on Loops
"else" here is crazily simple, just mean
1, "if
for clause
is completed"It's wielding to write such long statements as "for clause is completed", so they introduce "else".
else
here is a if in its nature.2, However, How about
for clause is not run at all
So it's completely statement is logic combination:
or put it this way:
or this way: