Reputation: 3
Im stuck with this exercise, because if i wrote with shortcut operator the result is 1, therefor is 9
a = 6
b = 3
a /= 2 * b
print(a)
a = a / 2 * b [result 9] a /= 2 * b [result 1]
BUT if i do this exercise with * and after / like this:
a *= 2 / b
Why in this case they dont do (2/b) first?
Upvotes: 0
Views: 1748
Reputation: 81624
a = a / 2 * b
is a = 6 / 2 * 3
(following 'normal' math PEMDAS rules).
On the other hand,
a /= 2 * b
is a = 6 / (2 * 3)
(since the right hand side must be evaluated first, this essentially becomes a /= 6
-> a = a / 6
)
why the right-hand side must be evaluated first? because the statement (a = 6 / 2) * 3
does not make sense.
Regarding your edit: The exact same behavior happens when comparing a *= 2 / b
and a = a * 2 / b
. The difference is that in this example a
is 4 in both cases because both (6 * 2) / 3
and 6 * (2 / 3)
evaluate to 4.
Upvotes: 1