We have compiled most frequently asked Python Interview Questions which 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 are called operands.

So, if we say 10/5=2

Here 7’ is the operator that performs division and 10 and 5 are the operands. Python has the following operators defined for various operations:

- Arithmetic Operators
- Relational Operators
- Logical/Boolean Operators
- Assignment Operators
- Bitwise Operators
- Membership Operators
- Identity Operators

Question 1.

What are Arithmetic Operators? What are various types of arithmetic operators that we can use in Python?

Answer:

Arithmetic 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:

‘+’for Addition

>>> a = 9 >>> b = 10 >>> a + b 19 >>>

‘-’ for Subtraction

>>> a = 9 >>>b = 10 >>> a - b -1 >>>

‘*’ for Multiplication

>>> a = 9 >>> b = 10 >>> a * b 90 >>>

‘/’ for division

>>> a = 9 >>> b = 10 >>> a/b 0.9 >>>

“%’ for Modulus – provides the value of the remainder

>>> a = 9 >>> b = 10 >>> a % b 9 >>> a = 11 >>> b = 2 >>> a % b 1 >>>

‘//’ for Floor division – a division of operands, provides integer value of quotient. The value after decimal points is removed.

>>> a = 9 >>> b = 10 >>> a // b 0 >>> a = 11 >>> b = 2 >>> a // b 5 >>>

‘**’ for finding Exponential value

>>> a = 2 >>> b = 3 >>> a**b 8 >>>b**a 9 >>>

- Operators in Java
- Python Data Persistence – Magic Methods
- Python Program to Evaluate a Postfix Expression Using Stack

Question 2.

What is the Arithmetic operator’s precedence in Python?

Answer:

When 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.

Parenthesis

Exponent

Multiplication

Division

Addition

Subtraction

(2+2)/2-2*2/(2/2)*2

= 4/2 -4/1*2

= 2-8

= -6

>>> (2+2)/2-2*2/(2/2)*2

-6.0

Question 3.

a = 2, b =4, c = 5, d = 4 Evaluate the following keeping Python’s precedence of operators:

- a+b+c+d
- a+b*c+d 4
- a/b+c/d
- a+b*c+a/b+d

Answer:

>>> a=2 >>> b=4 >>> c=5 >>> d=4 >>> a+b+c+d 15 >>> a+b*c+d 26 >>> a/b+c/d 1.75 >>> a+b*c+a/b+d 26.5 >>>

Question 4.

What are relational operators?

Answer:

Relational operators are also known as conditional or comparison operators. Relational operators in Python are defined as follows:

- ==: returns true if two operands are equal
- !=: returns true if two operands are not equal
- >: returns true if the left operand is greater than the right operand
- <: returns true if the left operand is smaller than the right operand
- >=: returns true if the left operand is greater than or equal to the right operand
- <=: returns true if the left operand is smaller or equal to the right operand

>>> a = 5 >>> b = 6 >>> c = 7 >>> d = 7 >>> a == b False >>> c == d True >>> a ! = b True >>> c ! = d False >>> >>> a > b False >>> a < b True >>> a>=b False >>> c>=d True >>> a<=b True >>> c<=d True

Question 5.

a = 5, b = 6, c =7, d=7 What will be the outcome for the following:

- a<=b>=c
- -a+b==c>d
- b+c==6+d>=13

Answer:

>>> a<=b>=c False >>> -a+b==c>d False >>> b+c==6+d>=13 True >>>

Question 6.

What are assignment operators?

Answer:

Assignment operators are used for assigning values to variables. Various types of assignment operators are as follows:

- =: a = 5 means that a is assigned value of 5
- += a += 5 is same as a = a+5
- -=: a -= 5 is same as a = a – 5
- *=. a *= 5 js same as a = a * 5
- /=: a /= 5 is same as a = a/5
- %=: a %=5 is same as a = a%5
- //=: x //= 5 is same as x= x//5
- **=• x **=5 js same as x = x**5
- &=: x &= 5 is same as x = x&5
- |=: x |= 5 is same as x = x|5
- A=: x A= 5 is same as x = xA5
- >>=: x >>= 5 is same as x = x>>5
- <<=: x <<= 5 is same as x = x<<5

Question 7.

Is a = a*2+6 same as a *= 2 + 6?

Answer:

No, 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,

a = a *2+6 => a = 16

a *= 2 + 6 => a = 40

>>> a = 5 >>> a = a *2+6 >>> a 16 >>> a = 5 >>> a*= 2+6 >>> a 40 >>>

Question 8.

What are logical operators?

Answer:

Logical 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 “True” or “False” depending on whether the condition evaluates to True or False. Three logical operators in Python are as follows:

- ‘and’
- ‘or’ and
- ‘not’

>>> a = True >>> b = False >>> a and b False >>> a or b True >>> not a False >>> not b True >>>

