Python Forum
Order of operations for reassignment of a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Order of operations for reassignment of a variable
#1
Hello folks,

A quick question about the following lines:

x=x**3+2
x=3*x+1
If I were to write this as an equivalent single calculation, does python interpret the first value of x into the second parenthesised:

x=3*(x**3+2)+1

or interpret as follows:

x=3*x**3+2+1

As these would produce different results ...
Reply
#2
Math rules apply.
And you can always test it in the interactive shell
>>> x = 2
>>> x = 3 * (x ** 3 + 2) + 1
>>> x
31
for more information - Operator precedence
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help implementing an input with file operations Gaijin 3 2,074 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  I'm a bit confused with these boolean operations on integers goelraghavg 6 3,649 Aug-25-2018, 03:16 PM
Last Post: perfringo
  Arithmetic operations using lists yassine 2 2,394 May-02-2018, 06:20 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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