Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I was simply messing with the python order line and the ** administrator, which to the extent I know plays out a power function. So 2 ** 3 ought to be (and is) 8 since 2 * 2 * 2 = 8. 

Would someone be able to clarify the conduct I found? I don't perceive any approach to bunch the tasks with enclosures to really get an aftereffect of 65536 like was achieved here.

>>> 2 ** 2 ** 2

16

>>> 2 ** 2 ** 2 ** 2

65536

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

256

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer

In Python, the associativity of the ** operator is right-associative. It means that when there are multiple ** operators in an expression, the calculation proceeds from right to left. This is important when using multiple exponentiation operators in a single expression. Here's an example to illustrate the associativity of the ** operator:

result = 2 ** 3 ** 2 print(result)

In this case, the calculation will be performed as 2 ** (3 ** 2), resulting in 2 ** 9, which equals 512. If the operator were left-associative, the calculation would have been performed as (2 ** 3) ** 2, resulting in 8 ** 2, which equals 64. However, in Python, the ** operator is right-associative, and hence the first calculation is performed.
0 votes
by (26.4k points)

Check the below code:

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

From this Documentation:

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).

Join the python online course fast, to learn python concepts in detail and get certified.

0 votes
by (15.4k points)
The ** operator in Python has right associativity, meaning that when there are multiple ** operators in an expression, the evaluation proceeds from right to left. This differs from left associativity, where the evaluation would proceed from left to right.
0 votes
by (19k points)

In Python, the ** operator exhibits right associativity, indicating that when there are multiple ** operators within an expression, the evaluation takes place from right to left. This contrasts with left associativity, where the evaluation would occur from left to right.

Related questions

0 votes
1 answer
asked Sep 30, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...