Python Forum

Full Version: Exponeniation and Right Binding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please see the included images.

I was taking an online course and came across what I perceive as a problem. If I am right binding inside of Python (performing the given examples) shouldn't this return a different result?

This was found in the Cisco Skills For All webpage. Am I being brain hacked?
I think this is an operator precedence question, not a right side binding question. ** has higher precedence than unary -, so ** is done first and - is done second.

This is an example of right side binding (right to left execution).
Output:
>>> 2 ** 3 ** 2 512 >>> (2 ** 3) ** 2 64 >>> 2 ** (3 ** 2) 512
The operators are the same, so they have the same precedence. When precedence is the same use left/right binding to determine order of execution. You can see from the results that 3 ** 2 is evaluated first and 2 ** 9 second.

division has left side binding (left to right execution).
Output:
>>> 4 / 2 / 2 1.0 >>> (4 / 2) / 2 1.0 >>> 4 / (2 / 2) 4.0
Thank you for the help.
(Feb-14-2024, 10:21 PM)PhDoctus Wrote: [ -> ]Am I being brain hacked?
This image shows how python parses the expression -3 ** 2 (the Abstract Syntax Tree)