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.

8 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. I am still confused in the second answer. Please help 🥺

    ReplyDelete
    Replies
    1. Do it either the way, it's the same. Dude, it's basic mathematics.
      See => 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 ^_^

      Delete
  2. First step from right is -30+40 = 10
    Second step is 20+10 = 30

    ReplyDelete
    Replies
    1. we need to follow the python rules not the mathematics

      Delete
  3. It's BODMAS dude

    ReplyDelete
  4. is the answers 100 per cent certified or correct and why should we believe

    ReplyDelete
    Replies
    1. All are correct, If you have doubt then ask to your subject tecther.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post