首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Python if、elif、else条件

  • 23-11-14 09:22
  • 3814
  • 10250
blog.csdn.net

Python if、elif、else条件

文章目录

  • Python if、elif、else条件
    • elif 条件
    • 嵌套的 if、elif、else 条件

默认情况下,脚本中的语句从第一个到最后一个按顺序执行。如果处理逻辑需要,可以通过两种方式改变顺序流程:

Python 使用if关键字实现决策控制。Python 有条件执行块的语法如下:

Syntax:

if [boolean expression]:
    statement1
    statement2
    ...
    statementN
  • 1
  • 2
  • 3
  • 4
  • 5

任何评估为True或False的布尔表达式都会出现在if关键字之后。使用:符号,并在表达式后按回车键,以增加的缩进开始一个块。一个或多个以相同缩进级别编写的语句将被执行if布尔表达式的计算结果为True。

要结束块,请减少缩进。块后的后续语句将在if条件之外执行。 以下示例演示了if条件。

Example: if Condition

price = 50

if price < 100:
    print("price is less than 100") 
  • 1
  • 2
  • 3
  • 4

Output

price is less than 100
  • 1

在上例中,表达式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) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Output

price*quantity is less than 500
price = 50
quantity = 5
  • 1
  • 2
  • 3

上图中,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) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Output

 print("quantity = ", quantity)
 ^
IdentationError: unexpected indent 
  • 1
  • 2
  • 3

与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.") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Output

No if block executed. 
  • 1

下面的示例演示了多个 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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Output

price is 100
  • 1

请注意,每个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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

如前所述,缩进块从:符号之后开始,在布尔表达式之后。当条件为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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Output

price is less than 100
  • 1

在上面的例子中,如果条件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]            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如果指定条件评估为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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Output

price is 100
  • 1

在上例中,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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Output

price is less than 100
  • 1

所有的 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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Output

 elif price == 100:
                    ^
IdentationError: unindent does not match any outer indentation level 
  • 1
  • 2
  • 3

嵌套的 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") 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Output

Amount is between 200 and 500
  • 1

《AUTOSAR谱系分解(ETAS工具链)》之总目录

注:本文转载自blog.csdn.net的PlutoZuo的文章"https://blog.csdn.net/PlutoZuo/article/details/133042106"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top