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:
+→ operatorxandy→ operands
Types of Python Operators
Python supports the following types of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
Arithmetic Operators
Used to perform mathematical operations.
| Operator | Description |
|---|---|
+ | 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.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
python
x = 10
x += 5
print(x)
python
x *= 2
print(x)
Comparison Operators
Used to compare values and return Boolean results.
| Operator | Description |
|---|---|
== | 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.
| Operator | Meaning |
|---|---|
and | True if both are true |
or | True if at least one is true |
not | Reverses 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.
| Operator | Meaning |
|---|---|
is | Same object |
is not | Not 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.
| Operator | Meaning |
|---|---|
in | Value exists |
not in | Value 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.
| Operator | Name | |
|---|---|---|
& | 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
isand== - Write an expression using
and,or, andnot
This topic is core Python logic and appears everywhere—from basics to interviews.