✅ Python Operators – Complete Guide with Examples
🔹 What are Operators in Python?
Operators are symbols used to perform operations on variables and values.
Python has several categories of operators.
a = 10
b = 5
print(a + b) # Output: 15
🔸 1. Arithmetic Operators
Used to perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
** |
Exponentiation | a ** b |
// |
Floor Division | a // b |
✅ Example:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.33
print(a % b) # 1
🔸 2. Assignment Operators
Used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
= |
Assign | a = 5 |
+= |
Add and assign | a += 2 |
-= |
Subtract and assign | a -= 3 |
*= |
Multiply and assign | a *= 4 |
/= |
Divide and assign | a /= 2 |
%= |
Modulus and assign | a %= 3 |
✅ Example:
a = 5
a += 2 # a = a + 2 → 7
print(a) # 7
🔸 3. Comparison Operators
Used to compare two values and return True or False.
| Operator | Description | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater or equal | a >= b |
<= |
Less or equal | a <= b |
✅ Example:
a = 10
b = 5
print(a > b) # True
print(a == b) # False
🔸 4. Logical Operators
Used to combine conditional statements.
| Operator | Description | Example |
|---|---|---|
and |
True if both | a > 5 and b < 5 |
or |
True if any one | a > 5 or b < 2 |
not |
Reverse the result | not(a > b) |
✅ Example:
a = 10
b = 5
print(a > 5 and b < 10) # True
print(a < 5 or b == 5) # True
print(not a == b) # True
🔸 5. Identity Operators
Check if two variables point to the same object.
| Operator | Description | Example |
|---|---|---|
is |
Same identity | a is b |
is not |
Different identity | a is not b |
✅ Example:
x = [1, 2]
y = [1, 2]
z = x
print(x is z) # True
print(x is y) # False
print(x is not y) # True
🔸 6. Membership Operators
Check if a value exists in a sequence (list, string, tuple).
| Operator | Description | Example |
|---|---|---|
in |
Present in sequence | "a" in "Arvind" |
not in |
Not present | "z" not in "AI" |
✅ Example:
skills = ["Python", "AI", "ML"]
print("Python" in skills) # True
print("Java" not in skills) # True
🔸 7. Bitwise Operators
Operate on binary numbers (bit-level).
| Operator | Description | Example |
|---|---|---|
& |
AND | a & b |
| ` | ` | OR |
^ |
XOR | a ^ b |
~ |
NOT | ~a |
<< |
Left Shift | a << 1 |
>> |
Right Shift | a >> 1 |
✅ Example:
a = 5 # 0101
b = 3 # 0011
print(a & b) # 1
print(a | b) # 7
print(~a) # -6
✅ Summary Table
| Category | Operators |
|---|---|
| Arithmetic | + - * / % // ** |
| Assignment | = += -= *= /= |
| Comparison | == != > < >= <= |
| Logical | and or not |
| Identity | is, is not |
| Membership | in, not in |
| Bitwise | `& |
✅ Real-World Use Example:
username = "arvind"
password = "1234"
if username == "arvind" and password == "1234":
print("Login successful")
else:
print("Access denied")
✅ Best Practices
- Use parentheses for clarity:
(a > 5 and b < 10) - Don't confuse
=(assign) with==(compare) - Use
isfor identity (not equality) - Always write readable conditions
0 Comments