Python是否具有三元条件运算符?

问题:Python是否具有三元条件运算符?

如果Python没有三元条件运算符,是否可以使用其他语言构造来模拟一个?

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?


回答 0

是的,它是在2.5版中添加的。表达式语法为:

a if condition else b

第一condition被评估,则恰好中的任一个ab进行评估并返回基于所述布尔condition。如果conditionTruea则被评估并返回但b被忽略,否则b被评估为返回但a被忽略。

这允许发生短路,因为condition仅当a评估为true时才进行评估,而b根本不评估,而当condition为false 评估时,仅b评估时a就完全不评估。

例如:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

请注意,条件是表达式,而不是语句。这意味着您不能在条件表达式中使用赋值语句pass或其他语句

>>> pass if False else x = 3
  File "<stdin>", line 1
    pass if False else x = 3
          ^
SyntaxError: invalid syntax

但是,您可以使用条件表达式来分配变量,如下所示:

x = a if True else b

将条件表达式视为在两个值之间切换。当您处于“一个价值或另一个价值”的情况下,它非常有用,但是它没有做其他的事情。

如果需要使用语句,则必须使用普通if 语句而不是条件表达式


请记住,由于某些原因,某些Pythonista对此并不满意:

  • 参数的顺序与condition ? a : b其他许多语言(例如C,C ++,Go,Perl,Ruby,Java,Javascript等)中的经典三元运算符的顺序不同,当人们不熟悉Python的“令人惊讶的”行为使用它(它们可能会颠倒参数顺序)。
  • 有些人认为它“笨拙”,因为它与正常的思维流程相反(先思考条件,然后思考效果)。
  • 风格上的原因。(尽管“内联if”可能确实有用,并且可以使脚本更简洁,但确实会使代码复杂化)

如果您在记住顺序时遇到麻烦,请记住当大声朗读时,您(几乎)说出了您的意思。例如,x = 4 if b > 8 else 9将朗读为x will be 4 if b is greater than 8 otherwise 9

官方文件:

Yes, it was added in version 2.5. The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Note that conditionals are an expression, not a statement. This means you can’t use assignment statements or pass or other statements within a conditional expression:

>>> pass if False else x = 3
  File "<stdin>", line 1
    pass if False else x = 3
          ^
SyntaxError: invalid syntax

You can, however, use conditional expressions to assign a variable like so:

x = a if True else b

Think of the conditional expression as switching between two values. It is very useful when you’re in a ‘one value or another’ situation, it but doesn’t do much else.

If you need to use statements, you have to use a normal if statement instead of a conditional expression.


Keep in mind that it’s frowned upon by some Pythonistas for several reasons:

  • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, Javascript, etc.), which may lead to bugs when people unfamiliar with Python’s “surprising” behaviour use it (they may reverse the argument order).
  • Some find it “unwieldy”, since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
  • Stylistic reasons. (Although the ‘inline if‘ can be really useful, and make your script more concise, it really does complicate your code)

If you’re having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

Official documentation:


回答 1

您可以索引到一个元组:

(falseValue, trueValue)[test]

test需要返回TrueFalse
始终将其实现为:

(falseValue, trueValue)[test == True]

或者您可以使用内置函数bool()来确保布尔值:

(falseValue, trueValue)[bool(<expression>)]

You can index into a tuple:

(falseValue, trueValue)[test]

test needs to return True or False.
It might be safer to always implement it as:

(falseValue, trueValue)[test == True]

or you can use the built-in bool() to assure a Boolean value:

(falseValue, trueValue)[bool(<expression>)]

回答 2

对于2.5之前的版本,有个窍门:

[expression] and [on_true] or [on_false]

on_true 布尔值错误时,可能会给出错误的结果。1
尽管这样做确实有好处,从左到右评估表达式,我认为这很清楚。

1. 是否有C的等价的“?:”三元运算符?

For versions prior to 2.5, there’s the trick:

[expression] and [on_true] or [on_false]

It can give wrong results when on_true has a false boolean value.1
Although it does have the benefit of evaluating expressions left to right, which is clearer in my opinion.

1. Is there an equivalent of C’s ”?:” ternary operator?


回答 3

<expression 1> if <condition> else <expression 2>

