📜  Python中的三元运算符

📅  最后修改于: 2022-05-13 01:55:29.934000             🧑  作者: Mango

Python中的三元运算符

三元运算符也称为条件表达式,是根据条件为真或假来评估某些内容的运算符。它在 2.5 版中被添加到Python中。
它只是允许在单行中测试条件,替换多行 if-else,使代码紧凑。

句法 :

[on_true] if [expression] else [on_false] 
  • 使用三元运算符的简单方法:
Python
# Program to demonstrate conditional operator
a, b = 10, 20
 
# Copy value of a in min if a < b else copy b
min = a if a < b else b
 
print(min)


Python
# Python program to demonstrate ternary operator
a, b = 10, 20
 
# Use tuple for selecting an item
# (if_test_false,if_test_true)[test]
# if [a


Python
# Python program to demonstrate nested ternary operator
a, b = 10, 20
 
print ("Both a and b are equal" if a == b else "a is greater than b"
        if a > b else "b is greater than a")


Python
# Python program to demonstrate nested ternary operator
a, b = 10, 20
 
if a != b:
    if a > b:
        print("a is greater than b")
    else:
        print("b is greater than a")
else:
    print("Both a and b are equal")


Python3
a=5
b=7
 
# [statement_on_True] if [condition] else [statement_on_false]
 
print(a,"is greater") if (a>b) else print(b,"is Greater")


Python
# Program to demonstrate conditional operator
a, b = 10, 20
 
# If a is less than b, then a is assigned
# else b is assigned (Note : it doesn't
# work if a is 0.
min = a < b and a or b
 
print(min)


输出:

10
  • 使用元组、字典和 lambda的直接方法

Python

# Python program to demonstrate ternary operator
a, b = 10, 20
 
# Use tuple for selecting an item
# (if_test_false,if_test_true)[test]
# if [a

输出:

10
10
10
  • 三元运算符可以写成嵌套的 if-else:

Python

# Python program to demonstrate nested ternary operator
a, b = 10, 20
 
print ("Both a and b are equal" if a == b else "a is greater than b"
        if a > b else "b is greater than a")

上面的方法可以写成:

Python

# Python program to demonstrate nested ternary operator
a, b = 10, 20
 
if a != b:
    if a > b:
        print("a is greater than b")
    else:
        print("b is greater than a")
else:
    print("Both a and b are equal")

输出:

b is greater than a
  • 要在三元运算符中使用打印函数,例如:-

示例:在 python3 中使用三元运算符查找 2 中的较大数

Python3

a=5
b=7
 
# [statement_on_True] if [condition] else [statement_on_false]
 
print(a,"is greater") if (a>b) else print(b,"is Greater")

输出:

7 is Greater

要点:

  • 首先评估给定条件 (a < b),然后根据条件返回的布尔值返回 a 或 b
  • 运算符中参数的顺序与其他语言(如 C/C++)不同(请参阅 C/C++ 三元运算符)。
  • 在所有Python操作中,条件表达式的优先级最低。

三元运算符不存在时,在 2.5 之前使用的方法
在如下给出的表达式中,解释器检查表达式是否为真,然后评估 on_true,否则评估 on_false。

句法 :

'''When condition becomes true, expression [on_false]
   is not executed and value of "True and [on_true]"
   is returned.  Else value of "False or [on_false]"
   is returned.
   Note that "True and x" is equal to x. 
   And "False or x" is equal to x. '''
[expression] and [on_true] or [on_false] 

例子 :

Python

# Program to demonstrate conditional operator
a, b = 10, 20
 
# If a is less than b, then a is assigned
# else b is assigned (Note : it doesn't
# work if a is 0.
min = a < b and a or b
 
print(min)

输出:

10

注意:此方法的唯一缺点是on_true 不能为零或 False 。如果发生这种情况,将始终评估 on_false。原因是如果表达式为真,解释器将检查 on_true,如果它为零或假,这将迫使解释器检查 on_false 以给出整个表达式的最终结果。