Python - if, elif, else 条件
2023-09-17 22:15:01
默认情况下,脚本中的语句从第一个到最后一个按顺序执行。如果处理逻辑需要,可以通过两种方式更改顺序流:
Python 使用 if
关键字来实现决策控制。Python 有条件地执行块的语法如下:
语法:
if [boolean expression]: statement1 statement2 ... statementN
任何计算结果为 True
或 False
的布尔表达式都显示在 if
关键字之后。使用:
符号并在表达式后按 Enter 键以增加缩进开始块。在布尔表达式计算结果为 True
if
,将执行一个或多个具有相同缩进级别的语句。
要结束块,请减少缩进。块之后的后续语句将在if
条件之外执行。 以下示例演示了if
条件。
price = 50
if price < 100:
print("price is less than 100")
输出:
price is less than 100
在上面的示例中,表达式的计算结果price < 100
True
,因此它将执行该块。 if
块从:
后的新行开始,if
条件下的所有语句都以增加的缩进(空格或制表符)开头。 上面,if
块只包含一个语句。下面的示例在 if 条件中包含多个语句。
price = 50
quantity = 5
if price*quantity < 500:
print("price*quantity is less than 500")
print("price = ", price)
print("quantity = ", quantity)
输出:
price*quantity is less than 500
price = 50
quantity = 5
上面,if 条件包含具有相同缩进的多个语句。如果所有语句都不在同一个缩进中,无论是空格还是制表符,那么它将引发一个IdentationError
。
price = 50
quantity = 5
if price*quantity < 500:
print("price is less than 500")
print("price = ", price)
print("quantity = ", quantity)
输出:
print("quantity = ", quantity)
^
IdentationError: unexpected indent
与条件具有相同缩进级别的语句if
将不会在 if 块中考虑。他们会考虑脱离if
条件。
price = 50
quantity = 5
if price*quantity < 100:
print("price is less than 500")
print("price = ", price)
print("quantity = ", quantity)
print("No if block executed.")
输出:
No if block executed.
下面的示例演示多个 if 条件。
price = 100
if price > 100:
print("price is greater than 100")
if price == 100:
print("price is 100")
if price < 100:
print("price is less than 100")
输出:
price is 100
请注意,每个 if
块都包含不同缩进中的语句,这是有效的,因为它们彼此不同。
注意: 建议使用 4 个空格或制表符作为默认缩进级别,以提高可读性。
else condition
与 if
语句一起,可以选择使用 else
条件来定义在 if
条件中的布尔表达式计算结果为 False
时要执行的备用语句块。
语法:
if [boolean expression]: statement1 statement2 ... statementN else: statement1 statement2 ... statementN
如前所述,缩进块在:
符号之后开始,在布尔表达式之后。当条件True
时,它将被执行。 我们还有另一个块,应该在if
条件为 False
时执行。 首先,通过退格键完成if
块并写else
,在新块前面添加:
符号以开始它,并在块中添加所需的语句。
price = 50
if price >= 100:
print("price is greater than 100")
else:
print("price is less than 100")
输出:
price is less than 100
在上面的例子中,if 条件price >= 100
是False
的,所以else
块将被执行。else 块还可以包含具有相同缩进的多个语句;否则,它将提高IndentationError
.
请注意,您不能有多个else
块,并且它必须是最后一个块。
elif条件
使用 elif
条件用于在if
条件之后或if
和else
条件之间包含多个条件表达式。
语法:
if [boolean expression]: [statements] elif [boolean expresion]: [statements] elif [boolean expresion]: [statements] else: [statements]
如果指定的条件的计算结果为 True
,则执行elif
块。
price = 100
if price > 100:
print("price is greater than 100")
elif price == 100:
print("price is 100")
elif price < 100:
print("price is less than 100")
输出:
price is 100
在上面的示例中,elif
条件在if
条件之后应用。 Python 将评估if
条件,如果它的计算结果为 False
,那么它将评估elif
块并执行表达式计算结果为 True
的elif
块。 如果多个elif
条件变得True
,则第一个elif
块将被执行。
下面的示例演示了 if、elif 和 else 条件。
price = 50
if price > 100:
print("price is greater than 100")
elif price == 100:
print("price is 100")
else price < 100:
print("price is less than 100")
输出:
price is less than 100
所有 if、elif 和 else 条件必须从相同的缩进级别开始,否则将提高IndentationError
。
price = 50
if price > 100:
print("price is greater than 100")
elif price == 100:
print("price is 100")
else price < 100:
print("price is less than 100")
输出:
elif price == 100:
^
IdentationError: unindent does not match any outer indentation level
嵌套的if,elif,else条件
Python 支持嵌套的 if、elif 和 else 条件。内部条件的缩进必须比外部条件增加,并且一个块下的所有语句都应具有相同的缩进。
price = 50
quantity = 5
amount = price*quantity
if amount > 100:
if amount > 500:
print("Amount is greater than 500")
else:
if amount <= 500 and amount >= 400:
print("Amount is between 400 and 500")
elif amount <= 400 and amount >= 300:
print("Amount is between 300 and 400")
else:
print("Amount is between 200 and 300")
elif amount == 100:
print("Amount is 100")
else:
print("Amount is less than 100")
输出:
Amount is between 200 and 500