三元运算符是条件语句的替代方案,它将真或假值与需要测试的语句组合在一起。
语法如下所示:
[onTrue] if [Condition] else [onFalse]
示例代码:
x, y = 35, 75
smaller = x if x < y else y
print(smaller)
86、三元运算符在 Python 中是如何工作的?
三元运算符是条件语句的替代方案,它将真或假值与需要测试的语句组合在一起。
语法如下所示:
[onTrue] if [Condition] else [onFalse]
示例代码:
x, y = 35, 75
smaller = x if x < y else y
print(smaller)