Blank space is important in Python. In fact, the blank at the beginning of the line is important. It is called indentation. The blank spaces (spaces and tabs) at the beginning of the logical line are used to determine the indentation level of the logical line and determine the grouping of statements. Indent
The most distinctive feature of Python is that it uses indentation to Mark block-based code. The following uses the if structure as an example. If followed by a condition, if the condition is true, a code block belonging to if is executed.
First look at the expression of C language (note that this is C, not Python !)
The code is as follows:
If (I> 0)
{
X = 1;
Y = 2;
}
If I> 0, we will assign values to the two values included in the brackets. The brackets contain block operations, which are affiliated with if.
In Python, this is the same purpose.
The code is as follows:
If I> 0:
X = 1
Y = 2
In Python, the parentheses around I> 0 are removed, and the semicolons at the end of each statement are removed, indicating that the curly braces of the block are also removed.
After if... is added, there are four spaces in front of x = 1 and y = 2. Through indentation, Python recognizes that these two statements are affiliated with if.
The reason for the design of Python is purely for the good-looking program.
If statement
Write a complete program named ifDemo. py. This program is used to implement the if structure.
The code is as follows:
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 execute x = x + 1 ,.
The print x statement is not indented, so it is out of the if statement.
If you change the first sentence to I =-1, if encounters a False value (False), x = x + 1 belongs to if, and this sentence is skipped. Print x is not indented. it is outside of if and will not be skipped. continue execution.
This writing method uses four spaces to show the affiliation. Forced indentation enhances program readability.
Complex if options:
The code is as follows:
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 there are three blocks, which belong to if, elif, and else.
Python Check Condition. if the condition of if is false, skip the block followed by it and check the condition of the next elif. if the condition is false, execute the else block.
The above structure separates the program into three branches. The program executes only one of the three branches according to the conditions.
The entire if statement can be placed in another if statement, that is, the nested use of the if structure:
The code is as follows:
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 if I> 2 is indented by four spaces relative to the if to indicate that it belongs to the if rather than the outer if.
Summary
Colon after if statement
It indicates the affiliation with four spaces. it cannot be indented at will in Python.
The code is as follows:
If <条件1> :
Statement
Elif <条件2> :
Statement
Elif <条件3> :
Statement
Else:
Statement