Q. How will Python evaluate the following expression?
(i) 20 + 30 * 40
(ii) 20 - 30 + 40
(iii) (20 + 30) * 40
(iv) 15.0 / 4 + (8 + 3.0)
Answer =
(i)
Since precedence of * operator is greater than + operator so in the given code it evaluate like:-
First step:- 30*40
Second step:- 1200 + 20
So for answer is 1220.
(ii)
Since precedence of - operator is equal to + operator so in the given code it evaluate like:-
First step:- -30 + 40 OR 20 - 30
Second step:- 10 OR -10
So for answer is 30
(iii)
Since precedence of parentheses is greater than * operator so in the given code it evaluate like:-
First step:- (50) * 40
Second step:- 50 * 40
So for answer is 2000.
(iv)
Since precedence of parentheses > / > + operator so in the given code it evaluate like:-
First step:- 15.0 / 4 + (11.0)
Second step:- 3.75 + (11.0)
So for answer is 14.75.
I am still confused in the second answer. Please help 🥺
ReplyDeleteDo it either the way, it's the same. Dude, it's basic mathematics.
DeleteSee => 20 - 30 + 40 = 20 + 40 - 30 => 60 - 30 = 30.
or you can do it as: = (20 - 30) + 40 => -10 + 40 = 30.
It's the same ^_^
First step from right is -30+40 = 10
ReplyDeleteSecond step is 20+10 = 30
we need to follow the python rules not the mathematics
DeleteIt's BODMAS dude
ReplyDelete: )
Deleteis the answers 100 per cent certified or correct and why should we believe
ReplyDeleteAll are correct, If you have doubt then ask to your subject tecther.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )