A python conditional statement determines which part of the code executes by executing the result of one or more statements (true or false).
If statement
The general form of the IF statement is as follows:
If condition 1:
Statement 1
Elif Condition 2:
Statement 2
Else
Statement 3
The meaning is:
If condition 1 is true, statement 1 is executed;
If condition 1 is false, the condition 2 is judged;
If condition 2 is true, statement 2 is executed;
If condition 2 is false, statement 3 is executed.
Attention:
1. Each condition is followed by a colon (:), which indicates the next statement to be executed after satisfying the condition;
2. Use indentation to divide the statement block, the same level of statements using the same indentation.
The following is a simple example:
VAR1 = 100
If var1:
Print ("1-if expression condition is true")
Print (VAR1)
VAR2 = 0
If var2:
Print ("2-if expression condition is true")
Print (VAR2)
Print ("Good bye!")
The results of the implementation are as follows:
1-if expression condition is true
100
Good bye!
As you can see from the results, the statement within the corresponding if is not executed because the value of VAR2 is 0.
Python3 Condition Control if