12

I am reading an Intro to Python textbook and came across this line:

Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left.

I understand most of this, but I do not understand why they say exponentiation is applied right to left. They do not provide any examples either. Also, am I allowed to ask general questions like this, or are only problem solving questions preferred?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
elitecheese1337
  • 149
  • 2
  • 13
  • 1
    That wording is slightly confusing. It is talking about binding behaviour, meaning that in a *larger expression*, determining what values the `**` binds to is determined by the precedence order. So in `a + b ** c ** d`, what is executed first is determined by the precedence order. – Martijn Pieters Nov 22 '17 at 08:02
  • And the order follows maths conventions, just like multiplication being applied before addition. – Martijn Pieters Nov 22 '17 at 08:03
  • In the example a + b ** c * d, I guess it would then be (c * d) ** (a + b), correct? Because according to precedence order multiplication applies before addition. – elitecheese1337 Nov 22 '17 at 08:10
  • `**` binds more tightly than multiplication or addition. So it is `c * (d ** a) + b`. – Martijn Pieters Nov 22 '17 at 08:26
  • shouldn't... a + b ** c ** d ...be... a + (b ** (c ** d)) ? – Joseph Diefenbach Mar 16 '23 at 00:43

4 Answers4

25

The ** operator follows normal mathematical conventions; it is right-associative:

In the usual computer science jargon, exponentiation in mathematics is right-associative, which means that xyz should be read as x(yz), not (xy)z. In expositions of the BODMAS rules that are careful enough to address this question, the rule is to evaluate the top exponent first.

and from Wikipedia on the Order of Operations:

If exponentiation is indicated by stacked symbols, the usual rule is to work from the top down, because exponention is right-associative in mathematics.

So 2 ** 3 ** 4 is calculated as 2 ** (3 ** 4) (== 2417851639229258349412352) not (2 ** 3) ** 4 (== 4096).

This is pretty universal across programming languages; it is called right-associativity, although there are exceptions, with Excel and MATLAB being the most notable.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Hi Martijn. I have one more question. Could you please explain to me why you chose the expression a + b ** c * d as an example in the first place? The only reason I am asking is because it only has one exponent in the expression. Thanks~ – elitecheese1337 Nov 22 '17 at 08:27
  • @elitecheese1337: it was a dumb example, one that doesn't illustrate the issue. Sorry. :-/ – Martijn Pieters Nov 22 '17 at 08:29
  • @elitecheese1337: it should have been `a + b ** c ** d`, really. – Martijn Pieters Nov 22 '17 at 08:29
  • There are many exceptions. Here is a large list of how different languages and apps handle exponential operator: https://codeplea.com/exponentiation-associativity-options – Kirill Ch Dec 02 '22 at 10:19
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).

>>> 2 ** 2 ** 2
16
>>> 2 ** 2 ** 2 ** 2
65536
>>> (2 ** 2 ** 2) ** 2
256

For the middle case 2 ** 2 ** 2 ** 2, this are the intermediate steps -

  1. broken down to 2 ** (2 ** (2 ** 2))
  2. 2 ** (2 ** (4)) # progressing right to left
  3. 2 ** (16) # this is 2 to the power 16 which finally evals to 65536 Hope that helps!
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • I just wanted to list out an example which goes from expected to unexpected behaviour and then explains what's up... but what you're saying makes sense – Vivek Kalyanarangan Nov 22 '17 at 08:10
1

Power operator, exponentiation, is handled differently across applications and languages.

If it has LEFT associativity then 2^3^4 = (2^3)^4 = 4096.
If it has RIGHT associativity then 2^3^4 = 2^(3^4) = 2417851639229260000000000.

In Excel, Matlab, Apple Numbers and more others exponentiation has LEFT associativity.
In Python, Ruby, Google Sheets, ... - RIGHT associativity.

Here is a vast list of how different languages and apps handle exponentiation: Exponentiation Associativity and Standard Math Notation

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
0

This explanation seems quite clear to me. Let me show you an example that might enlighten this :

print 2 ** 2 ** 3 # prints 256

If you would read this from left to right, you would first do 2 ** 2, which would result in 4, and then 4 ** 3, which would give us 64. It seems we have a wrong answer. :)

However, from right to left... You would first do 2 ** 3, which would be 8, and then, 2 ** 8, giving us 256 !

I hope I was able to enlighten this point for you. :)

EDIT : Martijn Pieters answered more accurately to your question, sorry. I forgot to say it was mathematical conventions.

IMCoins
  • 3,149
  • 1
  • 10
  • 25
  • Thank you so much! This makes perfect sense to me! Originally what I was thinking is that they meant 3 ** 2 ** 2 when they said right to left. Thanks again! – elitecheese1337 Nov 22 '17 at 08:18