Execution of Multiplication and Division in one line

Screen Link:

maths

My Code:

no /= 60 is short form for no = no / 60
But I see that the output for both is different.
I checked on google and came to know that python follows PEMDAS technique to follow order of operation.
It stated that in PEMDAS we strictly follow multiplication before division.

Kindly advise why is the difference between both the output and what is happening behind the scenes.

What I expected to happen:
The output of both the codes should be 25.0 as first one is short form of the second.

What actually happened:
In first case it first multiplied and then divided.

In second case it first divided and then multiplied(according to PEMDAS it should have multipled first and then divided)

Replace this line with the output/error

Hello @krrishd1007

Thanks for posting this wonderful question!

Case 1 - (no/=60100)
In this case, what happens behind the scenes is, the value which is the right side of assignment (=) will be computed first i,e.
(60*100 = 6000) then the short form expression will be expanded as (no=no/6000) which results the value 0.0025 and it is indeed.

Case 2- ( no=no/60*100)
PEMDAS says - Even though multiplication comes before division in the saying, division could be performed first. Which is performed first, between multiplication and division, is determined by which comes first when reading from left to right. The same is true of addition and subtraction. Don’t let the saying confuse you about this. You can read the same here

As mentioned above, PEMDAS division and multiplication has same precedence. Therefore, in our case division has appeared before multiplication so (no/60) will be computed first and then the resulted will be multiplied with 100 resulting the value 25.

Finally I would like to conclude that both case are different and their respective outputs are absolutely correct.

I hope this helps.

Best
K

1 Like

Thank you for explaining it in detail.

1 Like