a = 1
b = 2

1 if a > b else -1 
# Output is -1

1 if a > b else -1 if a < b else 0
# Output is -1

<expression 1> if <condition> else <expression 2>

a = 1
b = 2

1 if a > b else -1 
# Output is -1

1 if a > b else -1 if a < b else 0
# Output is -1

回答 4

文档中

条件表达式(有时称为“三元运算符”)在所有Python操作中具有最低的优先级。

表达式x if C else y首先计算条件Cnot x);如果C为true,则对x求值并返回其值;否则,将评估y并返回其值。

有关条件表达式的更多详细信息,请参见PEP 308

从2.5版开始新增。

From the documentation:

Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.

The expression x if C else y first evaluates the condition, C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

See PEP 308 for more details about conditional expressions.

New since version 2.5.


回答 5

作为Python增强建议308的一部分,2006年添加了Python条件表达式的运算符。它的形式与普通?:运算符不同,它是:

<expression1> if <condition> else <expression2>

等效于:

if <condition>: <expression1> else: <expression2>

这是一个例子:

result = x if a > b else y

可以使用的另一种语法(与2.5之前的版本兼容):

result = (lambda:y, lambda:x)[a > b]()

懒惰求操作数的地方。

另一种方法是通过索引元组(与大多数其他语言的条件运算符不一致):

result = (y, x)[a > b]

或显式构造的字典:

result = {True: x, False: y}[a > b]

另一种(不太可靠)但更简单的方法是使用andor运算符:

result = (a > b) and x or y

但是如果x这样的话不会起作用False

可能的解决方法是按如下所示制作xy列出或元组:

result = ((a > b) and [x] or [y])[0]

要么:

result = ((a > b) and (x,) or (y,))[0]

如果您使用的是字典,则可以使用,而不是使用三元条件,get(key, default)例如:

shell = os.environ.get('SHELL', "/bin/sh")

资料来源:?:维基百科中的Python

An operator for a conditional expression in Python was added in 2006 as part of Python Enhancement Proposal 308. Its form differ from common ?: operator and it’s:

<expression1> if <condition> else <expression2>

which is equivalent to:

if <condition>: <expression1> else: <expression2>

Here is an example:

result = x if a > b else y

Another syntax which can be used (compatible with versions before 2.5):

result = (lambda:y, lambda:x)[a > b]()

where operands are lazily evaluated.

Another way is by indexing a tuple (which isn’t consistent with the conditional operator of most other languages):

result = (y, x)[a > b]

or explicitly constructed dictionary:

result = {True: x, False: y}[a > b]

Another (less reliable), but simpler method is to use and and or operators:

result = (a > b) and x or y

however this won’t work if x would be False.

A possible workaround is to make x and y lists or tuples as in the following:

result = ((a > b) and [x] or [y])[0]

or:

result = ((a > b) and (x,) or (y,))[0]

If you’re working with dictionaries, instead of using a ternary conditional, you can take advantage of get(key, default), for example:

shell = os.environ.get('SHELL', "/bin/sh")

Source: ?: in Python at Wikipedia


回答 6

不幸的是,

(falseValue, trueValue)[test]

解决方案没有捷径;因此falseValuetrueValue无论条件如何,都对和进行评估。这可能不是最理想的,甚至可能是错误的(即两者兼有,trueValue并且falseValue可能是方法并且有副作用)。

一种解决方案是

(lambda: falseValue, lambda: trueValue)[test]()

(执行会延迟到知道获胜者为止;),但是会在可调用对象和不可调用对象之间引入不一致。此外,使用属性时无法解决问题。

故事就这样了-在提到的3个解决方案之间进行选择是要在具有短路功能,至少使用Зython2.5(恕我直言,不再是问题)与不易于出现“ trueValue-evaluates-to-false”错误之间进行权衡。

Unfortunately, the

(falseValue, trueValue)[test]

solution doesn’t have short-circuit behaviour; thus both falseValue and trueValue are evaluated regardless of the condition. This could be suboptimal or even buggy (i.e. both trueValue and falseValue could be methods and have side-effects).

One solution to this would be

(lambda: falseValue, lambda: trueValue)[test]()

