三元运算符:
如果条件满足则变量赋值为值1,否则变量赋值为值2
变量 = 值1 if 条件 else 值2
举例1:
name = "zhangsan" if 1==1 else "Lisi"
print(name)
打印结果:zhangsan
举例2:
name = "zhangsan" if 1==2 else "Lisi"
print(name)
打印结果:Lisi
三元运算符:
如果条件满足则变量赋值为值1,否则变量赋值为值2
变量 = 值1 if 条件 else 值2
举例1:
name = "zhangsan" if 1==1 else "Lisi"
print(name)
打印结果:zhangsan
举例2:
name = "zhangsan" if 1==2 else "Lisi"
print(name)
打印结果:Lisi