0

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?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

why the right hand side must be evaluated first? if i try with an expression with *= everything works great
@MC everything works great with /= too. It just so happens that (6 * 2) * 3 is equal to 6 * (2 * 3)
no i mean 6 * 2 / 3 anyway i read that the exponentiation operator uses right-sided binding. the other one left
@MC There are no exponentiation operators here, * is multiplication.
because of that i dont understand why i need to execute multiplication FIRST! ( in right-sided way)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.