Tips for using else statements in the for and break loop structures of Python, pythonelse

Source: Internet
Author: User

Tips for using else statements in the for and break loop structures of Python, pythonelse

An else clause can also be created after the while or for loop in Python. This clause is used to execute the else statement when the if condition in the for loop is not met.

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

However, we found that the if condition was set up in the loop process and finally the content in the else statement was executed. Why?

Okay. Now let's look at the following program:

for i in range(5): if i == 1:  print 'in for'  breakelse: print 'in else'print 'after for-loop'# in for# after for-loop

We added a break in the if statement because else is executed after the for statement, but the else statement is executed only when the for loop Exits normally (not ended by the break statement ). When the loop is interrupted by the break statement, else will not be executed.

For/else is equivalent to the following code. You can add a flag like the C language:

found = Falsefor 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

Similar to the for statement, the while statement uses the same else clause. The else block is executed when the loop ends normally and the loop condition is invalid.

We are familiar with the conditional statement if-else, but in Python, for-else is used to handle traversal failures.

For example, we want to implement this function: Find the maximum number of complete records in () and output the result. If no result is found, the output prompt is displayed.

If you use the c ++ for loop implementation, 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 ;}

With Python's for-else, you can simply implement this function:

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!" 

Else is executed only after the for loop is complete. If the for loop jumps out from break halfway, The else jumps out together.

Note that if the for statement contains an if statement, the else indent must be aligned with the for statement. if it is aligned with the if statement, it is changed to the if-else statement. Unexpected errors may occur 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 the use of for-else saves two lines of code for easy reading at the same time, it is easy to be confused with if-else. It seems that it is not commonly used. Instead, it is more likely to be processed manually.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.