Skip to content Skip to sidebar Skip to footer

What Is The Associativity Of Python's ** Operator?

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

Solution 1:

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

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

Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left).

Solution 2:

Also:

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

One way or the other, it becomes 2 ** 16.

This is following standard mathematical operations, where: 2 becomes 2 , instead of 8 and thus is 2417851639229258349412352, instead of 4096.

Post a Comment for "What Is The Associativity Of Python's ** Operator?"