Python if..else语句

决策是计算机编程的最基本概念之一。 Python支持其他语言的通用流控制语句,但有所更改。的 if 控制语句是用于根据某些条件执行代码的最基本,最著名的语句之一。

在本文中, if Python陈述式。

巨蟒 if 声明编号

最基本的形式 if Python语句是:

if EXPRESSION:
  STATEMENT

if 该声明是 if 关键字后跟一个条件表达式。

EXPRESSION 以后(:)冒号。如果 EXPRESSION 评价 TrueSTATEMENT 将被执行。如果 EXPRESSION 返回值 False没事 STATEMENT 忽略了。 STATEMENT 任何语句,包括多个语句,甚至是嵌套语句 if 声明。如果您不想执行该语句, pass 声明。

STATEMENT 该块以缩进开始,以第一条未缩进的行结束。大多数人选择使用4个空格或2个空格缩进。官方Python代码样式指南建议每个缩进级别使用四个空格,并且不要在缩进中混用制表符和空格。

让我们看下面的示例脚本,该脚本检查特定数字是否大于5。

number = int(input('Enter a number: '))

if number > 5:
    print(number, 'is greater than 5.')

将代码保存到文件中,然后从命令行运行它。

python test.py

脚本提示输入数字。例如,如果输入10,则条件表达式的计算如下: True (10大于5),并且 print 该功能将被执行。

10 is greater than 5.

Python支持标准比较操作:

  • a == b -如果为真 ab 是一样的。
  • a != b -如果为真 ab 不相等。
  • a > b -如果为真 a 大于 b
  • a >= b -如果为真 a 等于或大于 b
  • a < b -如果为真 a 小于 b
  • a <= b -如果为真 a 等于或小于 b

您也可以使用 in 用于检查值是否存在可重复项(字符串,列表,元组,字典等)的关键字:

s = 'linuxize'
if 'ze' in s:
    print('True.')

这是使用字典的另一个示例:

d = {'a': 2, 'b': 4}
if 'a' in d:
    print('True.')

在字典中使用时, in 关键字检查字典是否具有特定键。

要否定条件表达式,请使用逻辑表达式 not 操作员:

number = int(input('Enter a number: '))

if not number < 5:
    print(number, 'is greater than 5.')

if..else 声明编号

喔喔 if..else 该语句评估条件并根据结果执行两个语句之一。

巨蟒 if..else 语句的格式为:

if EXPRESSION:
    STATEMENT1
else:
    STATEMENT2

如果 EXPRESSION 评价 TrueSTATEMENT1 将被执行。不然 EXPRESSION 返回值 FalseSTATEMENT2 将被执行。我只能有一个 else 语句中的子句。

else 关键字以(:)与冒号相同,并相应的缩进级别 if 关键字。

让我们添加 else 上一个脚本示例中的子句:

number = int(input('Enter a number: '))

if number > 5:
    print(number, 'is greater than 5.')
else:
    print(number, 'is equal or less than 5.')

运行代码并输入数字时,脚本将根据数字是大于,小于还是等于5来打印不同的消息。

if..elif..else 声明编号

elif 关键字是 else if

巨蟒 if..elif..else 语句的格式为:

if EXPRESSION1:
    STATEMENT1
elif: EXPRESSION2:
    STATEMENT2
else:
    STATEMENT3

如果 EXPRESSION1 评价 TrueSTATEMENTS1 将被执行。如果 EXPRESSION2 评价 TrueSTATEMENTS2 将被执行。如果没有表达式被求值 TrueSTATEMENTS3 将被执行。

elif 关键字以(:)与冒号相同,并相应的缩进级别 if 关键字。可以有一个或多个 elif 语句中的子句。的 else 子句是可选的。如果 else 不使用任何表达式,对所有表达式求值 False,不执行任何语句。

条件按顺序评估。当条件恢复时 True,其余条件不执行且程序控制 if 声明。

让我们添加 elif 上一个脚本的部分:

number = int(input('Enter a number: '))

if number > 5:
    print(number, 'is greater than 5.')
elif number < 5:
    print(number, 'is less than 5.')
else:
    print(number, 'is equal to 5.')

与大多数编程语言不同,Python不会 switchcase 声明。多个序列 elif 可以代替声明 switchcase

套料 if 声明编号

可以嵌套Python if 内的声明 if 声明。通常,您应该始终避免使用过多的缩进 elif 而不是嵌套 if 声明书

以下脚本提示输入三个数字,并输出最大的数字。

number1 = int(input('Enter the first number: '))
number2 = int(input('Enter the second number: '))
number3 = int(input('Enter the third number: '))

if number1 > number2:
    if number1 > number3:
        print(number1, 'is the largest number.')
    else:
        print(number3, 'is the largest number.')
else:
    if number2 > number3:
        print(number2, 'is the largest number.')
    else:
        print(number3, 'is the largest number.')

输出看起来像这样:

Enter the first number: 455 
Enter the second number: 567
Enter the third number: 354
567 is the largest number.

多个条件

逻辑上 orand 运算符允许您组合多个条件 if 声明。

这是脚本的另一个版本,该版本显示三个数字中的最大数字。在此版本中,嵌套 if 声明,使用逻辑 and 运算符和 elif

number1 = int(input('Enter the first number: '))
number2 = int(input('Enter the second number: '))
number3 = int(input('Enter the third number: '))

if number1 > number2 and number1 > number3:
    print(number1, 'is the largest number.')
elif number2 > number3 and number2 > number3:
    print(number2, 'is the largest number.')
else:
    print(number3, 'is the largest number.')

总结#

ifif..elseif..elif..else 语句允许您通过评估某些条件来控制Python执行流程。

如果您有任何疑问或反馈,请随时发表评论。