Home > Article > Backend Development > Common Python branch statement uses include:
Branch statements can be divided into single branch, two branch and multi-branch structures. Among all branches, only one path can be selected, and whether to execute is determined based on whether the branch condition is true or not. Since only one path can be selected for execution, the rules for establishing conditions for branch statements should be fully considered. The following are several examples to illustrate the use of branch statements.
The single branch structure is the simplest selection structure. The syntax structure is as follows:
if 条件表达式: 语句块
When the conditional expression is established , execute the statement block, if it is not established, it will not be executed. For example: the user inputs two numbers, compares their sizes, and outputs the smaller one.
num_a = int(input('please input a number:')) num_b = int(input('please input another number:')) if num_a > num_b: num_a, num_b = num_b, num_a #交换两个数 print("the smaller one is",num_a)
The above example uses a single branch structure. When the condition that number a is greater than number b is established, the two are exchanged, and finally the smaller number a is output. In a branch statement, it is executed only when the condition is true. Otherwise, no exchange is performed, and the statements after the branch statement are directly executed sequentially.
The two-branch structure adds an else statement on the basis of the single-branch structure. When the if condition is not established, the else statement is executed. The syntax structure is as follows:
if 条件表达式: 语句块1 else: 语句块2
The two-branch structure is a two-choice structure. One and only one of statement block 1 and statement block 2 will definitely be executed. Still the above example, using a two-branch statement can be written:
num_a = int(input('please input a number:')) num_b = int(input('please input another number:')) if num_a > num_b: print("the smaller one is",num_b) else: print("the smaller one is",num_a)
Another example, to determine the gender based on the ID number, can be written:
id_code = input('请输入身份证号码:') number = int(id_code[-2]) if number%2 == 0: print("女性") else: print("男性")
The else implicit condition here is number%2 = = 0 does not hold, that is, number is an odd number, so else can be replaced by if number%2 == 1. However, considering the execution efficiency of the program, the two if statements need to be judged twice, while the else statement does not need to be judged. , so it is more efficient.
The multi-branch structure is an extension of the two-branch structure, that is, a case of multiple selections. The else statement is optional. When When else exists, one and only one branch will be executed. The grammatical structure is as follows:
if 条件表达式1: 语句块1 elif条件表达式2: 语句块2 … elif条件表达式n: 语句块n else: 语句块n+1
For example, let the user enter their height and weight to calculate their BMI index. BMI refers to body mass index, which is obtained by dividing weight (kg) by the square of height (m). There is an internationally accepted measurement standard:
##Too light |
##Less than 18.5
|
Normal | ##18.5-25 (not included)|
25- 30 (not included) |
|
##30-35(not included) |
|
重度肥胖 |
35及以上 |
weight = float(input("请输入你的体重(Kg):")) height = float(input("请输入你的身高(m):")) BMI = weight / height ** 2 if BMI < 0: print("输入错误") elif BMI < 18.5 : print("偏瘦") elif BMI < 25 : print("正常") elif BMI < 30 : print("偏胖") elif BMI < 35 : print("肥胖") else: print("重度肥胖")
上例通过计算得到BMI指数,根据其值输出所对应的“档位”,因为在设定分支条件时应当注意每个分支条件之间没有重复区域,才能保证输出结果为其中一种。
在分支语句中如果要做进一步的条件判断,就会用到嵌套的分支结构。嵌套也可以有多层,通过缩进来表示其包含关系。
代表性语法结构如下:
if 条件表达式1: … if条件表达式2: 语句块1 else: 语句块2 else: 语句块3
例如上例在做身份证号的性别判断时,如果要先对输入的身份证号合法性做基本检查,例如其位数是不是正确,则需要嵌套分支:
id_code = input('请输入身份证号码:') if len(id_code) == 18: number = int(id_code[-2]) if number%2 == 0: print("女性") else: print("男性") else: print("输入不合法")
The above is the detailed content of Common Python branch statement uses include:. For more information, please follow other related articles on the PHP Chinese website!