(execution delayed until the winner is known ;)), but it introduces inconsistency between callable and non-callable objects. In addition, it doesn’t solve the case when using properties.

And so the story goes – choosing between 3 mentioned solutions is a trade-off between having the short-circuit feature, using at least Зython 2.5 (IMHO not a problem anymore) and not being prone to “trueValue-evaluates-to-false” errors.


回答 7

不同编程语言的三元运算符

在这里,我只是试图展示ternary operator几种编程语言之间的一些重要区别。

Javascript中的三元运算符

var a = true ? 1 : 0;
# 1
var b = false ? 1 : 0;
# 0

Ruby中的三元运算符

a = true ? 1 : 0
# 1
b = false ? 1 : 0
# 0

Scala中的三元运算符

val a = true ? 1 | 0
# 1
val b = false ? 1 | 0
# 0

R编程中的三元运算符

a <- if (TRUE) 1 else 0
# 1
b <- if (FALSE) 1 else 0
# 0

Python中的三元运算符

a = 1 if True else 0
# 1
b = 1 if False else 0
# 0

Ternary Operator in different programming Languages

Here I just try to show some important difference in ternary operator between a couple of programming languages.

Ternary Operator in Javascript

var a = true ? 1 : 0;
# 1
var b = false ? 1 : 0;
# 0

Ternary Operator in Ruby

a = true ? 1 : 0
# 1
b = false ? 1 : 0
# 0

Ternary operator in Scala

val a = true ? 1 | 0
# 1
val b = false ? 1 | 0
# 0

Ternary operator in R programming

a <- if (TRUE) 1 else 0
# 1
b <- if (FALSE) 1 else 0
# 0

Ternary operator in Python

a = 1 if True else 0
# 1
b = 1 if False else 0
# 0

回答 8

对于Python 2.5及更高版本,有一种特定的语法:

[on_true] if [cond] else [on_false]

在较旧的Python中,未实现三元运算符,但可以对其进行仿真。

cond and on_true or on_false

不过,有一个潜在的问题,如果cond计算结果为True,并on_true评估为Falseon_false返回来代替on_true。如果您想要这种行为,该方法可以,否则请使用以下方法:

{True: on_true, False: on_false}[cond is True] # is True, not == True

可以用以下方法包装:

def q(cond, on_true, on_false)
    return {True: on_true, False: on_false}[cond is True]

并以这种方式使用:

q(cond, on_true, on_false)

它与所有Python版本兼容。

For Python 2.5 and newer there is a specific syntax:

[on_true] if [cond] else [on_false]

In older Pythons a ternary operator is not implemented but it’s possible to simulate it.

cond and on_true or on_false

Though, there is a potential problem, which if cond evaluates to True and on_true evaluates to False then on_false is returned instead of on_true. If you want this behavior the method is OK, otherwise use this:

{True: on_true, False: on_false}[cond is True] # is True, not == True

which can be wrapped by:

def q(cond, on_true, on_false)
    return {True: on_true, False: on_false}[cond is True]

and used this way:

q(cond, on_true, on_false)

It is compatible with all Python versions.


回答 9

您可能经常会发现

cond and on_true or on_false

但这会在on_true == 0时导致问题

>>> x = 0
>>> print x == 0 and 0 or 1 
1
>>> x = 1
>>> print x == 0 and 0 or 1 
1

您期望普通三元运算符得到的结果

>>> x = 0
>>> print 0 if x == 0 else 1 
0
>>> x = 1
>>> print 0 if x == 0 else 1 
1

You might often find

cond and on_true or on_false

but this lead to problem when on_true == 0

>>> x = 0
>>> print x == 0 and 0 or 1 
1
>>> x = 1
>>> print x == 0 and 0 or 1 
1

where you would expect for a normal ternary operator this result

>>> x = 0
>>> print 0 if x == 0 else 1 
0
>>> x = 1
>>> print 0 if x == 0 else 1 
1

回答 10

Python是否具有三元条件运算符?

是。从语法文件

test: or_test ['if' or_test 'else' test] | lambdef

感兴趣的部分是:

or_test ['if' or_test 'else' test]

因此,三元条件运算的形式为:

expression1 if expression2 else expression3

