In the previous article we explained the condition of a single condition and multiple conditions in a conditional statement. For today's article, let's take a look at some of the remaining scenarios in the Python conditional statement.
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 . Examples of simultaneous judgments are as follows:
#!/usr/bin/python#-*-coding:utf-8-*-num = 9if num >= 0 and num <=: # Determine if the value is between 0~10 print ' hello ' # output knot Result: Hello num = 10if num < 0 or num >: # Determine if the value is less than 0 or greater than the print ' hello ' else: print ' undefine ' # output: und Efine num = 8# Determine if the value is between 0~5 or 10~15 if (num >= 0 and Num <= 5) or (Num >= and num <=): print ' Hello ' els E: print ' undefine ' # output: undefine
If there are multiple conditions can use parentheses to distinguish the order of judgment, the judgment in parentheses precedence execution, and and and or priority is lower than > (greater than), < (less than), such as the judgment symbol, that is greater than and less than in the case without parentheses will be compared with or priority to judge .
In addition to the multiple conditions mentioned above, and the two cases mentioned in the previous article, there is a simple statement group . You can 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" print "good bye! "
Like this, the output will be as follows:
The value of variable var is 100Good bye!
(If statements can be judged with > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than equals) to indicate their relationship. )
The above is the Python conditional statement of the other two cases, combined with two of the previous article, a total of four cases of Python conditional statements for example and description, I hope you have just learned Python help you, to help you learn python easier.
"Recommended"
How to write conditional statements in Python 01: Single and multiple condition judgments