What is Python ** operator associativity?

I just played with the python command line and the ** operator, which, as far as I know, performs a power function. So, 2 ** 3 must be (and is) 8, because 2 * 2 * 2 = 8.

Can someone explain the behavior I found? I see no way to group parenthesized operations to get the result of 65536, as was achieved here.

>>> 2 ** 2 ** 2
16
>>> 2 ** 2 ** 2 ** 2
65536
>>> (2 ** 2 ** 2) ** 2
256
+7
source share
4 answers
2** (2**(2**2))

from http://docs.python.org/reference/expressions.html

( , , - . - , ).

+6

. , .

>>> 3 ** 3 ** 3
7625597484987
>>> (3 ** 3) ** 3
19683
>>> 3 ** (3 ** 3)
7625597484987

, .

. google: " ", http://www.python.org/doc/2.5.2/ref/power.html

:

, ( ).

.

+5

:

2 ** (2 ** 2 ** 2)

, 2 ** 16.

These are the following standard mathematical operations, where: 2 3 4 becomes 2 81 instead of 8 4 and, therefore, is 2417851639229258349412352 instead of 4096.

+1
source

The ** operator is the correct associative:

2 ** (2 ** (2 ** 2)) = 2 ** (2 ** 4) = 2 ** 16 = 65536

0
source

All Articles