title: Practicing add/sub/mul two complex number by python
body: <p>I am trying to add/sub/multiply two complex numbers. Terminal said there is a SyntaxError at &quot;ComplexCompute&quot;.
  What does that mean? Thanks.</p>
  <pre><code>Class ComplexCompute (object):
      def __init__(self, realPart, imagPart):
          self.realPart = realPart
          self.imagPart = imagPart
      def __add__(self, other)
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = self.imagPart
          resultR = r1+r2
          resultI = i1+i2
          result = complex(resultR, resultI)
          return result
      def __sub__(self, other)
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = self.imagPart
          resultR = r1-r2
          resultI = i1-i2
          result = complex(resultR, resultI)
          return result
      def __mul__(self, other)
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = self.imagPart
          resultR = (r1*r2-i1*i2)
          resultI = (r1*i2+r2*i1)
          result = complex(resultR, resultI)
          return result    
  c1 = ComplexCompute(2,3)
  c2 = ComplexCompute(1,4)
  print c1+c2
  print c1-c2
  print c1*c2
  </code></pre>
  <p>I edited the name of Class in some method.
  But the terminal showed:</p>
  <blockquote>
  <p>&lt;<strong>main</strong>.Complex object at 0x1005d8b90&gt;</p>
  <p>&lt;<strong>main</strong>.Complex object at 0x1005d8b90&gt;</p>
  <p>&lt;<strong>main</strong>.Complex object at 0x1005d8b90&gt;</p>
  </blockquote>
  <pre><code>class Complex (object):
      def __init__(self, realPart, imagPart):
          self.realPart = realPart
          self.imagPart = imagPart
  
      def __add__(self, other):
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart 
          resultR = r1+r2
          resultI = i1+i2
          result = Complex(resultR, resultI)
          return result
  
      def __sub__(self, other):
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = r1-r2
          resultI = i1-i2
          result = Complex(resultR, resultI)
          return result
  
      def __mul__(self, other):
          r1 = self.imagPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = (r1*r2-i1*i2)
          resultI = (r1*i2+r2*i1)
          result = Complex(resultR, resultI)
          return result   
  
  c1 = Complex(2,3)
  c2 = Complex(1,4)
  
  print c1+c2
  print c1-c2
  print c1*c2
  </code></pre>
  <p>the <strong>str</strong> method: (which not works)</p>
  <pre><code>def __str__(self):
      return '%d+(%d)j'&amp;(self.realPart, self.imagPart)
  </code></pre>
  <p>Latest version:
  (Terminal shows SyntaxError 'return' outside function in <strong>div</strong> method)</p>
  <pre><code>class Complex (object):
      def __init__(self, realPart, imagPart):
      self.realPart = realPart
      self.imagPart = imagPart            
  
      def __str__(self):
          if type(self.realPart) == int and type(self.imagPart) == int:
              if self.imagPart &gt;=0:
                  return '%d+%di'%(self.realPart, self.imagPart)
              elif self.imagPart &lt;0:
                  return '%d%di'%(self.realPart, self.imagPart)
          else:
              if self.imagPart &gt;=0:
                  return '%f+%fi'%(self.realPart, self.imagPart)
              elif self.imagPart &lt;0:
                 return '%f%fi'%(self.realPart, self.imagPart)
  
      def __add__(self, other):
          r1 = self.realPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = r1+r2
          resultI = i1+i2
          result = Complex(resultR, resultI)
          return result
  
      def __sub__(self, other):
          r1 = self.realPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = r1-r2
          resultI = i1-i2
          result = Complex(resultR, resultI)
          return result   
  
      def __mul__(self, other):
          r1 = self.realPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = (r1*r2-i1*i2)
          resultI = (r1*i2+r2*i1)
          result = Complex(resultR, resultI)
          return result
  
      def __div__(self, other):
          r1 = self.realPart
          i1 = self.imagPart
          r2 = other.realPart
          i2 = other.imagPart
          resultR = float(float(r1*r2+i1*i2)/float(r2*r2+i2*i2))
          resultI = float(float(r2*i1-r1*i2)/float(r2*r2+i2*i2))
          result = Complex(resultR, resultI)
          return result
  
  c1 = Complex(2,3)
  c2 = Complex(1,4)
  
  print c1+c2
  print c1-c2
  print c1*c2
  print c1/c2
  </code></pre>