Python Forum

Full Version: Order of operations for reassignment of a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ...
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