Python if、elif、else条件

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

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

Syntax:

if [boolean expression]:
    statement1
    statement2
    ...
    statementN

任何评估为TrueFalse的布尔表达式都会出现在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 >= 100False,那么将执行else块。else 块还可以包含多个缩进相同的语句;否则会升高IndentationError

注意不能有多个else块,必须是最后一个块。

elif 条件

使用elif条件用于在if条件之后或在ifelse条件之间包含多个条件表达式。

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块并执行表达式评估为Trueelif块。 如果多个elif条件变为True,则执行第一个elif块。

以下示例演示 ifelifelse条件。

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

所有的 ifelifelse条件必须从相同的缩进级别开始,否则会提升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 支持嵌套的 ifelifelse条件。内部条件必须比外部条件具有更大的缩进,并且一个块下的所有语句都应该具有相同的缩进。

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

原创文章,作者:简单一点,如若转载,请注明出处:https://www.506064.com/n/129671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
简单一点简单一点
上一篇 2024-10-03 23:26
下一篇 2024-10-03 23:26

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29

发表回复

登录后才能评论