Sort else statements in Python

Source: Internet
Author: User

PythonIt is an object-oriented, literal translation computer programming language. It is also a powerful and complete general-purpose language with more than a decade of development history, mature and stable. This language is very simple and clear syntax, suitable for completing a variety of high-level tasks, almost all operating systems can run. At present, technologies based on this language are rapidly developing, the number of users is rapidly expanding, and there are a lot of related resources. The following is a summaryElseStatement.

If-else statement:

The else statement used with the if statement in the Condition Statement is the same as that used in various languages. If the Boolean value of the conditional expression of the if statement is false, the program executes the code after the else statement. Its syntax is well-known:

Python code

 
 
  1. if expression:  
  2. expr_true_suite  
  3. else:  
  4. expr_false_suite 

Elifelse-if) Statement:

Elif is an else-if statement provided by python. It checks whether the values of multiple conditional expressions are true and executes the code in a specific code block when the values are true. Like else, elif is optional, but note that one if statement can be used with multiple elif statements, but only one else statement can be used at the end:

Python code

 
 
  1. if expression1:  
  2. expr1_true_suite  
  3. elif expression2:  
  4. expr2_true_suite  
  5. elif expressionN:  
  6. exprN_true_suite  
  7. else:  
  8. none_of_the_above_suite 

While-else/for-else statement:

In other languages, except for conditional statements, else branches are not seen, but else statements can also be used in the while and for loops in Python. Their work order is: when used in a loop, the else statement is executed only after the loop is completed. That is to say, the break statement also skips the else code block, as long as the loop ends normally, instead of using break, the else statement is executed.

Example of while-else:

Python code

 
 
  1. #! /Usr/bin/env python
  2. # Finding the maximum approx.
  3. Def showMaxFactor (num ):
  4. Count = num/2
  5. While count> 1:
  6. If num % count = 0:
  7. Print 'largest factor of % d is % d' % (num, count)
  8. Break
  9. Count-= 1
  10. Else:
  11. Print num, is prime'
  12. For I in range (10, 21 ):
  13. ShowMaxFactor (I)

The execution result of the program is:

Largest factor of 10 is 5

11 is prime

Largest factor of 12 is 6

13 is prime

Largest factor of 14 is 7

Largest factor of 15 is 5

Largest factor of 16 is 8

17 is prime

Largest factor of 18 is 9

19 is prime

Largest factor of 20 is 10

Try-else statement:

For errors and exceptions in python, the try-release T statement segment can also be used with else. Its function is not much different from the else statement in the Loop: if no exception is detected in the try range, run the else clause. Take a look at the Syntax:

Python code

 
 
  1. try:  
  2. log = open('logfile.txt','w')  
  3. except:  
  4. print 'file logfile.txt is not exsit' 
  5. else:  
  6. log.write('no exception caught')  
  7. log.close()  

Of course, in exception handling, else can be used together with finally: try-try t-else-finally.

Related Article

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.