We usually take the use of else statements in if structures as a matter of course. However, Python’s powerful syntactic sugar allows the else statement to be used in for and while loops! Let’s take a look at Python’s for and break loops through examples. Tips for using else statements in structures
After the while or for loop in Python, there can also be an else clause. The function is that if the condition in the for loop is not satisfied, the else statement will be executed at the end.
for i in range(5): if i == 1: print 'in for' else: print 'in else' print 'after for-loop' # in for # in else # after for-loop
But we found that the if condition was established during the loop, and the content in the else statement was eventually executed. Why is this?
Okay, let’s look at the following program now:
for i in range(5): if i == 1: print 'in for' break else: print 'in else' print 'after for-loop' # in for # after for-loop
We added a break in the if, this is because else It is executed after for, but the else statement will only be executed when the for loop exits normally (the loop is not ended by break). When the loop is interrupted by a break statement, else is not executed.
for/else is equivalent to the following code. You can add a flag similar to C language:
found = False for i in range(5): if i == 1: found = True print 'in for' if not found: print 'not found' print 'after for-loop' # in for # after for-loop
is similar to the for statement, The usage of the else clause in the while statement is the same. The else block is executed when the loop ends normally and the loop condition is not established.
We are already familiar with the conditional statement if-else, but in Python, for-else is used to handle traversal failure.
For example, we want to implement such a function: find the largest perfect square number in (81, 99) and output it. If it cannot find it, output a prompt.
If you use a C++ for loop to implement it, you must manually determine whether the for loop traversal fails:
#include <iostream> #include<math.h> using namespace std; int main() { int i; float n; for(i=99;i>81;i--) { n=sqrt((float)i); if(n==int(n)) { cout<<i; break; } } if(i==81) //边界判断 cout<<"didn't find it!"<<endl; return 0; }
This function can be easily implemented using Python's for-else:
from math import sqrt for n in range(99,81,-1): root = sqrt(n) if root == int(root): print n break else: print"Didn't find it!"
The else is executed only after the for loop is completely completed; if it is executed from If break jumps out, else will jump out together.
What needs special attention is that if there is an if statement in for, the indentation of else must be aligned with for. If it is aligned with if, it will become an if-else statement, which will produce unexpected errors as follows:
from math import sqrt for n in range(99,81,-1): root = sqrt(n) if root == int(root): print n break else: print"Didn't find it!"
Although using for-else saves two lines of code and is easier to read, it is easy to be confused with if-else. It seems that it is not commonly used in practice, but is more likely to be handled manually.
For more articles related to the use of else statements in Python's for and break loop structures, please pay attention to the PHP Chinese website!