# [表达式为真的返回值] if [表达式] else [表达式为假的返回值]defsmall(a,b,c):return a if a <= b and a<= c else(b if b <= a and b <= c else c)
print(small(1, 0, 1))
print(small(1, 2, 2))
print(small(2, 2, 3))
print(small(5, 4, 3))
0
1
2
3
# classA 与 classB 是两个类,其中一个类的构造函数会被调用
x = (classA if y == 1else classB)(param1, param2)
# 可以在列表推导中使用三元运算符
[m**2if m > 10else m**4for m in range(50)]
存储列表元素到新的变量中
# 元素个数与列表长度应该严格相同,不然会报错
testList = [1, 2, 3]
x, y, z = testList
print(x, y, z)
<module 'threading' from '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py'>
<module 'socket' from '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py'>