1 if 条件成立: 2 val = 1 3 else: 4 val = 2
改成三元运算:
val = 1 if 条件成立 else 2
举例
条件成立走左边,条件成立走右边
a = 2 b = 5 val = a if a > b else b print(val) # 5 val = a if a < 3 else b print(val) # 2