Hello friends!!!!!

Addition of two complex numbers

PYTHON

11/9/20241 min read

'''

Polymorphism:- When a same operator is allowed to have different

meaning according to the context.

For example, If we take "+",

1. addition of two number is addition.

2. adding two string is concatenation.

3. adding two lists is called as merging.

'''

#addition of two complex numbers

class complex:

def __init__(self,real,img):

self.real = real

self.img = img

def show(self):

print(self.real,"i + ",self.img,"j")

def __add__(self,num2):

new_real = self.real + num2.real

new_img = self.img + num2.real

return complex(new_real, new_img)

num1 = complex(1,2)

num2 = complex(3,4)

num3 = num1.__add__(num2)

num1.show()

num2.show()

num3.show()

Output:

1 i + 2 j

3 i + 4 j

4 i + 5 j

=== Code Execution Successful ===