class Complex_number:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def __add__(self, right):
return Complex_number(self.real + right.real,
self.imaginary + right.imaginary)
def __iadd__(self, right):
"""Overrides the += operator."""
self.real += right.real
self.imaginary += right.imaginary
return self
def __repr__(self):
return (f'({self.real}' +
(' + ' if self.imaginary >= 0 else ' - ') +
f'{abs(self.imaginary)}i)')
x = Complex_number(real = 2, imaginary = 4)
x
y = Complex_number(real = 5, imaginary = -1)
y
x + y
x += y
x
y