Difference between op= vs. value = value op expression

Hello,

I want to know if someone can help me to understand, what is the difference between these 2 modes. Both resolve an expression. But it gives different results. In different learning pages explain that “value op= expression” is shorter than “value = value op expression”.

image

Hi Indira,

Sorry I can’t see your image, all I get is this:

![image|324x140](upload://9XSB2Ylp8ROXnc0NoX0ZJO4YItQ.png)

which isn’t helpful. Can you copy and paste the example code into your
post as text please?

Not OP but I’ll type what’s in the picture for you:

# Mathematics operators
var = 6
var /= 3 * 2.  # the answer is 1.0
var = var / 3 * 2.  # the answer is 4.0

var /= 3 * 2 is the same as var = var / (3 * 2), as when using /=, Python would divide the variable on the left, with the value on the right, which can be simplified to var = var / 6.

Meanwhile, in var = var / 3 * 2, since there are no brackets at all (otherwise Python would evaluate the value inside the bracket first, like normal mathematical operations), so Python would just evaluate from left to right. So var = var / 3 * 2 can be simplified to var = 2 * 2 (var = 6 and 6 / 3 = 2), which will then be simplified to var = 4.0 , as 2 multiplied by 2 is 4.

To summarise, /= divides the var on the left by the final value on the right, while var = var / 3 * 2 will be evaluated from left to right since there are no brackets. If you bracket 3 * 2 you will find that var = 1.0 now. Hope this was clear.