What is the output of the following Python code? print(2 ** 3 ** 2)
Correct answer C. 512
Explanation
The power operator ** is right-associative, so 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2), which is 2 ** 9 = 512. Reading it left to right as (2 ** 3) ** 2 gives 64, the classic mistake. Most other binary operators in Python group from left to right, which is why this one catches people out.
Answer checked How we write and check questions
Discussion