0

I was playing around with few basic operations in Python 3 and came across this

Code 1:

a = 6
b = 3
a = a / 2 * b
print(a)

Output is 9.0

In one of the trainings I have seen that

a = a / 2 

can be written as

a /= 2

So I re-wrote my above code as below

Code 2:

a = 6
b = 3
a /= 2 * b
print(a)

But now the output is 1.0. I was expecting 9.0 as output. Can someone help me understand why code behaves this way?

6 Answers6

1

In the case of a /= 2 * b, you saying that a will be divided by expression after /=.

In other words, your expression could be rewritten as a = a / (2 * b) where a = 6, b = 3

Alex
  • 798
  • 1
  • 8
  • 21
0

in

a /= 2 * b

the 2 * b is carried out first so if b == 3 it can be rewritten as:

a /= 6
0

That's because they perform operations in different order.

The first of block of code divides a (6) by 2 and then multiplies that result by b (3)

E.g. (6 / 2) * 3 = 9

However the second block of code does the following:

6 / (2 * 3) = 1

alexandrosangeli
  • 262
  • 2
  • 13
0

Note, that

a /= 2 * b

is basically

a = a / (2 * b)

The previous code is different. It's not like the above (look at the parentheses)

a = a / 2 * b

This is different than

a = a / (2 * b)

Because the first code interpret it as division and then multiply, while the second code interpret it as multiply (bottom) and then division.

FaranAiki
  • 453
  • 3
  • 12
0

as you can see in the documentatiopn on operator precedence * and / have the same precedence. two operations with the same precedence are executed from left to right:

Operators in the same box group left to right

so your first example is evaluated as

(a / 2) * b

the second as

a / (2 * b)

regarding your comment

a = a / 2 and a /= 2 are always equivalent, right?

for the built-in python numbers you are right.

for your own classes you can make the operations differ. if a is an insance of a custom class a/x will call __truediv__(self, other) while a/=x will call __itruediv__(self, other) as described in the data model.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • yes..I got the logic behind getting a different value, but then my question becomes how can we say that a = a / 2 and a /= 2 are always equivalent. This purely depends on whether there is further expressions after first expression – Abraham Panicker Dec 03 '20 at 12:15
  • do you have an example for what you mean? in the `/=` case everything on the right-hand-side will be calculated before the division (and the assignment). – hiro protagonist Dec 03 '20 at 12:19
  • yes...you can add any further ops to that right side and its all considered as part of that same bracket and gets evaluated before division eg. a /= 2 * b + a is equivalent to a = a / (2 * b + a) – Abraham Panicker Dec 03 '20 at 12:27
0

I'm surprised none of the other answers mention this, but the reason doesn't have anything to do with math, order of operations or implicit parentheses.

x /= y is just another way of saying x.__itruediv__(y). Your second example is not equivalent to your first example, because parameters are evaluated before being passed in to a function call. So, a /= 2 * b is equivalent to a.__itruediv__(2 * b), and naturally, the expression 2 * b must be evaluated before being passed in to __itruediv__.

Paul M.
  • 10,481
  • 2
  • 9
  • 15