Pythonconditional Statements
A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).
You can easily understand the execution of conditional statements By:
The Python program language specifies any non-0 and non-null (null) values of true,0 or null to False.
In Python programming, If statements are used in the execution of control programs, in the basic form:
If judgment condition: EXECUTE statement ... Else: EXECUTE statement ...
Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.
else as an optional statement, you can execute the relevant statement when you need to execute the content when the condition is not true, as shown in the following example:
#!/usr/bin/python#-*-coding:utf-8-*-# example 1:if basic usageFlag= falsename = ' luren ' if name == ' python ' : # judgment variable no is ' python ' Span class= "pln" > flag = true# condition is set to True print Welcome boss ' # and output welcome information else : print name # Output variable name when condition is not valid
The output is:
>>> luren# output Results
The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).
When judging a condition as multiple values, you can use the following form:
if judgment condition 1: Execute statement 1... elif judgment Condition 2: EXECUTE statement 2... elif judgment Condition 3: EXECUTE statement 3... else: EXECUTE statement 4 ...
Examples are as Follows:
#!/usr/bin/python#-*-coding:utf-8-*-# example 2:elif usageNum= 5 IfNum== 3: # Determine the value of num Print ' Boss ' ElifNum== 2: Print ' user ' elif num == 1: ' worker ' elif num < 0:# value is less than zero output print ' error ' else: print ' roadman ' # conditions are not true when output
The output is:
>>> roadman# output Results
Since Python does not support switch statements, multiple conditions can only be determined by elif, and if the judgment requires multiple conditions to be judged at the same time, the or (or) is used to indicate that two conditions have a successful condition when they are established; when using and (and), Indicates that only two conditions have been established at the same time, the judgment condition SUCCEEDS.
#!/usr/bin/python#-*-coding:utf-8-*-# example 3:IF statement multiple conditionsNum= 9IfNum>= 0 andNum<= 10: # Determine if the value is between 0~10 Print ' Hello '>>>Hello# Output ResultsNum= 10IfNum< 0 OrNum> 10: # Determine if the value is less than 0 or greater than 10 Print ' Hello 'Else:Print ' Undefine '>>>Undefine# Output ResultsNum= 8# Determine if the value is between 0~5 or 10~15If (Num>= 0 and num <= 5) or (num >= 10 and num <= 15): print ' hello ' else: print ' undefine ' >>> undefine # output
If there are multiple conditions, you can use parentheses to distinguish the order of judgment, the judgment in parentheses precedence, and the priority of and and or is lower than > (greater than), < (less than), such as the judgment symbol, that is, greater than and less than in the absence of parentheses will be compared with or priority to Judge.
Simple Group of statements
You can also use the IF condition judgment statement on the same line, as in the following example:
#-*-coding:utf-8-*-var=if(var= =):print"variable var The value is "print" Good bye! "
The above code executes the output as Follows:
The value of variable var is 100
***********************************************************
Example Demo
***********************************************************
Python Foundation sixth Day