Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.