Home > Article > Backend Development > Python - indentation and selection
Indentation
The most distinctive feature of Python is using indentation to write modules. Let's take the if selection structure as an example below. if is followed by a condition, and if the condition is true, some statements belonging to if are executed.
Let’s first look at the expression in C language (note, this is C, not Python!)
if ( i > 0 )
{
x = 1;
y = 2;
}
This statement means that if i>1, we will perform the two assignment operations included in the brackets.
Contained in parentheses are block operations, which indicate that the statements within them belong to if.
In Python, for the same purpose, this passage is like this
if i > 0:
x = 1
y = 2
In Python, the brackets around i > 0 are removed, The semicolon at the end of each statement has been removed, and the curly braces denoting blocks have disappeared.
There is an extra: (colon) after if..., and there are four spaces indent before x = 1 and y =2. Through indentation, Python recognizes that these two statements belong to if.
The reason why Python is designed this way is very simple, just to make the program look good.
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 # When running the
program to if, the condition is True, Therefore x = x+1, is executed.
The print x statement is not indented, so it is outside the if.
If the first sentence is changed to i = -1, then if encounters a false value (False), x = x+1 belongs to if, and this sentence is skipped. print x has no indentation, is outside if, does not skip, and continues execution.
This writing method of using four spaces indentation to indicate affiliation is something we will see again in the future. Python places great emphasis on program readability. The requirement to force indentation allows programmers to write clean programs.
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
There are three blocks here, starting with if, elif, else leads.
Python detects conditions. If the condition of if is found to be false, skip the following block and detect the condition of the next elif; if it is still false, execute the else block.
Through the above structure, the program is actually divided into three branches. The program only executes one of the three branches based on conditions.
The entire if can be placed in another if statement, which is the nested use of 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'
if i > 2 The following block is indented four spaces relative to the if to indicate that it belongs to the if. Instead of the outer if.
Summary
The colon
after the if statement is indented with four spaces to indicate affiliation. In Python, indentation is not allowed arbitrarily
if :
statement
elif :
statement
elif :
statement
else:
statement