python_ ternary operator

The ternary operator, also known as three head operations, is shorthand for simple conditional statement

Simple conditional statement:

if conditions are met: 
    Val = 1
 the else : 
    Val = 2

Into the ternary operator:

= 1 Val IF condition is true the else 2

For example:

a = 2
b = 5
val = a if a > b else b
print(val)  # 5

val = a if a < 3 else b
print(val)  # 2

Guess you like

Origin www.cnblogs.com/wangdianchao/p/11459421.html