Home > Article > Backend Development > Python program branch structure example code analysis
Python The syntax format of the if statement is as follows:
if 76d55530246dcb42559e4adb6c3f53df
A statement block is a sequence of one or more statements executed after the if condition is met. The statements in the statement block are passed with The line if is indented to express the inclusion relationship. The if statement first evaluates the result value of the condition, and if the result is True, the sequence of statements in the statement block is executed, and control then passes to the next statement of the program. If the result is False, the statements in the statement block will be skipped.
if Whether the statement block in the statement is executed or not depends on conditional judgment. But no matter what the situation, control will go to the next statement at the same level after the if statement.
if The conditional part of the statement can use any statement or function that can produce True or False. The most common way to form judgment conditions is to use relational operators. Python The language has a total of 6 relational operators, including less than, less than or equal to, greater than or equal to, greater than, equal to, and not equal to.
Special attention, Pybon uses “=" to indicate assignment statements, and uses “==” to indicate equals.
Let’s use an example to better grasp the if statement.
Air pollution is a matter of great concern to society today, and PM2.5 is an important indicator to measure air pollution. PM2.5 refers to particulate matter in the atmosphere with a diameter less than or equal to 2.5 um that can enter the lungs. PM2.5 Particles are small in size, contain a large amount of toxic and harmful substances, stay in the atmosphere for a long time, and are transported over long distances. Therefore, they have a great impact on human health and the quality of the atmospheric environment. The current air quality level is classified as 6 based on the PM2.5 value. PM2.5 Values between 0~35 are excellent air quality, 35~75 is good, 75~115 is light pollution , 115~150 is moderate pollution, 150~250 is severe pollution, 250~500 is severe pollution.
A simplified version of the air quality standard adopts a three-level model: 0~35 is excellent, 35~75 is good, 75 and above for pollution. People may not care about the specific PM2.5 index value, but are more concerned about the air quality. The computer can issue air quality alerts by PM2.5 index classification.
The IPO description of this issue is as follows:
Input: PM2.5 value that receives external input
Processing: &emsp ; if PM2.5 value≥ 275, print air pollution warning if 35 ≤ PM2.5 Value618cce220b739fc92379d107cbd126a7 :
;Statement block 2>
Statement block
1
condition is met, statement block2 is the sequence of statements executed after if conditions are not met. The two-branch statement is used to distinguish two possibilities of conditions, namely True or
False, which form the execution path respectively. We use the if-else statement to improve the code of the previous example:
PM = eval(input("请输入 PM2.5 数值:")) if PM >= 75: print("空气存在污染,请小心!") else: print("空气没有污染,可以开展户外运动")
There is also a more concise expression of the two-branch structure, which is suitable for returning specific values through judgment. Value, the syntax format is as follows:
b3f790e75cece7406dad0d5afa023811if
41f9c8099d3aabef0bdcdb6df3ff67faelsee360f798593dced1ac38136a35c11f77 Among them, the expression 1/2
is generally a value of numeric type or string type. At this time, the code can be changed to:
PM = eval(input("请输入 PM2.5 数值:")) print("空气{}污染!".format("存在" if PM >= 75 else "没有"))
if-else 的紧凑结构非常适合对特殊值处理的情况,其他例子如下:
count = 2 print(count if count != 0 else "不存在")
2
count = 0 print(coutn if count != 0 else "不存在")
不存在
多分支结构:if-elif-else 语句
Python 的 if-elif-else 描述多分支结构,语句格式如下:
if da6609de41d6c7b464901c2013cda643:
2bf3f074588f7619a6666723021cf509
elif 3b19063b029fa80954a3ca7d64b82c67:
ee5ae3cef41dfec58b4bfe90356174b3
else:
42d4246f0cdfd60cdeda0a4a7d84adef
多分支结构是二分支结构的扩展,这种形式通常用于设置同一个判断条件的多条执行路径。
Python 依次评估寻找第一个结果为 True 的条件,执行该条件下的语句块,结束后跳过整个 if-elif-else 结构,执行后面的语句。如果没有任何条件成立,else 下面的语句块将被执行。else子句是可选的。
前面的例子通过多条独立的 if 语句对同一个变量 PM 进行判断,这种情况更适合多分支结构,改进后的代码如下:
PM = eval(input("请输入 PM2.5 数值:")) if 0 <= PM < 35: print("空气优质,快去户外运动!") elif 35 <= PM < 75: print("空气良好,适度户外运动") else: print("空气污染,请小心!")
The above is the detailed content of Python program branch structure example code analysis. For more information, please follow other related articles on the PHP Chinese website!