Question 9.

What are membership operators?

Answer:

The membership operators are used to check if a value exists in a sequence or not.

Two types of membership operators are as follows:

**in:**returns true if a value is found in a sequence**not in:**returns true if a value is not found in a sequence

>>> a = "Hello World" >>> "h" in a False >>> "h" not in a True >>> "H" not in a False >>>

Question 10.

What are bitwise operators?

Answer:

Bitwise operators work on bits and perform bit-by-bit operations. In Python, the following bit-wise operations are defined:

1. **AND – &**

2 & 3

2

2. **OR-|**

2|3

3

3. **One’s complement – ~**

>>> ~2

-3

4. **XOR -∧**

2∧3

1

5. **Right shift ->>**

2>>2

0

6. **Left shift -<<**

2<<2

8

Question 11.

What are identity operators?

Answer:

Identity 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:

**is:**return true if two operands are identical**is not:**returns true if two operands are not identical

>>> a = 3 >>> id(a) 140721094570896 >>> b = 3 >>> id (b) 140721094570896 >>> a is b True >>> a = 3 >>> b = 6 >>> c = b - a >>> id(c) 140721094570896 >>> a is c True >>> a = 4 >>> b = 8 >>> a is b False >>> a is not b True >>>

Question 12.

What is the difference between a = 10 and a= = 10?

Answer:

The 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 ‘Ti^te’ else it will return ‘False’.

Question 13.

What is an expression?

Answer:

A 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.

For example:

a = 6

a + b = 9

8/7

Question 14.

What are the basic rules of operator precedence in Python?

Answer:

The basic rule of operator precedence in Python is as follows:

- Expressions must be evaluated from left to right.
- Expressions of parenthesis are performed first.
- In Python the operation precedence follows as per the acronym PEMDAS:

- Parenthesis
- Exponent
- Multiplication
- Division
- Addition
- Subtraction

4. Mathematical operators are of higher precedence and the Boolean operators are of lower precedence. Hence, mathematical operations are performed before Boolean operations.

Question 15.

Arrange the following operators from high to low precedence:

- Assignment
- Exponent
- Addition and Subtraction
- Relational operators
- Equality operators
- Logical operators
- Multiplication, division, floor division, and modulus

Answer:

The precedence of operators from high to low is as follows:

- Exponent
- Multiplication, division, floor division, and modulus
- Addition and subtraction operators
- Relational operators
- Equality operators
- Assignment operators
- Logical Operators

Question 16.

Is it possible to change the order of evaluation in an expression?

Answer:

Yes, 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.

(2+4)*4

Question 17.

What is the difference between implicit expression and explicit expression?

Answer:

Conversion is the process of converting one data type into another. Two types of conversion in Python are as follows:

- Implicit type conversion
- Explicit type conversion

When Python automatically converts one data type to another it is called implicit conversion.

>>> a = 7 >>> type(a) Cclass 'int'> >>> b = 8.7 >>> type(b) <class 'float'> >>> type(a+b) <class 'float' > >>>

Explicit conversion is when the developer has to explicitly convert datatype of an object to carry out an operation.

>>> c = "12" >>> type(c) <class 'str'> >>> d = 12 # addition of string and integer will generate error >>> c+d Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> c+d TypeError: can only concatenate str (not "int") to str # convert string to integer and then add >>> int (c) +d 24 # convert integer to string and then perform concatenation >>> c+str(d) '1212' >>>

Question 18.

What is a statement?

Answer:

A complete unit of code that a Python interpreter can execute is called a statement.

Question 19.

What is an input statement?

Answer:

The input statement is used to get user input from the keyboard. The syntax for input() function is as follows:

input(prompt)

The prompt is a strong message for the user.

>>> a = input ("Please enter your message here :") Please enter your message here: It is a beautiful day >>> a ' It is a beautiful day' >>>

Whenever an input function is called, the program comes on hold till an input is provided by the user. The **input( )** function converts the user input to a string and then returns it to the calling program.

Question 20.

Look at the following code:

num1 = input ("Enter the first number: ") num2 = input("Enter the second number: ") print(num1 + num2)

When the code is executed the user provides the following values:

Enter the first number: 67 Enter the second number: 78 What would be the output?

Answer:

The 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 ‘67’ and ‘78’ and the ‘+’ operator concatenates the two strings giving ‘6778’ as the answer. To add the two numbers they must be first converted to an integer value. Hence, the code requires slight modification:

num1 = input ("Enter the first number: ") num2 = input("Enter the second number: ") print(int(num1) + int(num2))

**Output:**

Enter the first number: 67 Enter the second number: 78 145 >>>

Question 21.

What is the Associativity of Python Operators? What are non-associative operators?

Answer:

Associativity 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.

Operators like assignment or comparison operators have no associativity and are known as Nonassociative operators.