If syntax
Python conditional statements are blocks of code that are executed based on the execution result of one or more statements (True or False).
The following figure can be used to briefly understand the execution process of conditional statements:
The Python programming language specifies that any non-0 and non-null values are true, and 0 or null is false.
In Python programming, if statements are used to control the execution of programs, and their basic form is:
Python
if judgment condition:
Statement 1......
else:
Statement 2......
Statement 1 is the statement executed by the if-if judgment condition
Then if the judgment condition is not true, execute statement 2 under else
In Python, indentation is used to represent hierarchical relationships
Python
if 5>2:
print("condition holds", "execution statement 1")
else:
print('condition not valid', "execution statement 2")
# Condition Validation Execution Statement 1
The judgment conditions of the if statement can be expressed as > (greater than), < (less than), == (equal to), > = (greater than or equal), and < = (less than or equal).
When the judgment condition is more than one value, you can use the following form:
Python
if judgment condition 1:
Execution Statement 1......
ELIF judgment condition 2:
Execution Statement 2......
ELIF judgment condition 3:
Execution statement 3......
else:
Execution Statement 4......
For example:
Python
# Grades are judged based on scores
score = int(input("Please enter your grade:"))
if score >=60 and score <80:
print("Passing grade")
elif score>=80 and score <90:
print("Good grade")
elif score>=90 and score <=100:
print("Excellent")
else:
print("Failed")
# Input 50 outputs failed grades
# Input 88 output is good
Since Python does not support switch statements, multiple conditional judgments can only be implemented with elif, if multiple conditions need to be judged at the same time
- Use or (or) to indicate that one of the two conditions is true, and the condition is judged to be successful
- When using and (and ), it means that the judgment condition is successful only when both conditions are true at the same time.
When there are multiple ifs, parentheses can be used to distinguish the order of judgments, and the judgments in parentheses are executed first
In addition, the priority of and and or is lower than that of judgment symbols such as > (greater than) and < (less than), that is, greater than and less than will be judged in the absence of parentheses
You can also use the if condition judgment statement and the output statement on the same line, as follows:
Python
# Grades are judged based on scores
score = int(input("Please enter your grade:"))
if score >=60 and score <80: print
elif score>=80 and score <90: print("Good grade")
ELIF score>=90 and score <=100: print ("Excellent")
else: print("Failed")
# Input 50 outputs failed grades
# Input 88 output is good
if nested
[Python] (http://c.biancheng.net/python/), if, if else, and if elif else can be nested with each other. Therefore, when developing a program, you need to choose the appropriate nesting scheme according to the needs of the scenario. It should be noted that when nesting with each other, you must strictly abide by the indentation specifications of code blocks at different levels.
For example, nest an if else statement in the simplest if statement with the following form:
Python
if expression 1:
if expression 2:
Code block 1
else:
Code block 2
Alternatively, nest an if else statement in an if else statement in the form of:
Python
if expression 1:
if expression 2:
Code block 1
else:
Code block 2
else:
if expression 3:
Code block 3
else:
Code block 4
Example: Determine whether it is drunk driving
If it is stipulated that the blood alcohol content of the vehicle driver is less than 20mg/100ml, it does not constitute drunk driving; the alcohol content is greater than or equal to 20mg/100ml is drunk driving; the alcohol content is greater than or equal to 80mg/100ml is drunk driving. First, write a Python program to determine whether it is drunk driving.
So we use if nesting to implement this requirement:
Python
proof = int(input("Enter the driver's blood alcohol per 100ml:"))
if proof < 20:
print("The driver does not constitute a drunk driving")
else:
if proof < 80:
print("The driver has constituted a DUI")
else:
print("The driver has constituted drunk driving")
Or use if nesting to improve our scoring system:
Python
score = int(input("Please enter your grade:"))
if score >= 60:
if score>=80:
if score>=90:
print("Excellent")
else:
print("Good grade")
else:
print("Passing grade")
else:
print("Failed")
# This time we are implementing triple nesting
Trinocular Operations
Python can implement the function of a trinocular operator through an if statement, so it can be roughly treated as a trinocular operator. The syntax format of an if statement as a trinocular operator is as follows:
Python
True_code if decide else False_code
The rules for the trinocular operator are: first evaluate the logical expression expression, if the logical expression returns True, execute and return a value of True_statements; if the logical expression returns False, execute and return a value of False_statements
Python
Condition Valid Execution Statement 1 if Condition else Condition Not Valid Execution Statement 2
Look at the code below:
Python
a = 5
b = 3
st = "a is greater than b" if a > b else "a is not greater than b"
# Output "a is greater than b"
print(st)
If you just need to see the result in the console, then we can write something like this:
Python
a,b=3,5
print(f"{a}>{b}") if a>b else print(f"{a}<{b}")
# 3<5
Or use a print statement
Python
a,b=5,3
print(f"{a}>{b}" if a>b else f"{a}<{b}")
Example: Use the trinocular operator to determine the size of two numbers (realize the effect of the if else statement)
Python
x=int(input('Please enter the first number'))
y=int(input('Please enter the second number'))
if x>y:
print(f'{x} greater than {y}')
else:
print(f'{x} less than {y}')
Python
# If the condition is true, put it in the front, and put it below the else statement
x=int(input('Please enter the first number'))
y=int(input('Please enter the second number'))
print(f'{x} greater than {y}' if x>y else f'{x} less than {y}')
We can also use methods such as if nesting to nest the trinocular operator, which can form more complex expressions. When nesting, we need to pay attention to the pairing of if and else, for example:
Python
a if a>b else c if c>d else d
# Understood as
a if a>b else ( c if c>d else d )
# Parentheses can be used
This is the smallest number of the three numbers output using if else
Python
a, b, c = -1, -5, 1
if a < b and a < c:
print(a)
elif b < c and b < a:
print(b)
else:
print(c)
How to implement it with the trinocular operator? After-class homework!
Python
a, b, c = -1, 2, 1
print(a if a < b and a < c else b if b < c and b < a else c)