Learn Object Oriented Programming by re-implementing Python’s Complex class: Part-2

leangaurav
Industrial Python
Published in
3 min readJul 13, 2019

The previous story covered theory of complex numbers. Here we’ll play with complex numbers with python. For all the code samples we are using python3 but python2 should also work. But you will have to make some changes to code when needed.

In python we already have a built-in complex class.(You can confirm that yourself if you don’t believe me… just do dir(__builtins__) on interpreter and see complex will be somewhere in there the list).

Lists dir(__builtins__) which contains the complex class.
>>> help(__builtins__)

Okay its there, but before we start with the complex class lets try what we did in previous story(that is finding square roots).

Square root of a simple positive number(4) works. But wait for negative number(-4) the math module’s sqrt can’t fails. No worries, we need sqrt from cmath module.

This works but the result is a number with a j at the end. Well that’s a complex number that uses j instead of i. And also a quick type check of the last result says it’s the built-in type complex.

Building Complex Numbers

Creating complex numbers using built-in complex class is simple. Type help(complex) on the interpreter it gives you this:

Help on class complex in module builtins:class complex(object)
| complex(real=0, imag=0)
|
| Create a complex number from a real part and an optional imaginary part.
|
| This is equivalent to (real + imag*1j) where imag defaults to 0.

Based on the help, all these are valid ways to create complex objects or complex numbers using complex class.

>>> complex()
0j
>>> complex(1)
(1+0j)
>>> complex(1,2)
(1+2j)
>>> complex(1,-2)
(1-2j)

Complex Operations

We don’t need much discussion on the operations so I will just show you all the operations directly.

Conjugate

Each complex object has a conjugate() method in it which change sign of the imaginary part.

>>> c1 = complex(10,20)
>>> c1
(10+20j)
>>> c1.conjugate()
(10–20j)
>>>

Square and other powers of i

I will just put the code sample here.

>>> c1 = complex(0,1)
>>> c1
1j
>>> c1**2
(-1+0j)
>>> c1**4
(1+0j)
>>> c1**5
1j
>>> c1**7
(-0–1j)
>>>

Addition and Subtraction

These are quite simple but since both are binary operations, we will create 2 complex numbers.

>>> c1 = complex(10,20)
>>> c2 = complex(5,-10)
>>> c1
(10+20j)
>>> c2
(5–10j)
>>> c3 = c1 + c2 # addition
>>> c3
(15+10j)
>>> c4 = c1 - c2 # subtraction
>>> c4
(5+30j)
>>>

Multiplication and Division

For both of these operations, we are not interested in implementing the logic our self. So we will use the * and / operators for multiplication and division.

>>> c1 = complex(5,10)
>>> c2 = complex(10,-20)
>>> c1 * c2
(250+0j)
>>> c2 = complex(10,20)
>>> c3 = c1 * c2 # multiplication
>>> c3
(-150+200j)
>>> c4 = c1/c2 # division
>>> c4
(0.5+0j)
>>>

And I have only this much for this story. Hope you followed with all the code on the interpreter. In the next section we will create our own implementation of these operations using just functions. And in the last one we will have our own complex class.

Prev: Complex Numbers 1: The Math

Next: Complex Numbers 3: Reinvent Python’s complex class

--

--

leangaurav
Industrial Python

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav