1、Python 中使用 if、elif、else 来进行基础的条件选择操作:
ifx<0: x=0 print('Negativechangedtozero') elifx==0: print('Zero') else: print('More'
2、Python 同样支持 ternary conditional operator,也可以使用 Tuple 来实现类似的效果:
#test需要返回True或者False (falseValue,trueValue)[test] #更安全的做法是进行强制判断 (falseValue,trueValue)[test==True] #或者使用bool类型转换函数 (falseValue,trueValue)[bool(<expression>)]
以上就是python实现条件选择的方法,希望对大家有所帮助。
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
上一篇
下一篇