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
- if expression:
- expr_true_suite
- else:
- 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
- if expression1:
- expr1_true_suite
- elif expression2:
- expr2_true_suite
- elif expressionN:
- exprN_true_suite
- else:
- 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
- #! /Usr/bin/env python
- # Finding the maximum approx.
- Def showMaxFactor (num ):
- Count = num/2
- While count> 1:
- If num % count = 0:
- Print 'largest factor of % d is % d' % (num, count)
- Break
- Count-= 1
- Else:
- Print num, is prime'
- For I in range (10, 21 ):
- 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
- try:
- log = open('logfile.txt','w')
- except:
- print 'file logfile.txt is not exsit'
- else:
- log.write('no exception caught')
- log.close()
Of course, in exception handling, else can be used together with finally: try-try t-else-finally.