What will this code return?

Technology CommunityCategory: PythonWhat will this code return?
VietMX Staff asked 3 years ago
Problem

Consider:

a = True
print(('A', 'B')[a == False])

What will this code return? Explain.

Here ('A', 'B') is a tuple. We could access values in tuple, use the square brackets []. The a == False is an expression that could be evaluated as boolean. In Python 3.x True and False are keywords and will always be equal to 1 and 0. So the result will be A:

a = True
print(('A', 'B')[a == False])
# (falseValue, trueValue)[test]
# Output: A