๐ Python Operators โ Types, Usage & Examples
Python offers a rich set of operators that allow developers to perform operations on variables and values. Operators are the foundation of most computations in programming, and mastering them is essential for writing efficient Python code.
Whether you’re working with arithmetic, making decisions, or comparing values, Python provides built-in operators for all types of tasks โ making your code more expressive and readable.
โ Arithmetic Operators
These operators are used to perform mathematical operations.
| Operator | Description | Example | Output | 
|---|---|---|---|
| + | Addition | 3 + 2 | 5 | 
| - | Subtraction | 3 - 2 | 1 | 
| * | Multiplication | 3 * 2 | 6 | 
| / | Division | 3 / 2 | 1.5 | 
| // | Floor Division | 3 // 2 | 1 | 
| % | Modulus | 3 % 2 | 1 | 
| ** | Exponentiation | 2 ** 3 | 8 | 
๐ง  Note: Floor division // returns the largest integer less than or equal to the result.
๐งฎ Assignment Operators
Used to assign values to variables, often combined with arithmetic.
| Operator | Example | Equivalent | 
|---|---|---|
| = | x = 5 | x = 5 | 
| += | x += 3 | x = x + 3 | 
| -= | x -= 2 | x = x - 2 | 
| *= | x *= 2 | x = x * 2 | 
| /= | x /= 2 | x = x / 2 | 
| //= | x //= 2 | x = x // 2 | 
| %= | x %= 2 | x = x % 2 | 
| **= | x **= 2 | x = x ** 2 | 
๐งพ Comparison Operators
Used to compare values and return True or False.
| Operator | Description | Example | Result | 
|---|---|---|---|
| == | Equal to | 5 == 5 | True | 
| != | Not equal to | 5 != 3 | True | 
| > | Greater than | 5 > 3 | True | 
| < | Less than | 5 < 3 | False | 
| >= | Greater or equal | 5 >= 5 | True | 
| <= | Less or equal | 5 <= 3 | False | 
๐ค Logical Operators
Used to combine conditional statements.
| Operator | Description | Example | Result | 
|---|---|---|---|
| and | Returns Trueif both areTrue | True and False | False | 
| or | Returns Trueif one isTrue | True or False | True | 
| not | Inverts the result | not True | False | 
๐งฉ Bitwise Operators
Used to perform operations on binary values.
| Operator | Name | Example | Result | 
|---|---|---|---|
| & | AND | 5 & 3 | 1 | 
| ` | ` | OR | `5 | 
| ^ | XOR | 5 ^ 3 | 6 | 
| ~ | NOT | ~5 | -6 | 
| << | Left Shift | 5 << 1 | 10 | 
| >> | Right Shift | 5 >> 1 | 2 | 
๐ง Tip: Bitwise operations are useful in low-level programming and optimization techniques.
๐งช Identity Operators
Used to compare memory locations of two objects.
| Operator | Description | Example | Result | 
|---|---|---|---|
| is | True if both refer to same object | x is y | True/False | 
| is not | True if not same object | x is not y | True/False | 
x = [1, 2]
y = x
z = [1, 2]
print(x is y)    # True
print(x is z)    # False (different objects)
๐ฆ Membership Operators
Used to test if a value exists in a sequence (list, tuple, string, etc).
| Operator | Description | Example | Result | 
|---|---|---|---|
| in | True if found | 'a' in 'apple' | True | 
| not in | True if not found | 3 not in [1,2] | True | 
โ๏ธ Operator Precedence
Python evaluates expressions based on operator precedence (also called order of operations).
๐ Example:
result = 3 + 4 * 2     # Output: 11, not 14
๐ Precedence (High to Low):
- ()– Parentheses
- **– Exponentiation
- +,- -,- *,- /,- //,- %– Arithmetic
- ==,- !=,- >,- <,- >=,- <=– Comparison
- not,- and,- or– Logical
- is,- in, etc.
โ Summary
๐น Python operators allow efficient computation and logical decision-making.
๐น They’re categorized into arithmetic, assignment, comparison, logical, bitwise, identity, and membership.
๐น Operator precedence ensures that expressions are evaluated in the correct order.
๐น Knowing operators is crucial for writing clean, optimized, and bug-free Python code.
โ FAQs
Q1. What’s the difference between = and == in Python?= is an assignment operator, while == is a comparison operator.
Q2. Can I override operators in Python?
Yes, using magic methods (like __add__, __eq__, etc.) in classes.
Q3. Whatโs the result of True + True in Python?2 โ Because True is treated as 1.
Q4. What does is check in Python?
It checks whether two variables point to the same object in memory.
Share Now :
