Indent in
Python's most distinctive feature is the use of indentation to write modules. Let's use the if selection structure below for an example. If followed by the condition, if the condition is true, then some statements that belong to the if are executed.
First look at the expression of the C language (note that this is C, not python!)
if (i > 0)
{
x = 1;
y = 2;
}
This statement says that, if i>1, we will carry out the two assignment operations that are included in parentheses.
The parentheses contain the block action, which indicates that the statement is subordinate to the IF.
In Python, for the same purpose, this passage is like this
If i > 0:
x = 1
y = 2
In Python, the parentheses around the i > 0 are removed, the semicolon is stripped at the end of each statement, and the curly brackets of the block disappear.
Come out more if ... After: (colon), there is the indentation of x = 1 and y = 2 preceded by four spaces. By indenting, Python recognizes that both statements are subordinate to the IF.
The reason for the design of Python is simply that it is good for the program.
If statement
We write a complete program named ifdemo.py. This program is used to implement the IF structure.
i = 1
x = 1
If i > 0:
x = x+1
Print X
$python ifdemo.py # Run
When the program runs to if, the condition is true, so x = X+1 is executed.
The print x statement is not indented, so it is outside the IF.
If you change the first sentence to i =-1, then if you encounter a false value (false), x = x+1 is subordinate to if, this sentence skips. Print x does not indent, is outside of if, does not skip, continue execution.
This notation, which is indented in four spaces, indicates how the affiliation is written, and we will see it later. Python emphasizes the readability of the program. Forcing the indentation requires the programmer to write a neat program.
A more complex if selection:
i = 1
If i > 0:
print ' Positive i '
i = i + 1
elif i = = 0:
print ' I is 0 '
i = i * 10
Else
print ' Negative i '
i = I-1
print ' New I: ', I
Here are three blocks, respectively, with if, elif, else lead.
The python detection condition, if the condition of the if is found to be false, then skip the immediately following block, detect the next elif condition, if it is false, then execute the else block.
Through the structure above, the program is actually divided into three branches. The program executes only one of the three branches according to the conditions.
The entire if can be placed in another if statement, which is the nested use of the IF structure:
i = 5
If i > 1:
print ' I bigger than 1 '
print ' good '
If I > 2:
print ' I bigger than 2 '
print ' Even better '
The block after the If > 2 is indented with four spaces relative to the if, to indicate that it is subordinate to the IF, not the outer one.
Summarize
The colon after the IF statement
The affiliation is represented by the indentation of four spaces and cannot be indented in Python
If <条件1> :
Statement
Elif <条件2> :
Statement
Elif <条件3> :
Statement
Else
Statement