Python Operators
5 min read ·
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).
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 |
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 |
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 |
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 |
Identity Operators (Tricky)
Used to compare memory locations, not values.
| Operator | Meaning |
|---|---|
is | Same object |
is not | Not same object |
Membership Operators
Used to test whether a value exists in a sequence.
| Operator | Meaning |
|---|---|
in | Value exists |
not in | Value does not exist |
Bitwise Operators (Advanced)
Operate on binary values.
| Operator | Name | |
|---|---|---|
& | AND | |
| ` | ` | OR |
^ | XOR | |
~ | NOT | |
<< | Left Shift | |
>> | Right Shift |
Operator Precedence (Very Important)
Python follows a specific order while evaluating expressions.
Key Rule
Parentheses () always have the highest priority.
Tricky Operator Examples (Interview Level)
Common Operator Mistakes
Using is instead of ==
Unreliable ❌
Correct Way
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.