Python conditional statements
Python conditional statements are code blocks that are executed based on the execution results (True or False) of one or more statements.
You can simply understand the execution process of conditional statements through the following figure:
The Python programming language specifies that any non-0 and non-null value is true , 0 or null is false.
The if statement in Python programming is used to control the execution of the program. The basic form is:
Execution statement...
else:
Execution statement...
When the "judgment condition" is true (non-zero), the following statement will be executed, and the execution content can be multiple lines, and the same range is represented by indentation.
else is an optional statement. When you need to execute the content when the condition is not met, you can execute the relevant statement. The specific example is as follows:
# -*- coding: UTF-8 -*-
# Example 1: Basic usage of if
flag = False
name = 'luren'
if name == ' python': # Determine whether the variable is 'python'
flag = True # Set the flag to true when the condition is true
print 'welcome boss' # and output the welcome message
else:
When it is not true, the output variable name
The output result is:
elif Judgment condition 2:
Execution statement 2...
elif Judgment condition 3:
Execution statement 3...
else:
Execution statement 4...
Examples are as follows:
# -*- coding: UTF-8 -*-
# Example 2: elif usage
num = 5
if num == 3: #Determine the value of num
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
PRINT 'Worker'
Elif Num & LT; 0:#Value is less than zero output
Print '
Else:
PRINT' Roadman '
The output result is:
Since python does not support switch statements, there are many A conditional judgment can only be realized by using elif. If the judgment requires multiple conditions and needs to be judged at the same time, you can use or (or), which means that the judgment condition is successful when one of the two conditions is true; when using and (and), it means that only The judgment condition is successful only when both conditions are met at the same time.
# -*- coding: UTF-8 -*-
# Example 3: if statement with multiple conditions
num = 9
if num >= 0 and num <= 10: # Determine whether the value is between 0 and 10
print 'hello'
>>> hello # Output result
num = 10
if num < 0 or num > 10: # Determine whether the value is less than 0 or greater than 10
print 'hello'
else:
print 'undefine'
>>> undefine # Output result
num = 8
# Determine whether the value is between 0~5 or 10~15
if (num > ;= 0 and num <= 5) or (num >= 10 and num <= 15):
print 'hello'
else:
print 'undefine'
>> ;> undefine # Output result
When if has multiple conditions, brackets can be used to distinguish the order of judgments. The judgments in brackets are executed first. In addition, the priority of and and or is lower than that of > (greater than), < (less than) and other judgment symbols. , that is, greater than and less than will be judged first than and or without parentheses.
Simple statement group
You can also use if conditional judgment statements on the same line, as shown below:
# -*- coding: UTF-8 -*-
var = 100
if (var == 100): print "The value of variable var is 100"
print "Good bye!"
The above code execution output results are as follows:
Good bye!