Tips] [Python a triplet of expressions (ternary operator)

Ternary expressions with a fixed format programming, programming language is a general format: 判段条件 ? 条件为真时的结果 : 条件为假时的结果, illustrate:

int A,B,C; 
A = 1;
B = 2; 
C = A > B ? 10 : 20; 

Explanation: If A> B, will be assigned to C 10, or 20 will be assigned to C, so in this case it is 10 C


But a triplet of expressions in Python and other languages are different, the format is: 条件为真时的结果 if 判段条件 else 条件为假时的结果, example:

>>> A = 1
>>> B = 2
>>> C = 20 if A > B else 10
>>> print(C)
10

Explanation: If A> B, will be assigned to C 20, or 10 will be assigned to C, so in this case it is 10 C

Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104381111