Ternary operator

2018-04-01  本文已影响4人  成江
is_fat = True
state = "fat" if is_fat else "not fat"

Another more obscure and not widely used example involves tuples. Here is some sample code:

Blueprint:

(if_test_is_false, if_test_is_true)[test]

Example:

fat = True
fitness = ("skinny", "fat")[fat]
print("Ali is ", fitness)
# Output: Ali is fat
上一篇 下一篇

猜你喜欢

热点阅读