In this article, we will discuss how to handle complex numbers in Python. We will discuss about various ways to create a complex number. Let’s begin our journey of complex numbers.

What are Complex Numbers?

Numbers which have a real part and an imaginary part are called as complex numbers. Complex numbers are defined as any number in the form a+bi where a is the real part and b is the imaginary part. Both a and b can be positive or negative real numbers. The value of i is square root of -1, which cannot be calculated. In other words the value of i2=-1.

For example 5+6i is a complex number. Similarly 5-6i is also a complex number. (In case you need more information about complex numbers, read this article on Complex Numbers at Wikipedia)

How to represent complex number in Python?

Python supports complex numbers. Any expression written in the form a+bj or a-bj is treated as a complex number. Complex numbers are formed by writing a “j” with the imaginary part. The value of a and b can be integer or float. The keyword complex is used to represent complex numbers in Python. Let’s see some examples of complex numbers in below code snippet.

Python code to store complex numbers

# code to store complex numbers 
a=5+6j
print(a)
print(type(a))      #complex
    
b=5-6j
print(b)
print(type(b))      #complex

c=7j
print(c)
print(type(c))      #complex

d=2.7+3.6j
print(d)
print(type(d))      #complex

e=5.4j
print(e)
print(type(e))      #complex

Complex numbers can be added and subtracted

Two complex numbers can be added using the addition operator (+). In the addition of two complex numbers, the corresponding real parts are added and the corresponding imaginary parts are added. Similarly, two complex numbers can also be subtracted using the subtraction operator (-).

Python code to add and subtract two complex numbers

# addition and subtraction of two complex numbers
a = 7 + 12j
b = 5 + 10j
c = a + b
print("The sum is ", c)         #12+22j
d = a - b
print("The difference is ", d)  #2+2j

Complex numbers can be multiplied too

A complex number can be multiplied with another complex number. The first complex number has a real part and an imaginary part. The second complex number likewise has a real part and an imaginary part. The multiplication of these two numbers will result in four individual products which can be simplified to two products, one real and one imaginary. For example, lets assume the first complex number is (a+bj) and the second complex number is (c+dj). The multiplication process of these two complex numbers is explained below.

Product = (a+bj)*(c+dj)
Product = a*(c+dj)+bj*(c+dj)
Product = ac + adj + bcj + bdj2
Product = ac + adj + bcj - bd (because j2 = -1)
Product = (ac-bd) + (ad+bc)j

Python code to multiply two complex numbers

#multiplication of two complex numbers
a = 7 + 12j
b = 5 + 10j
c = a * b
print("The product is ", c)         #-85+130j

Complex numbers can be divided too

A complex number can be divided with another complex number. The first complex number has a real part and an imaginary part. The second complex number likewise has a real part and an imaginary part. The division of these two numbers will result in another complex number. For example, lets assume the first complex number is (a+bj) and the second complex number is (c+dj). The division process of these two complex numbers is explained below.

Division = (a+bj) / (c+dj)
Division = [(a+bj)*(c-dj)] / [(c+dj)*(c-dj)]
Division = [(ac+bd) + (bc-ad)j] / (c2+d2)
Division = (ac+bd)/(c2+d2) + (bc-ad)j/(c2+d2)

Python code to divide two complex numbers

#division of two complex numbers
a = 7 + 12j
b = 5 + 10j
c = a / b
print("The division is ", c)         #1.24-0.08j

More Concepts in Data Types

Last modified: March 28, 2023

Author