Basics of Python programming: program flow control (1)

1. Conditional expressions

There are mainly three basic structures of program flow, sequence structure, selection structure and loop structure. In the sequence structure, each statement is executed in sequence according to the position of the statement. Each statement is executed only once. In the selection structure and loop structure, , usually it is necessary to determine the next step of the execution process according to the value of the conditional expression.
Two types of operators are generally used in conditional expressions: relational operators and logical operators.

1. Relational operators

Relational operators in the Python language are used to represent the relationship between different variables. The biggest feature is that they can be used together, similar to the writing method in mathematics. The premise of using them is that the left and right sides of the operator can be compared. If they cannot be compared, an error will be reported. The relational operators in the Python language are as follows:

operator describe
> more than the
< less than
== equal
!= not equal to
>= greater or equal to
<= less than or equal to

insert image description here

2. Logical operators

Complex conditional expressions use the logical operators and (and), or (or), and not (not).
insert image description here

3. Conditional expressions

Use the different operators above for building different conditional expressions.
example:

m % 3 == 0 and m % 2 == 0          # 表示m既是2的倍数,也是3的倍数
( a + b > c and a + c > b and b + c > a)          # 表示a、b、c作为三条边时能构成一个三角形
x % 2 == 1         # 表示整数x为一个奇数

2. Select structure

The program makes different processing according to different situations, and then causes the program to branch. This structure is called a selection structure, also called a branch structure or a conditional structure. In this structure, some statements in the program are not necessarily every Each operation is executed, and the program will choose different execution statements according to different situations.

1. Single-branch structure if statement

if 条件表达式:
   语句块

Indicates that if the conditional expression is met and the result is True, the statement block will be executed; otherwise, the statement block will be skipped and the following program will be executed. Among them, ":" cannot be omitted, which means the beginning of a statement block. In addition, attention should be paid to tightening. The same statement block must maintain the same amount of tightening.
insert image description here
In addition, similar to the relatively short statement block in the above figure, it can also be written directly after the if conditional expression, for example:

if a < b:
   print("a<b")

can also be written as:

if a < b: print("a<b")

2. Double branch structure if-else statement

if 条件表达式:
   语句块1
else:
   语句块2

Indicates that if conditional expression 1 is satisfied and the result is True, statement block 1 will be executed; otherwise, statement block 2 will be executed, that is, statement block 1 and statement block 2 can be selected to be executed.
insert image description here
There is also a concise expression format for the double branch structure, the format is as follows:

语句1 if 条件表达式1 else 语句2

3. Multi-branch structure if-elif-else statement

if 条件表达式1:
   语句块1
elif 条件表达式2:
   语句块2
elif 条件表达式3:
   语句块3
......
else:
   语句块n

Where elif is the abbreviation of else if.
insert image description here

4. Nested if structure

In the if selection structure, if the statement block itself is also an if statement, then a nested structure of if statements is formed.

if 条件表达式1:
   if 条件表达式2:
      语句块1
   else:
      语句块2
else:
   语句块3

Similar to the previous one, the above means that when the conditional expression 1 is satisfied, it is judged whether the conditional expression 2 is satisfied, and if it is satisfied, the statement block 1 is executed, otherwise the statement block 2 is executed, and when the conditional expression 1 is not satisfied, the statement block is executed 3.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42051846/article/details/131391546