Python else and elif statement syntax, pythonelif
Else and elif statements can also be called clauses because they cannot be used independently. Both of them appear in the if, for, and while statements. An else clause can be added. The elif clause is used when more conditions need to be checked. It is used with if and else, and elif is short for else if.
How to use if and else statements
The following example shows how to use the if and else statements together. First, set an integer variable. Let's see if the variable is greater than 0 or smaller than 0.
>>> Num = 0
>>> If num> 0:
>>> Print 'This number is greater than 0'
>>> Else:
>>> Print 'the number is smaller than 0'
First, the program determines whether the condition of the if statement is true. if it is true, the content of the if statement block will be executed. If this num is greater than 0, print 'This number is greater than 0' will be executed. But num = 0. Obviously, the if statement is false, and the corresponding statement block is not executed. if the first statement block is not executed, the second statement block is executed.
The calculation result of the program is 'This number is smaller than 0'. Some people may doubt that the result of this number being equal to 0 is more accurate. So why is such a result? For the data in the case, the if and else statements are judged and checked in the following way: if num is greater than 0, 'This number is greater than 0'; otherwise, 'This number is smaller than 0 '. Because else has no conditions to set, if conditions cannot be met as false, the else clause block will be output unconditionally.
For this question, we need to add a condition for it, so that the result will be more correct and the elif clause will be used.
If, else, And elif statements
>>> Num = 0
>>> If num> 0:
>>> Print 'This number is greater than 0'
>>> Elif num <0:
>>> Print 'the number is smaller than 0'
>>> Else:
>>> Print 'the number is 0'
The program judges if and elif respectively. if the condition is not true, the program outputs the content of the else statement block. The program running result is: The number is 0.
Note the following when using the if, else, And elif statements:
1. else and elif are child blocks and cannot be used independently.
2. An if statement can contain multiple elif statements, but only one else statement can end
Role of else in while and for loop statements
In python, The else clause can be used in the while and for loops. It is executed only after the loop ends. If the break statement is used at the same time, the else clause block will be skipped. Note that the else clause and break statement cannot be used at the same time!
Play snake net article, reprint please indicate the source and source URL: http://www.iplaypython.com/jinjie/else-elif.html
Q: customized IT education platform, one-to-one service, Q & A. Official Website for developing programming social headlines: www.wenaaa.com
QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!