The IF statement is used for the execution of the control program, 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 is an optional statement that executes the relevant statement when it needs to be executed when the condition is not true:
' Python ' if ' Python ': # to determine whether the variable is ' python ' print(" Welcome to learn python ") # and output welcome message Else: print(name) # output variable name when condition is not valid
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:
num =-59ifNum >= 90:#determine the value of Num Print('Excellent')elifNum >= 80: Print("Good")elifNum >= 60: Print("Pass")Else: Print("inferior lattice")
If it is judged that multiple conditions need to be judged at the same time, the or (or) can be used to indicate that two conditions have a successful condition, and when using and (with), it means that only two conditions have been established and the condition is successful.
num = 9ifNum >= 0 andNum <= 10:#determine if the value is between 0~10 Print(True) Num= 10ifNum < 0ornum > 10:#determine if the value is less than 0 or greater than Print(True)Else: Print(False) Num= 13#determine if the value is between 0~5 or 10~15if(Num >= 0 andNum <= 5)or(Num >= 10 andNum <= 15): Print(True)Else: Print(False)
Python IF condition judgment