Python Operators

Operators in Python are used to perform operations on variables and values. They are the backbone of logic, calculations, comparisons, and decision-making in Python programs.
This topic covers Python operators from basics to advanced, including tricky and interview-level concepts.

What Are Operators?

Operators are special symbols that perform operations on operands (values or variables).
python
x = 10
y = 5
print(x + y)
Here:
  • + → operator
  • x and y → operands

Types of Python Operators

Python supports the following types of operators:
  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

Arithmetic Operators

Used to perform mathematical operations.
OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
//Floor Division
python
a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
python
print(a % b)
print(a ** b)
print(a // b)

Assignment Operators

Used to assign values to variables.
OperatorExampleMeaning
=x = 5Assign
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
python
x = 10
x += 5
print(x)
python
x *= 2
print(x)

Comparison Operators

Used to compare values and return Boolean results.
OperatorDescription
==Equal
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
python
a = 10
b = 20

print(a == b)
print(a != b)
print(a < b)
print(a >= b)

Logical Operators

Used to combine Boolean expressions.
OperatorMeaning
andTrue if both are true
orTrue if at least one is true
notReverses result
python
print(True and False)
python
print(True or False)
python
print(not True)
python
print(10 > 5 and 5 > 2)

Identity Operators (Tricky)

Used to compare memory locations, not values.
OperatorMeaning
isSame object
is notNot same object
python
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)
print(a is c)
print(a == c)

Membership Operators

Used to test whether a value exists in a sequence.
OperatorMeaning
inValue exists
not inValue does not exist
python
print("a" in "apple")
python
print(5 in [1, 2, 3, 5])
python
print("x" not in "python")

Bitwise Operators (Advanced)

Operate on binary values.
OperatorName
&AND
``OR
^XOR
~NOT
<<Left Shift
>>Right Shift
python
a = 5      # 101
b = 3      # 011

print(a & b)
print(a | b)
print(a ^ b)
python
print(a << 1)
print(a >> 1)

Operator Precedence (Very Important)

Python follows a specific order while evaluating expressions.
python
print(10 + 5 * 2)
python
print((10 + 5) * 2)
python
print(5 > 3 and 3 < 10)
Key Rule

Parentheses () always have the highest priority.


Tricky Operator Examples (Interview Level)

python
print(10 > 5 > 2)
python
print(True + True)
python
print(False * 10)
python
print(0 or 10)
python
print(10 and 0)

Common Operator Mistakes

Using is instead of ==

python
print(1000 is 1000)
Unreliable ❌

Correct Way

python
print(1000 == 1000)

Summary

  • Operators perform actions on data
  • Python supports multiple operator types
  • Arithmetic and comparison operators are most common
  • Identity and logical operators are tricky
  • Operator precedence affects results
  • Understanding operators is critical for logic building

Practice (Hard)

  • Predict output of chained comparisons
  • Use bitwise operators on numbers
  • Find difference between is and ==
  • Write an expression using and, or, and not
This topic is core Python logic and appears everywhere—from basics to interviews.