This chapter contains the most frequently asked interview questions on Python Operators.
It covers:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
- Operator Precedence
- Output-Based Questions
- Frequently Asked Programs
- Viva Questions
An Operator is a symbol that performs an operation on one or more operands and produces a result.
Example
a = 10
b = 20
print(a + b)Output
30
An operand is the value or variable on which an operator performs an operation.
Example
a + bHere,
+→ Operatoraandb→ Operands
Python provides seven major categories of operators.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic operators perform mathematical operations.
Examples:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Floor Division (
//) - Modulus (
%) - Exponent (
**)
/ |
// |
|---|---|
| Returns floating-point result | Returns floor division result |
Example
print(10 / 3)Output
3.3333333333333335
print(10 // 3)Output
3
The Modulus operator (%).
Example
print(25 % 4)Output
1
Exponent operator (**).
Example
print(5 ** 3)Output
125
Assignment operators assign values or expression results to variables.
Example
x = 10=assigns a value.+=adds a value and updates the variable.
Example
x = 10
x += 5
print(x)Output
15
Comparison operators compare two values and return either True or False.
= |
== |
|---|---|
| Assignment Operator | Comparison Operator |
Example
print(10 == 10)Output
True
Boolean (True or False).
andornot
Only when both conditions are True.
When at least one condition is True.
It reverses the Boolean value.
Example
print(not True)Output
False
Bitwise operators perform operations on binary values.
- &
- |
- ^
- ~
- <<
-
Python uses the formula:
~n = -(n + 1)
Therefore,
~5 = -6
Membership operators check whether a value exists in a sequence.
Operators:
innot in
- Strings
- Lists
- Tuples
- Sets
- Dictionaries
Boolean (True or False).
Identity operators compare object references.
Operators:
isis not
== |
is |
|---|---|
| Compares values | Compares object identity |
The is operator is commonly used when comparing with None.
Example
value = None
print(value is None)Output
True
Operator precedence determines which operator is evaluated first in an expression.
Associativity determines the evaluation order of operators having the same precedence.
Parentheses ().
Exponent operator (**).
print(10 + 5 * 2)Output
20
print((10 + 5) * 2)Output
30
print(10 // 3)Output
3
print(10 % 3)Output
1
print(2 ** 3)Output
8
print(5 & 3)Output
1
print(5 | 3)Output
7
print(5 ^ 3)Output
6
print("P" in "Python")Output
True
print("Java" not in "Python")Output
True
a = [1, 2]
b = a
print(a is b)Output
True
Perform all arithmetic operations.
a = 20
b = 10
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** 2)Check voting eligibility.
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible")Check whether a course exists.
courses = ["Java", "Python", "SQL"]
print("Python" in courses)Compare two objects.
a = [1, 2]
b = a
print(a is b)Demonstrate operator precedence.
print(10 + 5 * 2)
print((10 + 5) * 2)- What is an operator?
- What is an operand?
- Name the different types of operators.
- What is the difference between
/and//? - What is the modulus operator?
- What is an assignment operator?
- Difference between
=and==. - Difference between
==andis. - What are logical operators?
- What are bitwise operators?
- What are membership operators?
- What are identity operators?
- What is operator precedence?
- What is associativity?
- Which operator has the highest precedence?
- Which operator has right-to-left associativity?
- Arithmetic → Mathematical calculations
- Assignment → Assign values
- Comparison → Returns
TrueorFalse - Logical →
and,or,not - Bitwise → Binary operations
- Membership →
in,not in - Identity →
is,is not - Parentheses have the highest precedence.
- Exponent (
**) is right-to-left associative.
In this chapter, you learned:
- Basic operator concepts.
- Arithmetic operators.
- Assignment operators.
- Comparison operators.
- Logical operators.
- Bitwise operators.
- Membership operators.
- Identity operators.
- Operator precedence.
- Output-based interview questions.
- Frequently asked interview programs.
- Viva questions.
- Quick revision.