Learning Python- Intermediate course: Day 9, Complex numbers part 1

Today we will learn about complex numbers in Python by importing the cmath module.

Handling complex numbers in Python is very easy. We can easily do normal stuff like addition and subtraction, etc. without using any functions!. Complex numbers have their use in many applications related to Mathematics or algorithm based problems. They provide an excellent alternative to coordinates or vector systems.

The cmath module provides useful functions to handle complex numbers and their properties. The good thing is that the module is already inbuilt and preinstalled in Python. So no need to download and install any third party add-ons! The cmath module is very similer to math module, but has the functionality to handle complex as well as real numbers. You will find the entire documentation here

In case you want to check out how tedious it is to implement complex numbers in Java, you might like this repository. This repository contains a class "Complex", which is the implementation of complex numbers. You can call the constructor to make complex numbers. This class also contains many useful methods to operate on complex numbers.

Making complex numbers.

We can make a complex number x+iy by using the following syntax

z= complex(x,y)

Let us now create a few complex numbers.

>>> import cmath # importing "cmath" for complex number operations
>>> x=2
>>> y=3
>>> z=complex(x,y)
>>> z

Real and imaginary parts of Complex numbers.

The real and the imaginary parts of the complex numbers can be obtained by z.real and z.img

>>> z.real
>>> z.imag

Let us now see a full fledged program

import cmath
x = 5
y = 3

# converting x and y into complex number
z = complex(x,y);

print ("The real part of complex number is : ",end="")
print (z.real)

print ("The imaginary part of complex number is : ",end="")
print (z.imag)
The real part of complex number is : 5.0
The imaginary part of complex number is : 3.0

Operations on Complex numbers.

Let us now see how to do the usual operations on complex numbers. Those who are unfamiliar with math behind the operations may check this out

>>> a=complex(2,3)
>>> b=complex(4,5)
>>> a+b 
# Adding two complex numbers
>>> a-b 
# Subtracting two complex numbers.
>>> a*2
 # Multiplying complex number by a scalar
>>> a*b 
# Product of two complex numbers
>>> b/4 
# Scalar division of complex number
>>> a/b 
# Dividing two complex numbers
>>> 1/b 
# Reciprocal of a complex number.
>>> a**2
 # Squaring a complex number
>>> a**b
# Complex number to a complex power
>>> a//2 
# Invalid operation of Dividing and rounding off complex numbers.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't take floor of complex number.

So friends Tomorrow we will continue with the cmath module and check out the inbuilt functions like phase() polar() and many others!
😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you all👍

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too