a if condition else b
a = 2
b = 5
val = a if a > b else b
print(val) # 5
简单条件语句
if 条件:
val = 1
else:
val = 2
改成三元运算:
val = 1 if 条件成立 else 2
a if condition else b
a = 2
b = 5
val = a if a > b else b
print(val) # 5
简单条件语句
if 条件:
val = 1
else:
val = 2
改成三元运算:
val = 1 if 条件成立 else 2