1.
Arithmetic Operators Demo
python
CopyEdit
def arithmetic_operators(a, b):
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
print("Floor Division:", a // b)
# Example usage
arithmetic_operators(10, 3)
✅ 2. Comparison Operators Demo
python
CopyEdit
def comparison_operators(a, b):
print("Equal:", a == b)
print("Not Equal:", a != b)
print("Greater Than:", a > b)
print("Less Than:", a < b)
print("Greater Than or Equal To:", a >= b)
print("Less Than or Equal To:", a <= b)
comparison_operators(10, 20)
✅ 3. Logical Operators Demo
python
CopyEdit
def logical_operators(x, y):
print("x and y:", x and y)
print("x or y:", x or y)
print("not x:", not x)
logical_operators(True, False)
✅ 4. Bitwise Operators Demo
python
CopyEdit
def bitwise_operators(a, b):
print("Bitwise AND:", a & b)
print("Bitwise OR:", a | b)
print("Bitwise XOR:", a ^ b)
print("Bitwise NOT (~a):", ~a)
print("Left Shift a by 2:", a << 2)
print("Right Shift b by 2:", b >> 2)
bitwise_operators(10, 4)
✅ 5. Assignment Operators Demo
python
CopyEdit
def assignment_operators():
a=5
print("Initial a:", a)
a += 3
print("After a += 3:", a)
a *= 2
print("After a *= 2:", a)
a -= 4
print("After a -= 4:", a)
a /= 2
print("After a /= 2:", a)
a %= 3
print("After a %= 3:", a)
assignment_operators()
✅ 6. Identity and Membership Operators Demo
python
CopyEdit
def identity_membership_demo():
x = [1, 2, 3]
y = [1, 2, 3]
z=x
print("x is z:", x is z) # True
print("x is y:", x is y) # False
print("x == y:", x == y) # True
print("2 in x:", 2 in x)
print("4 not in x:", 4 not in x)
identity_membership_demo()
✅ 7. Interactive Operator Menu Program
python
CopyEdit
def operator_menu():
print("Operator Menu:")
print("1. Arithmetic")
print("2. Comparison")
print("3. Logical")
print("4. Bitwise")
choice = int(input("Choose option (1-4): "))
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if choice == 1:
arithmetic_operators(a, b)
elif choice == 2:
comparison_operators(a, b)
elif choice == 3:
logical_operators(bool(a), bool(b))
elif choice == 4:
bitwise_operators(a, b)
else:
print("Invalid choice!")
# Call interactive menu
# operator_menu() # Uncomment to use
Would you like a specific example involving operator overloading, or should I include
programs for custom classes using operators?
You said:
yes
ChatGPT said:
Great! Let's explore operator overloading in Python with custom classes. Operator
overloading allows you to redefine how operators like +, -, *, etc., behave for objects of your
own classes.
✅ 8. Operator Overloading Example with a Vector Class
python
CopyEdit
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# Overload + operator
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# Overload - operator
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
# Overload * operator (scalar multiplication)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
# Overload == operator
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# String representation
def __str__(self):
return f"Vector({self.x}, {self.y})"
# Example usage
v1 = Vector(3, 4)
v2 = Vector(1, 2)
print("v1:", v1)
print("v2:", v2)
print("v1 + v2:", v1 + v2)
print("v1 - v2:", v1 - v2)
print("v1 * 3:", v1 * 3)
print("v1 == v2:", v1 == v2)
✅ 9. Operator Overloading Example with a ComplexNumber Class
python
CopyEdit
class ComplexNumber:
def __init__(self, real, imag):
[Link] = real
[Link] = imag
def __add__(self, other):
return ComplexNumber([Link] + [Link], [Link] + [Link])
def __sub__(self, other):
return ComplexNumber([Link] - [Link], [Link] - [Link])
def __str__(self):
return f"{[Link]} + {[Link]}i"
# Example usage
c1 = ComplexNumber(2, 3)
c2 = ComplexNumber(4, 5)
print("c1:", c1)
print("c2:", c2)
print("c1 + c2:", c1 + c2)
print("c1 - c2:", c1 - c2)
Would you like to see operator overloading with comparison (<, >, etc.), or even in real-world
classes like a BankAccount or Matrix?
You said:
yes
ChatGPT said:
Perfect! Let's build on operator overloading with more real-world examples and also
demonstrate comparison operator overloading using:
1. A BankAccount class with arithmetic operators
2. A Matrix class with +, -, and *
3. A class that uses <, >, <=, >=, etc.
✅ 10. Operator Overloading in a BankAccount Class
python
CopyEdit
class BankAccount:
def __init__(self, balance):
[Link] = balance
def __add__(self, other):
return BankAccount([Link] + [Link])
def __sub__(self, other):
return BankAccount([Link] - [Link])
def __str__(self):
return f"BankAccount Balance: ₹{[Link]}"
# Example usage
acc1 = BankAccount(5000)
acc2 = BankAccount(2000)
acc3 = acc1 + acc2
acc4 = acc1 - acc2
print(acc1)
print(acc2)
print("After adding accounts:", acc3)
print("After subtracting accounts:", acc4)
✅ 11. Matrix Addition and Subtraction (2D List Implementation)
python
CopyEdit
class Matrix:
def __init__(self, data):
[Link] = data
def __add__(self, other):
result = []
for i in range(len([Link])):
row = [[Link][i][j] + [Link][i][j] for j in range(len([Link][0]))]
[Link](row)
return Matrix(result)
def __sub__(self, other):
result = []
for i in range(len([Link])):
row = [[Link][i][j] - [Link][i][j] for j in range(len([Link][0]))]
[Link](row)
return Matrix(result)
def __str__(self):
return "\n".join(str(row) for row in [Link])
# Example usage
m1 = Matrix([[1, 2], [3, 4]])
m2 = Matrix([[5, 6], [7, 8]])
print("Matrix 1:")
print(m1)
print("Matrix 2:")
print(m2)
print("Addition Result:")
print(m1 + m2)
print("Subtraction Result:")
print(m1 - m2)
✅ 12. Comparison Operators with a Student Class (based on marks)
python
CopyEdit
class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks
def __lt__(self, other):
return [Link] < [Link]
def __gt__(self, other):
return [Link] > [Link]
def __eq__(self, other):
return [Link] == [Link]
def __str__(self):
return f"{[Link]} ({[Link]} marks)"
# Example usage
s1 = Student("Alice", 85)
s2 = Student("Bob", 92)
print(s1)
print(s2)
print("Is Alice < Bob?", s1 < s2)
print("Is Alice > Bob?", s1 > s2)
print("Is Alice == Bob?", s1 == s2)