Identity Operators<\/li>\n<\/ol>\nQuestion 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>\nQuestion 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
\na+b+c+d<\/li>\n a+b*c+d 4<\/li>\n a\/b+c\/d<\/li>\n a+b*c+a\/b+d<\/li>\n<\/ol>\nAnswer:<\/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>\nQuestion 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==: returns true if two operands are equal<\/li>\n !=: returns true if two operands are not equal<\/li>\n >: returns true if the left operand is greater than the right operand<\/li>\n <: returns true if the left operand is smaller than the right operand<\/li>\n >=: returns true if the left operand is greater than or equal to the right operand<\/li>\n <=: 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>\nQuestion 5. \na = 5, b = 6, c =7, d=7 What will be the outcome for the following:<\/p>\n
\na<=b>=c<\/li>\n -a+b==c>d<\/li>\n b+c==6+d>=13<\/li>\n<\/ol>\nAnswer:<\/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>\nQuestion 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=: a = 5 means that a is assigned value of 5<\/li>\n += a += 5 is same as a = a+5<\/li>\n -=: a -= 5 is same as a = a – 5<\/li>\n *=. a *= 5 js same as a = a * 5<\/li>\n \/=: a \/= 5 is same as a = a\/5<\/li>\n %=: a %=5 is same as a = a%5<\/li>\n \/\/=: x \/\/= 5 is same as x= x\/\/5<\/li>\n **=\u2022 x **=5 js same as x = x**5<\/li>\n &=: x &= 5 is same as x = x&5<\/li>\n |=: x |= 5 is same as x = x|5<\/li>\n A=: x A= 5 is same as x = xA5<\/li>\n >>=: x >>= 5 is same as x = x>>5<\/li>\n <<=: x <<= 5 is same as x = x<<5<\/li>\n<\/ol>\nQuestion 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>\nQuestion 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
\n\u2018and\u2019<\/li>\n \u2018or\u2019 and<\/li>\n \u2018not\u2019<\/li>\n<\/ul>\n>>> a = True\r\n>>> b = False\r\n>>> a and b\r\nFalse\r\n>>> a or b\r\nTrue\r\n>>> not a\r\nFalse\r\n>>> not b\r\nTrue\r\n>>><\/pre>\nQuestion\u00a0 9. \nWhat are membership operators? \nAnswer: \nThe membership operators are used to check if a value exists in a sequence or not. \nTwo types of membership operators are as follows:<\/p>\n
\nin:<\/strong> returns true if a value is found in a sequence<\/li>\nnot in:<\/strong> returns true if a value is not found in a sequence<\/li>\n<\/ol>\n>>> a = \"Hello World\"\r\n>>> \"h\" in a\r\nFalse\r\n>>> \"h\" not in a\r\nTrue\r\n>>> \"H\" not in a\r\nFalse\r\n>>><\/pre>\nQuestion 10. \nWhat are bitwise operators? \nAnswer: \nBitwise operators work on bits and perform bit-by-bit operations. In Python, the following bit-wise operations are defined: \n1. AND – &<\/strong> \n2 & 3 \n2 \n2. OR-|<\/strong> \n2|3 \n3 \n3. One\u2019s complement – ~<\/strong> \n>>> ~2 \n-3 \n4. XOR -\u2227<\/strong> \n2\u22273 \n1 \n5. Right shift ->><\/strong> \n2>>2 \n0 \n6. Left shift -<<<\/strong> \n2<<2 \n8<\/p>\nQuestion 11. \nWhat are identity operators? \nAnswer: \nIdentity operators are used to verifying whether two values are on the same part of the memory or not. There are two types of identity operators:<\/p>\n
\nis:<\/strong> return true if two operands are identical<\/li>\nis not:<\/strong> returns true if two operands are not identical<\/li>\n<\/ol>\n>>> a = 3\r\n>>> id(a)\r\n140721094570896\r\n>>> b = 3\r\n>>> id (b)\r\n140721094570896\r\n>>> a is b\r\nTrue\r\n>>> a = 3\r\n>>> b = 6\r\n>>> c = b - a\r\n>>> id(c)\r\n140721094570896\r\n>>> a is c\r\nTrue\r\n>>> a = 4\r\n>>> b = 8\r\n>>> a is b\r\nFalse\r\n>>> a is not b\r\nTrue\r\n>>><\/pre>\nQuestion 12. \nWhat is the difference between a = 10 and a= = 10? \nAnswer: \nThe expression a = 10 assigns the value 10 to variable a, whereas a == 10 checks if the value of a is equal to 10 or not. If yes then it returns \u2018Ti^te\u2019 else it will return \u2018False\u2019.<\/p>\n
Question 13. \nWhat is an expression? \nAnswer: \nA logical line of code that we write while programing, is called expressions. An expression can be broken into operator and operands. It is therefore said that an expression is a combination of one or more operands and zero or more operators that are together used to compute a value. \nFor example: \na = 6 \na + b = 9 \n8\/7<\/p>\n
Question 14. \nWhat are the basic rules of operator precedence in Python? \nAnswer: \nThe basic rule of operator precedence in Python is as follows:<\/p>\n
\nExpressions must be evaluated from left to right.<\/li>\n Expressions of parenthesis are performed first.<\/li>\n In Python the operation precedence follows as per the acronym PEMDAS:<\/li>\n<\/ol>\n\nParenthesis<\/li>\n Exponent<\/li>\n Multiplication<\/li>\n Division<\/li>\n Addition<\/li>\n Subtraction<\/li>\n<\/ul>\n4. Mathematical operators are of higher precedence and the Boolean operators are of lower precedence. Hence, mathematical operations are performed before Boolean operations.<\/p>\n
Question 15. \nArrange the following operators from high to low precedence:<\/p>\n
\nAssignment<\/li>\n Exponent<\/li>\n Addition and Subtraction<\/li>\n Relational operators<\/li>\n Equality operators<\/li>\n Logical operators<\/li>\n Multiplication, division, floor division, and modulus<\/li>\n<\/ol>\nAnswer: \nThe precedence of operators from high to low is as follows:<\/p>\n
\nExponent<\/li>\n Multiplication, division, floor division, and modulus<\/li>\n Addition and subtraction operators<\/li>\n Relational operators<\/li>\n Equality operators<\/li>\n Assignment operators<\/li>\n Logical Operators<\/li>\n<\/ol>\nQuestion 16. \nIs it possible to change the order of evaluation in an expression? \nAnswer: \nYes, it is possible to change the order of evaluation of an expression. Suppose you want to perform addition before multiplication in an expression, then you can simply put the addition expression in parenthesis. \n(2+4)*4<\/p>\n
Question 17. \nWhat is the difference between implicit expression and explicit expression? \nAnswer: \nConversion is the process of converting one data type into another. Two types of conversion in Python are as follows:<\/p>\n
\nImplicit type conversion<\/li>\n Explicit type conversion<\/li>\n<\/ol>\nWhen Python automatically converts one data type to another it is called implicit conversion.<\/p>\n
>>> a = 7\r\n>>> type(a)\r\nCclass 'int'>\r\n>>> b = 8.7\r\n>>> type(b)\r\n<class 'float'>\r\n>>> type(a+b)\r\n<class 'float' >\r\n>>><\/pre>\nExplicit conversion is when the developer has to explicitly convert datatype of an object to carry out an operation.<\/p>\n
>>> c = \"12\"\r\n>>> type(c)\r\n<class 'str'>\r\n>>> d = 12\r\n# addition of string and integer will generate error\r\n>>> c+d\r\nTraceback (most recent call last):\r\nFile \"<pyshell#43>\", line 1, in <module> c+d\r\nTypeError: can only concatenate str (not \"int\") to str\r\n# convert string to integer and then add\r\n>>> int (c) +d 24\r\n# convert integer to string and then perform concatenation\r\n>>> c+str(d)\r\n'1212'\r\n>>><\/pre>\nQuestion 18. \nWhat is a statement? \nAnswer: \nA complete unit of code that a Python interpreter can execute is called a statement.<\/p>\n
Question 19. \nWhat is an input statement? \nAnswer: \nThe input statement is used to get user input from the keyboard. The syntax for input() function is as follows: \ninput(prompt) \nThe prompt is a strong message for the user.<\/p>\n
>>> a = input (\"Please enter your message here :\")\r\nPlease enter your message here: It is a beautiful\r\nday\r\n>>> a\r\n' It is a beautiful day'\r\n>>><\/pre>\nWhenever an input function is called, the program comes on hold till an input is provided by the user. The input( )<\/strong> function converts the user input to a string and then returns it to the calling program.<\/p>\nQuestion 20. \nLook at the following code:<\/p>\n
num1 = input (\"Enter the first number: \")\r\nnum2 = input(\"Enter the second number: \")\r\nprint(num1 + num2)<\/pre>\nWhen the code is executed the user provides the following values: \nEnter the first number: 67 Enter the second number: 78 What would be the output? \nAnswer: \nThe output will be 6778. This is because the input() function converts the user input into a string and then returns it to the calling program. So, even though the users have entered integer values, the input() function has returned string values \u201867\u2019 and \u201878\u2019 and the ‘+\u2019 operator concatenates the two strings giving \u20186778\u2019 as the answer. To add the two numbers they must be first converted to an integer value. Hence, the code requires slight modification:<\/p>\n
num1 = input (\"Enter the first number: \")\r\nnum2 = input(\"Enter the second number: \")\r\nprint(int(num1) + int(num2))<\/pre>\nOutput:<\/strong><\/p>\nEnter the first number: 67\r\nEnter the second number: 78\r\n145\r\n>>><\/pre>\nQuestion 21. \nWhat is the Associativity of Python Operators? What are non-associative operators? \nAnswer: \nAssociativity defines the order in which an expression will be evaluated if it has more than one operator having the same precedence. In such a case generally left to right associativity is followed. \nOperators like assignment or comparison operators have no associativity and are known as Nonassociative operators.<\/p>\n","protected":false},"excerpt":{"rendered":"
We have compiled most frequently asked\u00a0Python Interview Questions\u00a0which will help you with different expertise levels. Python Interview Questions on Operators in Python What are operators? Operators 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 …<\/p>\n
Python Interview Questions on Operators in Python<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Interview Questions on Operators in Python - Python Programs<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n