Operator Precedence and Associativity in Python || Lesson 6 || Python || Learning Monkey ||

Поделиться
HTML-код
  • Опубликовано: 12 авг 2020
  • #python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
    In this class, we discuss operator precedence and associativity in python.
    We discussed all the operators present in python in our previous class. press here for operators.
    Precedence in Python
    First, check the operator precedence and associativity chart given below.
    From the above chart, it was given the topmost operators are given the highest precedence.
    The precedence from top to bottom decreases. means the lowest operator in the chart has the lowest precedence
    Let’s take an example and understand what precedence means.
    Take the expression 2+5*2.
    In order to evaluate the expression, we can do in two ways.
    First do addition ie 2+5 = 7. Then do multiplication 7*2 = 14.
    The other way first do multiplication ie 5*2 =10. then do addition 2+10 =12.
    We got different answers, to avoid confusion we have to follow a particular order.
    This order is followed according to precedence. the operator having the highest precedence will execute first.
    From our example and the precedence char given above. multiplication operator has the highest precedence.
    Multiplication is done first. the result is added with 2.
    From the above chart, the operators present at the same level are having the same precedence.
    Multiplication, division, modulus, and floor division all are at the same level. so same precedence.
    With this understanding, we will check what is associativity means.
    Associativity
    Let’s take an example and understand the need for associativity.
    Example: 2+5*2*2/5.
    How to evaluate the expression?
    The above expression had both multiplication and division.
    Both are having the same precedence. so which one should be evaluated first?
    Here comes the concept of associativity. In the table, it was given associativity as left to right.
    The expression evaluates from left to right. ie 5*2 evaluates first, the result is 10.
    10*2 is evaluated next. the result is 20. 20/5 is evaluated next, the result is 4. The last 2+4 will be done.
    The final output is 6.
    Few more examples.
    print(2**3**2) the output is 512.
    Here power is having a right to left associativity. so 3**2 is evaluated first, the output is 9.
    Now 2**9 is evaluated which results in 512.
    Deviations in associativity
    Take example: x=5,y=6,z=7.
    print(xyz). this should be evaluated xy first the result is true. means 1.
    Now, 1z should be evaluated according to associativity.
    But it evaluates like xy and yz.
    Which results in true.
    Take one more example.
    x=5,y=5,z=5.
    print(x is y is z). this evaluates as x is y and y is z. which gives true.
    Link for playlists:
    / @learningmonkey
    Link for our website: learningmonkey.in
    Follow us on Facebook @ / learningmonkey
    Follow us on Instagram @ / learningmonkey1
    Follow us on Twitter @ / _learningmonkey
    Mail us @ learningmonkey01@gmail.com

Комментарии • 9

  • @maipeloikitseng2628
    @maipeloikitseng2628 Год назад

    Wooow 😯🔥

  • @sandeepdeepu6713
    @sandeepdeepu6713 9 месяцев назад

    Sir, how to use | (vertical dash operator)???

    • @LearningMonkey
      @LearningMonkey  9 месяцев назад +1

      Bit wise OR operator
      A=4. In binary 100
      B=2 in binary. 010
      A | B = 110 = 6

    • @sandeepdeepu6713
      @sandeepdeepu6713 9 месяцев назад

      @@LearningMonkey thankyou very much sir

  • @databank5086
    @databank5086 3 года назад

    Can you please repeat on the 3rd in precedence chart (await)

    • @LearningMonkey
      @LearningMonkey  3 года назад

      Hello
      I didn't get your question.
      can you please elaborate.

    • @databank5086
      @databank5086 3 года назад

      @@LearningMonkey hai, i mean you have written await on board in row3-- what is it referring to

    • @LearningMonkey
      @LearningMonkey  3 года назад +1

      Hi
      For the sake of completeness we have written the complete precedence chart.
      Await is used in concurrency programming.
      In future if we discuss concurrent programming we discuss it clearly.
      It's not possible to discuss it hear.
      Have a great learning

    • @yannamsettiaishwarya7448
      @yannamsettiaishwarya7448 2 года назад +1

      Thankyou sir.. you made my day..