Python Conditional 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 usage flag = Falsename = ' Luren ' if name = = ' Python ': # Judgment variable no is ' python '
flag = True # condition set flag for true print ' welcome boss ' # and OUTPUT welcome message else: Print name # condition not set when output variable name
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 for 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 usage num = 5 If num = = 3: # Determine NUM's value print ' boss ' elif num = = 2: print ' user ' elif num = = 1: print ' worker ' Elif num < 0: # value less than zero output print ' error ' else: print ' Roadman ' # conditions are not set when the 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 conditions num = 9if num >= 0 and num <=: # Determine if the value is between 0~10 print ' Hello ' >>> hello # output num = 10if num < 0 or num >: # Determine if the value is less than 0 or greater than print ' Hello ' else:print ' u Ndefine ' >>> undefine # output num = 8# Determine if the value is between 0~5 or 10~15 if (num >= 0 and Num <= 5) or (Num >= and Num & lt;=: print ' hello ' else: print ' undefine ' >>> undefine # Output results
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:
#!/usr/bin/python #-*-Coding:utf-8-*-var = if (var = =): print "variable var value is 100"
The above code executes the output as follows:
The value of variable var is 100Good bye!
Python operator Precedence
The following table lists all the operators from highest to lowest priority:
The following example demonstrates the operation precedence of all Python operators:
#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (A + b) * C/D # (+ *)/5print "Value of (A + B) * C/D is", EE = ((a + b) * c)/d # ($ *)/5print "Value of ((A + b) * C)/d is", EE = (A + b) * (C/D); # (15/5) print "Value of (A + B) * (C/D) is", EE = a + (b * c)/D; # + (150/5) print "Value of A + (b * c)/d is", E
The result of the above example output:
Value of (A + B) * C/D is 90Value of ((A + b) * C)/d are 90Value of (A + B) * (C/D) is 90Value of A + (b * c)/D-is 50