First, sequential structure
The sequential structure is the simplest kind of program structure, and the program executes from top to bottom in the order in which the statements are written.
Two, branch control statements
A python conditional statement determines the code block that executes by executing the result of one or more statements (true or false).
1. If statement
The general form of the IF statement in Python is as follows:
if condition_1: statement_block_1elif condition_2: statement_block_2Else : statement_block_3
- If "condition_1" is True, the "statement_block_1" block statement is executed
- If "condition_1" is false, the "condition_2" will be judged
- If "Condition_2" is True, the "statement_block_2" block statement is executed
- If "Condition_2" is false, the "STATEMENT_BLOCK_3" block statement is executed
Python replaces the else ifwith elif , so the keyword for the IF statement is:if–elif–else.
Attention:
- 1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.
- 2, using indentation to divide the statement block, the same indentation number of statements together to form a block of statements.
- 3. There are no switch–case statements in Python.
2. If nesting
In a nested if statement, the IF...ELIF...ELSE structure can be placed in another if...elif...else structure.
if expression 1: statement if expression 2: statement elif expression 3: statement else statement elif expression 4: statement Else: statement
Third, cycle programming
1. For statement
A Python for loop can traverse any sequence of items, such as a list or a string.
The general format for the For loop is as follows:
for in <sequence>: <statements>Else: <statements>
2. While loop
The general form of a while statement in Python:
while Judging Condition: statement
3. Break and continue statements and ELSE clauses in loopsThe break statement can jump out of a for and while loop body. If you terminate from a for or while loop, any corresponding loop else blocks will not execute. 4. Pass Statement
The Python Pass is an empty statement in order to maintain the integrity of the program structure.
Pass does not do anything, generally used as a placeholder statement,
Python conditional Statements and loop statements