美文网首页
Ternary operator

Ternary operator

作者: 成江 | 来源:发表于2018-04-01 09:03 被阅读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

相关文章

网友评论

      本文标题:Ternary operator

      本文链接:https://www.haomeiwen.com/subject/xtdocftx.html