expression3将被懒惰地求值(即,仅expression2在布尔上下文中为false 时才求值)。而且由于递归定义,您可以无限地链接它们(尽管它可能被认为是不好的样式。)

expression1 if expression2 else expression3 if expression4 else expression5 # and so on

使用注意事项:

请注意,每个if之后都必须带有else。人们在学习列表理解和生成器表达式时可能会发现这是一个很难学习的课-以下内容将不起作用,因为Python期望将其他表达式用作第三个表达式:

[expression1 if expression2 for element in iterable]
#                          ^-- need an else here

这引发了一个SyntaxError: invalid syntax。因此,以上内容要么是一个不完整的逻辑(也许用户期望在错误条件下不进行操作),要么是打算将expression2用作过滤器-请注意,以下内容是合法的Python:

[expression1 for element in iterable if expression2]

expression2用作列表理解的过滤器,而不是三元条件运算符。

较窄情况的替代语法:

您可能会发现编写以下内容有些痛苦:

expression1 if expression1 else expression2

expression1对于上述用法,将必须进行两次评估。如果它只是局部变量,则可以限制冗余。但是,此用例的常见且高性能的Python习惯用法是使用or的快捷方式行为:

expression1 or expression2

这在语义上是等效的。请注意,某些样式指南可能出于清楚的原因而限制了此用法-它确实将很多含义包含在很少的语法中。

Does Python have a ternary conditional operator?

Yes. From the grammar file:

test: or_test ['if' or_test 'else' test] | lambdef

The part of interest is:

or_test ['if' or_test 'else' test]

So, a ternary conditional operation is of the form:

expression1 if expression2 else expression3

expression3 will be lazily evaluated (that is, evaluated only if expression2 is false in a boolean context). And because of the recursive definition, you can chain them indefinitely (though it may considered bad style.)

expression1 if expression2 else expression3 if expression4 else expression5 # and so on

A note on usage:

Note that every if must be followed with an else. People learning list comprehensions and generator expressions may find this to be a difficult lesson to learn – the following will not work, as Python expects a third expression for an else:

[expression1 if expression2 for element in iterable]
#                          ^-- need an else here

which raises a SyntaxError: invalid syntax. So the above is either an incomplete piece of logic (perhaps the user expects a no-op in the false condition) or what may be intended is to use expression2 as a filter – notes that the following is legal Python:

[expression1 for element in iterable if expression2]

expression2 works as a filter for the list comprehension, and is not a ternary conditional operator.

Alternative syntax for a more narrow case:

You may find it somewhat painful to write the following:

expression1 if expression1 else expression2

expression1 will have to be evaluated twice with the above usage. It can limit redundancy if it is simply a local variable. However, a common and performant Pythonic idiom for this use-case is to use or‘s shortcutting behavior:

expression1 or expression2

which is equivalent in semantics. Note that some style-guides may limit this usage on the grounds of clarity – it does pack a lot of meaning into very little syntax.


回答 11

模拟python三元运算符。

例如

a, b, x, y = 1, 2, 'a greather than b', 'b greater than a'
result = (lambda:y, lambda:x)[a > b]()

输出:

'b greater than a'

Simulating the python ternary operator.

For example

a, b, x, y = 1, 2, 'a greather than b', 'b greater than a'
result = (lambda:y, lambda:x)[a > b]()

output:

'b greater than a'

回答 12

三元条件运算符仅允许在单行中测试条件,从而代替多行if-else,从而使代码紧凑。

句法 :

如果[表达式],则为[on_true],否则为[on_false]

1-使用三元运算符的简单方法:

# 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)  # Output: 10

2-使用元组,字典和lambda的直接方法:

# Python program to demonstrate ternary operator
a, b = 10, 20
# Use tuple for selecting an item
print( (b, a) [a < b] )
# Use Dictionary for selecting an item
print({True: a, False: b} [a < b])
# lamda is more efficient than above two methods
# because in lambda  we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]()) # in output you should see three 10

3-三元运算符可以写为嵌套if-else:

# 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 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") 
# Output: b is greater than a

Ternary conditional operator simply allows testing a condition in a single line replacing the multiline if-else making the code compact.

Syntax :

[on_true] if [expression] else [on_false]

