Note 2.9.1.
An exception to the left-to-right left-associative rule is the exponentiation operator **. A useful hint is to always use parentheses to force exactly the order you want when exponentiation is involved:
2 * (3-1)
is 4, and (1+1)**(5-2)
is 8. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60
, even though it doesn’t change the result.2**1+1
is 3 and not 4, and 3*1**3
is 3 and not 27. Can you explain why?2*3-1
yields 5 rather than 4, and 5-2*2
is 1, not 6.**
) are evaluated from left-to-right. In algebra we say they are left-associative. So in the expression 6-3+2
, the subtraction happens first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from right to left, the result would have been 6-(3+2)
, which is 1.16 - 2 * 5 // 3 + 1
2 ** 2 ** 3 * 3