+ 3

# Please can someone explain how '512' is the answer of these code; print(2**3**2). I did mine like (2**3) 2×2×2= (8×8)=64

Exponential

12 Answers

+ 10

print(2**3**2) means :
-->2**3**2
-->2 to the power of (3 to the power of 2)(3*3)(9)
-->2 to the power 9
-->512
it like this
a = 3**2
b = 2**a
means: b = 2**(3**2)
a is evaluated first to give us 3 to the power of 2 and returns 9
a =>3**2 = 9
then b is evaluated as 2 to the power of a to give us 2 to the power of 9 which returns as 512
b=> 2**9 = 512
which means 2**3**2 = 512

+ 7

Lack of brackets, so the python Compiler(interpreter) is solving it from right to left it seems. Its evaluating 3 **2 which is 9, then 2**9 which is 512.

+ 6

Hi funkey!
You can just think about what we're doing in usual mathematics. We can write this expression on a piece of paper like this
2
3
2
So, it can be shorten as 2⁹ which is equal to 512.

+ 3

Ira Gideon Kane When operations have the same rank in the order of precedence python evaluates from left to right. But exponentiation comes before multiplication in python.

+ 3

Let me explain:
2**3**2
= 2**(3**2).............. [3**2 = 3×3 = 9]
= 2**9
= 2×2×2×2×2×2×2×2×2
=512

+ 2

I appreciate the explanation, thank's you all.

+ 1

Simon Sauter
Thanks !

+ 1

I am wrong still right to left as computer does not represent top down as such.

+ 1

Ans : 2**3**2
= 2 **(3*3)
= 2**9
= 512
Because ** works as right to left not left to right like other operators

+ 1

Yes

0

both are exponents so you would think expinenents are always to the right not to the left so right side first, but the law of precedence is not clear about it as it says left to right if operation has same level of precedence or identical operations.

- 1

Usually, evaluation starts from the left side.Strange