python 三元运算符

环境:

操作系统: windows10 教育版 1903

python版本: 3.7.3

IDE: pycharm: PyCharm 2019.2.4 (Professional Edition)


解析:

  res = 运算结果true if 逻辑表达式 else 运算结果false

  注: 若逻辑表达式为true, 则res = 运算结果true ; 若逻辑表达式为false,则 res = 运算结果false

代码(demo1.py)

#!/user/bin/python
#-*- coding:utf-8 -*-
num1 = 1
num2 = 2
res = '1大' if num1 > num2 else '2大'
print(res)

运行结果

2大
原文地址:https://www.cnblogs.com/tu13/p/python_3_base_three_lo.html