- Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Types of Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| // | Floor Division | a // b |
| % | Modulus (remainder) | a % b |
| ** | Exponentiation | a ** b |
Example
a = 10
b = 3print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.33
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponent:", a ** b) # 1000
Key Points
/always returns a float//removes the decimal part%is useful for checking even/odd numbers
- Comparison Operators
Comparison operators compare two values and return a Boolean result (True or False).
Operators
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
x = 5
y = 10print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
Use Case
Used in decision-making statements like if, while.
- Logical Operators
Logical operators are used to combine multiple conditions.
Operators
| Operator | Description |
|---|---|
| and | True if both conditions are true |
| or | True if at least one condition is true |
| not | Reverses the condition |
Example
a = True
b = Falseprint(a and b) # False
print(a or b) # True
print(not a) # False
Real Example
age = 20
print(age > 18 and age < 60) # True
- Assignment Operators
Assignment operators are used to assign values and update variables.
Operators
| Operator | Example | Meaning |
|---|---|---|
| = | x = 5 | Assign |
| += | x += 3 | x = x + 3 |
| -= | x -= 2 | x = x – 2 |
| *= | x *= 4 | x = x * 4 |
| /= | x /= 2 | x = x / 2 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 2 | x = x // 2 |
| **= | x **= 2 | x = x ** 2 |
Example
x = 10
x += 5
x *= 2
print(x) # 30
Advantage
- Reduces code length
- Improves readability
- Bitwise Operators
Bitwise operators work on binary representations of numbers.
Operators
| Operator | Meaning |
|---|---|
| & | AND |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
Example
a = 5 # 101
b = 3 # 011print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6
print(~a) # -6
print(a << 1) # 10
print(a >> 1) # 2
Explanation
&compares bits and returns 1 if both bits are 1|returns 1 if at least one bit is 1<<shifts bits to the left (multiplies by 2)
- Membership Operators
Membership operators check whether a value exists in a sequence.
Operators
innot in
Example
nums = [1, 2, 3, 4]print(2 in nums) # True
print(5 not in nums) # True
Use Case
- Searching elements in lists, strings, tuples
- Identity Operators
Identity operators check whether two variables refer to the same object in memory.
Operators
isis not
Example
a = [1, 2]
b = a
c = [1, 2]print(a is b) # True
print(a is c) # False
print(a == c) # True
Key Difference
==checks value equalityischecks memory location
Conclusion
Operators play a crucial role in Python programming by enabling calculations, comparisons, and logical decision-making. From simple arithmetic to complex bitwise operations, operators are used in almost every Python program.
A strong understanding of operators allows learners to write efficient, clean, and logical code. Mastering this topic will make it easier to understand control flow statements, loops, and advanced programming concepts.
Leave a Reply