1- Simple Method to use ternary operator:

# 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)  # Output: 10

2- Direct Method of using tuples, Dictionary, and lambda:

# Python program to demonstrate ternary operator
a, b = 10, 20
# Use tuple for selecting an item
print( (b, a) [a < b] )
# Use Dictionary for selecting an item
print({True: a, False: b} [a < b])
# lamda is more efficient than above two methods
# because in lambda  we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]()) # in output you should see three 10

3- Ternary operator can be written as nested if-else:

# 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")

Above approach can be written as:

# 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") 
# Output: b is greater than a

回答 13

你可以这样做 :-

[condition] and [expression_1] or [expression_2] ;

例:-

print(number%2 and "odd" or "even")

如果数字为奇数,则将打印“ odd”;如果数字为偶数,则将打印“偶数”。


结果:- 如果条件为true,则执行exp_1,否则执行exp_2。

注意 :- -0,None,False,emptylist,emptyString计算为False。除0以外的任何数据都将评估为True。

运作方式如下:

如果条件[condition]变为“ True”,则将评估expression_1而不是expression_2。如果我们以0(零)“和”某物,结果将总是令人讨厌。因此,在下面的语句中,

0 and exp

完全不会对表达式exp求值,因为带有0的“ and”将始终求值为零,因此无需求值。这就是编译器本身在所有语言中的工作方式。

1 or exp

表达式exp根本不会被求值,因为带有“ 1”的“或”将始终为1。因此,无论如何,只要结果为1,就不会费心计算表达式exp。(编译器优化方法)。

但是在

True and exp1 or exp2

由于第二个表达式exp2不会被求值 True and exp1当exp1不为false时将为True。

同样在

False and exp1 or exp2

由于False等于写入0并用0进行“和”本身将为0,因此不会对表达式exp1进行求值,但是在exp1之后,由于使用了“或”,它将在表达式“或”之后求值exp2。


注意:-仅当expression_1的True值不为False(或0或None或emptylist []或emptystring”。)时,才可以使用这种使用“ or”和“ and”的分支,因为如果expression_1变为False,则由于exp_1和exp_2之间存在“或”,将对表达式_2进行求值。

如果您仍然想使它适用于所有情况,而不论exp_1和exp_2真值是多少,请执行以下操作:

[condition] and ([expression_1] or 1) or [expression_2] ;

you can do this :-

[condition] and [expression_1] or [expression_2] ;

Example:-

print(number%2 and "odd" or "even")

This would print “odd” if the number is odd or “even” if the number is even.


The result :- If condition is true exp_1 is executed else exp_2 is executed.

Note :- 0 , None , False , emptylist , emptyString evaluates as False. And any data other than 0 evaluates to True.

Here’s how it works:

if the condition [condition] becomes “True” then , expression_1 will be evaluated but not expression_2 . If we “and” something with 0 (zero) , the result will always to be fasle .So in the below statement ,

0 and exp

The expression exp won’t be evaluated at all since “and” with 0 will always evaluate to zero and there is no need to evaluate the expression . This is how the compiler itself works , in all languages.

In

1 or exp

the expression exp won’t be evaluated at all since “or” with 1 will always be 1. So it won’t bother to evaluate the expression exp since the result will be 1 anyway . (compiler optimization methods).

But in case of

True and exp1 or exp2

The second expression exp2 won’t be evaluated since True and exp1 would be True when exp1 isn’t false .

Similarly in

False and exp1 or exp2

The expression exp1 won’t be evaluated since False is equivalent to writing 0 and doing “and” with 0 would be 0 itself but after exp1 since “or” is used, it will evaluate the expression exp2 after “or” .


Note:- This kind of branching using “or” and “and” can only be used when the expression_1 doesn’t have a Truth value of False (or 0 or None or emptylist [ ] or emptystring ‘ ‘.) since if expression_1 becomes False , then the expression_2 will be evaluated because of the presence “or” between exp_1 and exp_2.

In case you still want to make it work for all the cases regardless of what exp_1 and exp_2 truth values are, do this :-

[condition] and ([expression_1] or 1) or [expression_2] ;


回答 14

