Ternary operator

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)