Python if、elif、else条件
Python if、elif、else条件
默认情况下,脚本中的语句从第一个到最后一个按顺序执行。如果处理逻辑需要,可以通过两种方式改变顺序流程:
Python 使用if关键字实现决策控制。Python 有条件执行块的语法如下:
Syntax:
if [boolean expression]: statement1 statement2 ... statementN
任何评估为True或False的布尔表达式都会出现在if关键字之后。使用:符号,并在表达式后按回车键,以增加的缩进开始一个块。一个或多个以相同缩进级别编写的语句将被执行if布尔表达式的计算结果为True。
要结束块,请减少缩进。块后的后续语句将在if条件之外执行。 以下示例演示了if条件。
Example: if Condition
price = 50 if price < 100: print("price is less than 100")
Output
price is less than 100
在上例中,表达式price < 100的计算结果为True,因此它将执行该块。 if块从:之后的新行开始,并且if条件下的所有语句都以增加的缩进开始,无论是空格还是制表符。 以上,if块只包含一条语句。下面的示例在 if 条件中有多个语句。
Example: Multiple Statements in the if Block
price = 50 quantity = 5 if price*quantity < 500: print("price*quantity is less than 500") print("price = ", price) print("quantity = ", quantity)
Output
price*quantity is less than 500 price = 50 quantity = 5
上图中,if 条件包含多个缩进相同的语句。如果所有语句都不在同一个缩进中,无论是空格还是制表符,那么它都会引发IdentationError。
Example: Invalid Indentation in the Block
price = 50 quantity = 5 if price*quantity < 500: print("price is less than 500") print("price = ", price) print("quantity = ", quantity)
Output
print("quantity = ", quantity) ^ IdentationError: unexpected indent
与if条件具有相同缩进级别的语句将不在 if 块中考虑。他们会考虑退出if状态。
Example: Out of Block Statements
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.")
Output
No if block executed.
下面的示例演示了多个 if 条件。
Example: Multiple if Conditions
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")
Output
price is 100
请注意,每个if块包含不同缩进的语句,这是有效的,因为它们彼此不同。
*Note*It is recommended to use 4 spaces or a tab as the default indentation level for more readability. *## 其他条件
如果if条件中的布尔表达式计算结果为False,则else条件可以与if语句一起用于定义要执行的替代语句块。
Syntax:
if [boolean expression]: statement1 statement2 ... statementN else: statement1 statement2 ... statementN
如前所述,缩进块从:符号之后开始,在布尔表达式之后。当条件为True时执行。 当if条件为False时,我们还有另一个块需要执行。 首先用退格完成if块并写else,在新块前面加上:符号开始,并在块中加上所需语句。
Example: else Condition
price = 50 if price >= 100: print("price is greater than 100") else: print("price is less than 100")
Output
price is less than 100
在上面的例子中,如果条件price >= 100是False,那么将执行else块。else 块还可以包含多个缩进相同的语句;否则会升高IndentationError。
注意不能有多个else块,必须是最后一个块。
elif 条件
使用elif条件用于在if条件之后或在if和else条件之间包含多个条件表达式。
Syntax:
if [boolean expression]: [statements] elif [boolean expresion]: [statements] elif [boolean expresion]: [statements] else: [statements]
如果指定条件评估为True,则执行elif块。
Example: if-elif Conditions
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")
Output
price is 100
在上例中,elif条件在if条件之后应用。 Python 将评估if条件,如果评估为False,则评估elif块并执行表达式评估为True的elif块。 如果多个elif条件变为True,则执行第一个elif块。
以下示例演示 if、elif、else条件。
Example: if-elif-else Conditions
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")
Output
price is less than 100
所有的 if、elif、else条件必须从相同的缩进级别开始,否则会提升IndentationError。
Example: Invalid Indentation
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")
Output
elif price == 100: ^ IdentationError: unindent does not match any outer indentation level
嵌套的 if、elif、else 条件
Python 支持嵌套的 if、elif、else条件。内部条件必须比外部条件具有更大的缩进,并且一个块下的所有语句都应该具有相同的缩进。
Example: Nested if-elif-else Conditions
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") elif amount < 500 and amount > 300: print("Amount is between 300 and 500") else: print("Amount is between 200 and 500") elif amount == 100: print("Amount is 100") else: print("Amount is less than 100")
Output
Amount is between 200 and 500
评论关闭