提示多于答案(不需要在第一百遍重复显而易见的内容),但是我有时在这样的结构中将其用作单行捷径:

if conditionX:
    print('yes')
else:
    print('nah')

,变为:

print('yes') if conditionX else print('nah')

有些(很多:)可能不喜欢它是非Python风格的(甚至是红宝石色的:),但我个人认为它更自然-即您通常的表达方式,并且在较大的代码块中更具视觉吸引力。

More a tip than an answer (don’t need to repeat the obvious for the hundreth time), but I sometimes use it as a oneliner shortcut in such constructs:

if conditionX:
    print('yes')
else:
    print('nah')

, becomes:

print('yes') if conditionX else print('nah')

Some (many :) may frown upon it as unpythonic (even, ruby-ish :), but I personally find it more natural – i.e. how you’d express it normally, plus a bit more visually appealing in large blocks of code.


回答 15

a if condition else b

如果您难以记住,只需记住这座金字塔:

     condition
  if           else
a                   b 
a if condition else b

Just memorize this pyramid if you have trouble remembering:

     condition
  if           else
a                   b 

回答 16

Python 条件表达式的替代方法之一

"yes" if boolean else "no"

是以下内容:

{True:"yes", False:"no"}[boolean]

具有以下很好的扩展:

{True:"yes", False:"no", None:"maybe"}[boolean_or_none]

最短的选择仍然是:

("no", "yes")[boolean]

但是别无选择

yes() if boolean else no()

如果要避免对yes() 求值no(),因为

(no(), yes())[boolean]  # bad

no()yes()评估。

One of the alternatives to Python’s conditional expression

"yes" if boolean else "no"

is the following:

{True:"yes", False:"no"}[boolean]

which has the following nice extension:

{True:"yes", False:"no", None:"maybe"}[boolean_or_none]

The shortest alternative remains:

("no", "yes")[boolean]

but there is no alternative to

yes() if boolean else no()

if you want to avoid the evaluation of yes() and no(), because in

(no(), yes())[boolean]  # bad

both no() and yes() are evaluated.


回答 17

许多派生自的编程语言C通常具有以下三元条件运算符的语法:

<condition> ? <expression1> : <expression2>

起初,Python enevolent d ictator ˚F大号 IFE(我的意思是吉多·范罗苏姆,当然)拒绝了(非Python化风格),因为它是挺难理解不习惯的人C的语言。另外,冒号在中:已经有很多用途Python。在PEP 308批准后,Python最终收到了自己的快捷方式条件表达式(我们现在使用的是):

<expression1> if <condition> else <expression2>

因此,首先它评估条件。如果返回True,则将对expression1求值以给出结果,否则将对expression2求值。由于懒惰的评估机制–仅执行一个表达式。

以下是一些示例(条件将从左到右评估):

pressure = 10
print('High' if pressure < 20 else 'Critical')

# Result is 'High'

三元运算符可以串联在一起:

pressure = 5
print('Normal' if pressure < 10 else 'High' if pressure < 20 else 'Critical')

# Result is 'Normal'

下一个与上一个相同:

pressure = 5

if pressure < 20:
    if pressure < 10:
        print('Normal')
    else:
        print('High')
else:
    print('Critical')

# Result is 'Normal'

希望这可以帮助。

Many programming languages derived from C usually have the following syntax of ternary conditional operator:

<condition> ? <expression1> : <expression2>

At first, the Python Benevolent Dictator For Life (I mean Guido van Rossum, of course) rejected it (as non-Pythonic style), since it’s quite hard to understand for people not used to C language. Also, the colon sign : already has many uses in Python. After PEP 308 was approved, Python finally received its own shortcut conditional expression (what we use now):

<expression1> if <condition> else <expression2>

So, firstly it evaluates the condition. If it returns True, expression1 will be evaluated to give the result, otherwise expression2 will be evaluated. Due to Lazy Evaluation mechanics – only one expression will be executed.

Here are some examples (conditions will be evaluated from left to right):

pressure = 10
print('High' if pressure < 20 else 'Critical')

# Result is 'High'

Ternary operators can be chained in series:

pressure = 5
print('Normal' if pressure < 10 else 'High' if pressure < 20 else 'Critical')

# Result is 'Normal'

