{"id":9969,"date":"2021-06-22T16:28:42","date_gmt":"2021-06-22T10:58:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=9969"},"modified":"2021-11-22T18:40:36","modified_gmt":"2021-11-22T13:10:36","slug":"python-interview-questions-on-operators-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-interview-questions-on-operators-in-python\/","title":{"rendered":"Python Interview Questions on Operators in Python"},"content":{"rendered":"

We have compiled most frequently asked\u00a0Python Interview Questions<\/a>\u00a0which will help you with different expertise levels.<\/p>\n

Python Interview Questions on Operators in Python<\/h2>\n

What are operators?<\/strong>
\nOperators are required to perform various operations on data. They are special symbols that are required to carry out arithmetic and logical operations. The values on which the operator operates are called operands.
\nSo, if we say 10\/5=2
\nHere 7\u2019 is the operator that performs division and 10 and 5 are the operands. Python has the following operators defined for various operations:<\/p>\n

    \n
  1. Arithmetic Operators<\/li>\n
  2. Relational Operators<\/li>\n
  3. Logical\/Boolean Operators<\/li>\n
  4. Assignment Operators<\/li>\n
  5. Bitwise Operators<\/li>\n
  6. Membership Operators<\/li>\n
  7. Identity Operators<\/li>\n<\/ol>\n

    Question 1.
    \nWhat are Arithmetic Operators? What are various types of arithmetic operators that we can use in Python?
    \nAnswer:
    \nArithmetic operators are used to performing mathematical functions such as addition, subtraction, division, and multiplication. Various types of Arithmetic operators that we can use in Python are as follows:<\/p>\n

    \u2018+\u2019for Addition<\/p>\n

    >>> a = 9\r\n>>> b = 10\r\n>>> a + b\r\n19\r\n>>><\/pre>\n

    \u2018-\u2019 for Subtraction<\/p>\n

    >>> a = 9\r\n>>>b = 10\r\n>>> a - b\r\n-1\r\n>>><\/pre>\n

    ‘*’ for Multiplication<\/p>\n

    >>> a = 9\r\n>>> b = 10\r\n>>> a * b\r\n90\r\n>>><\/pre>\n

    ‘\/’ for division<\/p>\n

    >>> a = 9\r\n>>> b = 10\r\n>>> a\/b\r\n0.9\r\n>>><\/pre>\n

    \u201c%\u2019 for Modulus – provides the value of the remainder<\/p>\n

    >>> a = 9\r\n>>> b = 10\r\n>>> a % b\r\n9\r\n>>> a = 11\r\n>>> b = 2\r\n>>> a % b\r\n1\r\n>>><\/pre>\n

    ‘\/\/\u2019 for Floor division – a division of operands, provides integer value of quotient. The value after decimal points is removed.<\/p>\n

    >>> a = 9\r\n>>> b = 10\r\n>>> a \/\/ b\r\n0\r\n>>> a = 11\r\n>>> b = 2\r\n>>> a \/\/ b\r\n5\r\n>>><\/pre>\n

    \u2018**\u2019 for finding Exponential value<\/p>\n

    >>> a = 2\r\n>>> b = 3\r\n>>> a**b\r\n8\r\n>>>b**a\r\n9\r\n>>><\/pre>\n

    Question 2.
    \nWhat is the Arithmetic operator’s precedence in Python?
    \nAnswer:
    \nWhen more than one arithmetic operator appears in an expression the operations will execute in a specific order. In Python, the operation precedence follows as per the acronym PEMDAS.
    \nParenthesis
    \nExponent
    \nMultiplication
    \nDivision
    \nAddition
    \nSubtraction
    \n(2+2)\/2-2*2\/(2\/2)*2
    \n= 4\/2 -4\/1*2
    \n= 2-8
    \n= -6
    \n>>> (2+2)\/2-2*2\/(2\/2)*2
    \n-6.0<\/p>\n

    Question 3.
    \na = 2, b =4, c = 5, d = 4 Evaluate the following keeping Python\u2019s precedence of operators:<\/p>\n

      \n
    1. a+b+c+d<\/li>\n
    2. a+b*c+d 4<\/li>\n
    3. a\/b+c\/d<\/li>\n
    4. a+b*c+a\/b+d<\/li>\n<\/ol>\n

      Answer:<\/p>\n

      >>> a=2\r\n>>> b=4\r\n>>> c=5\r\n>>> d=4\r\n>>> a+b+c+d\r\n15\r\n>>> a+b*c+d\r\n26\r\n>>> a\/b+c\/d\r\n1.75\r\n>>> a+b*c+a\/b+d\r\n26.5\r\n>>><\/pre>\n

      Question 4.
      \nWhat are relational operators?
      \nAnswer:
      \nRelational operators are also known as conditional or comparison operators. Relational operators in Python are defined as follows:<\/p>\n

        \n
      1. ==: returns true if two operands are equal<\/li>\n
      2. !=: returns true if two operands are not equal<\/li>\n
      3. >: returns true if the left operand is greater than the right operand<\/li>\n
      4. <: returns true if the left operand is smaller than the right operand<\/li>\n
      5. >=: returns true if the left operand is greater than or equal to the right operand<\/li>\n
      6. <=: returns true if the left operand is smaller or equal to the right operand<\/li>\n<\/ol>\n
        >>> a = 5\r\n>>> b = 6\r\n>>> c = 7\r\n>>> d = 7\r\n>>> a == b\r\nFalse\r\n>>> c == d\r\nTrue\r\n>>> a ! = b\r\nTrue\r\n>>> c ! = d\r\nFalse\r\n>>>\r\n>>> a > b\r\nFalse\r\n>>> a < b\r\nTrue\r\n>>> a>=b\r\nFalse\r\n>>> c>=d\r\nTrue\r\n>>> a<=b\r\nTrue\r\n>>> c<=d\r\nTrue<\/pre>\n

        Question 5.
        \na = 5, b = 6, c =7, d=7 What will be the outcome for the following:<\/p>\n

          \n
        1. a<=b>=c<\/li>\n
        2. -a+b==c>d<\/li>\n
        3. b+c==6+d>=13<\/li>\n<\/ol>\n

          Answer:<\/p>\n

          >>> a<=b>=c\r\nFalse\r\n>>> -a+b==c>d\r\nFalse\r\n>>> b+c==6+d>=13\r\nTrue\r\n>>><\/pre>\n

          Question 6.
          \nWhat are assignment operators?
          \nAnswer:
          \nAssignment operators are used for assigning values to variables. Various types of assignment operators are as follows:<\/p>\n

            \n
          1. =: a = 5 means that a is assigned value of 5<\/li>\n
          2. += a += 5 is same as a = a+5<\/li>\n
          3. -=: a -= 5 is same as a = a – 5<\/li>\n
          4. *=. a *= 5 js same as a = a * 5<\/li>\n
          5. \/=: a \/= 5 is same as a = a\/5<\/li>\n
          6. %=: a %=5 is same as a = a%5<\/li>\n
          7. \/\/=: x \/\/= 5 is same as x= x\/\/5<\/li>\n
          8. **=\u2022 x **=5 js same as x = x**5<\/li>\n
          9. &=: x &= 5 is same as x = x&5<\/li>\n
          10. |=: x |= 5 is same as x = x|5<\/li>\n
          11. A=: x A= 5 is same as x = xA5<\/li>\n
          12. >>=: x >>= 5 is same as x = x>>5<\/li>\n
          13. <<=: x <<= 5 is same as x = x<<5<\/li>\n<\/ol>\n

            Question 7.
            \nIs a = a*2+6 same as a *= 2 + 6?
            \nAnswer:
            \nNo, a = a*2+6 is not same as a *= 2 + 6 this is because assign operator have lower precedence than the addition operator. So, if a = 5 then,
            \na = a *2+6 => a = 16
            \na *= 2 + 6 => a = 40<\/p>\n

            >>> a = 5\r\n>>> a = a *2+6\r\n>>> a\r\n16\r\n>>> a = 5\r\n>>> a*= 2+6\r\n>>> a\r\n40\r\n>>><\/pre>\n

            Question 8.
            \nWhat are logical operators?
            \nAnswer:
            \nLogical operators are generally used in control statements like if and while. They are used to control program flow. The logical operator evaluates a condition and returns \u201cTrue\u201d or \u201cFalse\u201d depending on whether the condition evaluates to True or False. Three logical operators in Python are as follows:<\/p>\n