Chapter 6 of python3, python3
A Python Condition Statement is a code block that is determined by the execution result (True or False) of one or more statements.
The execution process of conditional statements, such:
A condition statement, also known as an if statement, has the following complete Syntax:
If condition 1: Statement Block 1 elif condition 2: Statement Block 2 else: Statement block 3
The execution process of the preceding syntax is:
When condition 1 is True, execute the statement of statement Block 1.
If condition 1 is False, condition 2 is used.
When condition 2 is True, execute the statement of statement Block 2.
If condition 2 is False, execute statement Block 3.
Elif can be omitted, that is
If condition 1: Statement Block 1 else: Statement Block 2
Elif can appear multiple times, that is
If condition 1: Statement Block 1 elif condition 2: Statement Block 2 elif Condition 3: Statement block 3 else: Statement block 4
Else can be omitted, that is
If condition 1: Statement Block 1
It seems complicated. In fact, you only need to remember that the condition statements are from top to bottom to determine whether the condition is true. If the condition is true, the block corresponding to the condition is executed, if not, continue to judge the next condition. If none of the conditions are true, execute the statement block corresponding to else.
Note that:
- Each condition is followed by a colon (:), indicating the statement block to be executed after the condition is met.
- Use indentation to divide statement blocks. statements with the same number of indentations form a statement block.
Elif is short for else if. in python, elif replaces else if. Therefore, the keywords of python are: if, elif, and else.
The following shows how to use it:
A = 20b = 100if a> B: print ('a greater than B ') else: print ('a not greater than B ')
Output the code after execution:
A is not greater than B