The following one is the same as previous one:

pressure = 5

if pressure < 20:
    if pressure < 10:
        print('Normal')
    else:
        print('High')
else:
    print('Critical')

# Result is 'Normal'

Hope this helps.


回答 18

正如已经回答的那样,是的,在python中有一个三元运算符:

<expression 1> if <condition> else <expression 2>

附加信息:

如果<expression 1>是这种情况,则可以使用“ 短罪犯”评估

a = True
b = False

# Instead of this:
x = a if a else b

# You could use Short-cirquit evaluation:
x = a or b

PS:当然,短路短路评估不是三元运算符,但是在短路就足够的情况下,经常使用三元运算符。

As already answered, yes there is a ternary operator in python:

<expression 1> if <condition> else <expression 2>

Additional information:

If <expression 1> is the condition you can use Short-cirquit evaluation:

a = True
b = False

# Instead of this:
x = a if a else b

# You could use Short-cirquit evaluation:
x = a or b

PS: Of course, a Short-cirquit evaluation is not a ternary operator but often the ternary is used in cases where the short circuit would be enough.


回答 19

是的,python有一个三元运算符,这是语法和示例代码来演示相同的内容:)

#[On true] if [expression] else[On false]
# if the expression evaluates to true then it will pass On true otherwise On false


a= input("Enter the First Number ")
b= input("Enter the Second Number ")

print("A is Bigger") if a>b else print("B is Bigger")

YES, python have a ternary operator, here is the syntax and an example code to demonstrate the same :)

#[On true] if [expression] else[On false]
# if the expression evaluates to true then it will pass On true otherwise On false


a= input("Enter the First Number ")
b= input("Enter the Second Number ")

print("A is Bigger") if a>b else print("B is Bigger")

回答 20

Python具有三元形式的赋值。但是,人们甚至应该注意更短的形式。

通常需要根据条件将一个值或另一个值赋给变量。

>>> li1 = None
>>> li2 = [1, 2, 3]
>>> 
>>> if li1:
...     a = li1
... else:
...     a = li2
...     
>>> a
[1, 2, 3]

^这是进行此类分配的长格式。

以下是三元形式。但这不是最简洁的方法-请参阅最后一个示例。

>>> a = li1 if li1 else li2
>>> 
>>> a
[1, 2, 3]
>>> 

使用Python,您可以简单地or用于其他分配。

>>> a = li1 or li2
>>> 
>>> a
[1, 2, 3]
>>> 

上面的代码自li1is开始起作用,None并且interp在逻辑表达式中将其视为False。然后,interp继续并计算第二个表达式,该表达式不是None,也不是一个空列表-因此将其分配给a。

这也适用于空列表。例如,如果您想分配a包含项目的列表。

>>> li1 = []
>>> li2 = [1, 2, 3]
>>> 
>>> a = li1 or li2
>>> 
>>> a
[1, 2, 3]
>>> 

知道了这一点,您可以在遇到作业时简单地进行此类作业。这也适用于字符串和其他可迭代对象。您可以分配a不为空的任何字符串。

>>> s1 = ''
>>> s2 = 'hello world'
>>> 
>>> a = s1 or s2
>>> 
>>> a
'hello world'
>>> 

我一直很喜欢C三进制语法,但是Python更进一步!

我知道有人可能说这不是一个好的风格选择,因为它依赖于并非所有开发人员都立即了解的机制。我个人不同意这种观点。Python是一种语法丰富的语言,具有许多惯用技巧,而这些惯用技巧对Dabler而言并不立即显而易见。但是,您越了解和理解底层系统的机制,就越会欣赏它。

Python has a ternary form for assignments; however there may be even a shorter form that people should be aware of.

It’s very common to need to assign to a variable one value or another depending on a condition.

>>> li1 = None
>>> li2 = [1, 2, 3]
>>> 
>>> if li1:
...     a = li1
... else:
...     a = li2
...     
>>> a
[1, 2, 3]

^ This is the long form for doing such assignments.

Below is the ternary form. But this isn’t most succinct way – see last example.

>>> a = li1 if li1 else li2
>>> 
>>> a
[1, 2, 3]
>>> 

With Python, you can simply use or for alternative assignments.

