Skip to content

Instantly share code, notes, and snippets.

@MLWhiz
Created December 10, 2020 23:20
Show Gist options
  • Save MLWhiz/1790e82e2ddc875fb0b57a086383898a to your computer and use it in GitHub Desktop.
Save MLWhiz/1790e82e2ddc875fb0b57a086383898a to your computer and use it in GitHub Desktop.
import math
class Complex:
def __init__(self, re=0, im=0):
self.re = re
self.im = im
def __add__(self, other):
# If Int or Float Added, return a Complex number where float/int is added to the real part
if isinstance(other, int) or isinstance(other, float):
return Complex(self.re + other,self.im)
# If Complex Number added return a new complex number having a real and complex part
elif isinstance(other, Complex):
return Complex(self.re + other.re , self.im + other.im)
else:
raise TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment