Python Forum
Exponeniation and Right Binding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exponeniation and Right Binding
#1
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?

Attached Files

Thumbnail(s)
       
Reply
#2
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
Gribouillis, Pedroski55, PhDoctus like this post
Reply
#3
Thank you for the help.
Reply
#4
(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)

Attached Files

Thumbnail(s)
   
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 609 Sep-01-2023, 11:26 AM
Last Post: bthomas
  Binding Complex data to Combobox gcfernando 2 2,091 Sep-14-2020, 03:24 PM
Last Post: gcfernando
  Binding not explicitly declared happening karkas 4 2,886 Aug-05-2019, 05:09 PM
Last Post: karkas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020