>>> a = li1 or li2
>>> 
>>> a
[1, 2, 3]
>>> 

The above works since li1 is None and the interp treats that as False in logic expressions. The interp then moves on and evaluates the second expression, which is not None and it’s not an empty list – so it gets assigned to a.

This also works with empty lists. For instance, if you want to assign a whichever list has items.

>>> li1 = []
>>> li2 = [1, 2, 3]
>>> 
>>> a = li1 or li2
>>> 
>>> a
[1, 2, 3]
>>> 

Knowing this, you can simply such assignments whenever you encounter them. This also works with strings and other iterables. You could assign a whichever string isn’t empty.

>>> s1 = ''
>>> s2 = 'hello world'
>>> 
>>> a = s1 or s2
>>> 
>>> a
'hello world'
>>> 

I always liked the C ternary syntax, but Python takes it a step further!

I understand that some may say this isn’t a good stylistic choice because it relies on mechanics that aren’t immediately apparent to all developers. I personally disagree with that viewpoint. Python is a syntax rich language with lots of idiomatic tricks that aren’t immediately apparent to the dabler. But the more you learn and understand the mechanics of the underlying system, the more you appreciate it.


回答 21

其他答案正确地讨论了Python三元运算符。我想通过提及一个经常使用三元运算符但有更好的成语的场景来进行补充。这是使用默认值的情况。

假设我们要使用option_value未设置的默认值:

run_algorithm(option_value if option_value is not None else 10)

或简单地

run_algorithm(option_value if option_value else 10)

但是,一个更好的解决方案是简单地编写

run_algorithm(option_value or 10)

Other answers correctly talk about the Python ternary operator. I would like to complement by mentioning a scenario for which the ternary operator is often used but for which there is a better idiom. This is the scenario of using a default value.

Suppose we want to use option_value with a default value if it is not set:

run_algorithm(option_value if option_value is not None else 10)

or simply

run_algorithm(option_value if option_value else 10)

However, an ever better solution is simply to write

run_algorithm(option_value or 10)

回答 22

如果定义了变量,并且您想检查它是否具有值,则可以 a or b

def test(myvar=None):
    # shorter than: print myvar if myvar else "no Input"
    print myvar or "no Input"

test()
test([])
test(False)
test('hello')
test(['Hello'])
test(True)

将输出

no Input
no Input
no Input
hello
['Hello']
True

if variable is defined and you want to check if it has value you can just a or b

def test(myvar=None):
    # shorter than: print myvar if myvar else "no Input"
    print myvar or "no Input"

test()
test([])
test(False)
test('hello')
test(['Hello'])
test(True)

will output

no Input
no Input
no Input
hello
['Hello']
True

回答 23

链接多个运算符的一种巧妙方法:

f = lambda x,y: 'greater' if x > y else 'less' if y > x else 'equal'

array = [(0,0),(0,1),(1,0),(1,1)]

for a in array:
  x, y = a[0], a[1]
  print(f(x,y))

# Output is:
#   equal,
#   less,
#   greater,
#   equal

A neat way to chain multiple operators:

f = lambda x,y: 'greater' if x > y else 'less' if y > x else 'equal'

array = [(0,0),(0,1),(1,0),(1,1)]

for a in array:
  x, y = a[0], a[1]
  print(f(x,y))

# Output is:
#   equal,
#   less,
#   greater,
#   equal


回答 24

我发现麻烦的是默认的python语法val = a if cond else b,所以有时我这样做:

iif = lambda (cond, a, b): a if cond else b
# so I can then use it like:
val = iif(cond, a, b)

当然,它总是总是评估双方(a和b),但它的语法对我来说更清晰

I find cumbersome the default python syntax val = a if cond else b, so sometimes I do this:

iif = lambda (cond, a, b): a if cond else b
# so I can then use it like:
val = iif(cond, a, b)

Of course, it has the downside of always evaluating both sides (a and b), but the syntax it’s way clearer to me


回答 25

is_spacial=True if gender = "Female" else (True if age >= 65 else False)

**

它可以根据需要嵌套。祝你好运

**

is_spacial=True if gender = "Female" else (True if age >= 65 else False)

**

it can be nested as your need. best of luck

**