In mathematics, a complex number is any number which can be expressed in the form of a + bi, where ‘a’ and ‘b’ are real numbers. Here, ‘i’ is a symbol called the imaginary unit, and it needs to satisfy the equation ‘i2 = −1′. It is so, because no “real” number will satisfy that equation. The unit ‘i’ was also first came to be known as an imaginary number by René Descartes. Thus, in this article, we get a sneak peek into Complex Number in Python.

Thus, a complex number is any number of the form a + bi, where a and b are real numbers, and i*i = -1.  We will now thus, see, that in Python, there are multiple ways to create such a Complex Number.

Complex Number in Python

Complex Numbers in Python

  • We can directly use the syntax a + bj to create a Complex Number.
>>> a = 14 + 4j
>>> print(a)
>>> print(type(a))
<class 'complex'>
  • We can also use the complex Class to create a complex number
>>> a = complex(14, 4)
>>> print(type(a))
<class 'complex'>
>>> print(a)

Real and Imaginary Parts in Complex Number

As discussed above every complex number (a + bj) has two important parts : A real part (a), and an imaginary part (b) due to the imaginary unit ‘i’.

We can also, use the number.real command to get the real part of the number and the  number.imag command to get the imaginary part of the complex number.

>>> a
>>> a.real
>>> a.imag

What is the Conjugate of a Complex Number?

The conjugate of a complex number a + bj is generally defined asthe component a - bj. One can use the number.conjugate()function to get the conjugate of the complex number also.

>>> a
>>> a.conjugate()

Various Arithmetic Operations

Similar to real numbers, the arithmetic operations of addition, subtraction, multiplication and division can be performed with complex numbers. Let’s get to see how.

a = 14 + 4j
b = 7 + 2j
print('Addition =', a + b)
print('Subtraction =', a - b)
print('Multiplication =', a * b)
print('Division =', a / b)


Addition = (21+6j)
Subtraction = (7-2j)
Multiplication = (98+8j)
Division = (2+0j)

An important point here is, comparing of two complex numbers is not possible. Only their real and imaginary parts can be compared individually, since they are real numbers. Let’s see this with the help of an example.

>>> a
>>> b
>>> a < b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'complex' and 'complex'

Phase of a Complex Number

We can represent a complex number in a plane, as a vector consisting of two components consisting of the real and imaginary axes. Hence, the real part and the imaginary part form the two components of the vector.

Complex Number Vector 1
Complex Number Vector

The angle in the arc of the vector and the real axis is commonly known as the phase or also the argument of a Complex Number.


phase(number) = arctan(imaginary_part / real_part) …………. where the arctan is the tan inverse mathematical function.

Now, thus, lets see how to get the phase of a complex number in Python.

import cmath
import math

a = 14 + 4j

# Using cmath module
phase = cmath.phase(a)
print('cmath Module value is : ', phase)

# Using math module
phase = math.atan(a.imag/a.real)
print('Math Module value is : ', phase)


cmath Module value is : 0.27829965900511133
Math Module value is : 0.27829965900511133

Also, to convert the radians into degrees we can use the NumPy library in Python.

import cmath
import numpy as np

a= 14 + 4j

# Using cmath module
phase = cmath.phase(a)
print('cmath Module in Radians : ', phase)
print('Phase in Degrees is : ', np.degrees(phase))


cmath Module in Radians :  0.27829965900511133
Phase in Degrees is :  15.945395900922854

Note : To thus, know more about math module in python take a sneak peek here.

Rectangular and Polar Coordinates

Complex numbers when wanting to be written into Rectangular Coordinate or Polar Coordinate formats, can hence, use the cmath.rect() and the cmath.polar()functions.

import cmath
a = 14 + 4j
polar_coordinates = cmath.polar(a)
(14.560219778561036, 0.27829965900511133)

modulus = abs(a)
phase = cmath.phase(a)
rect_coordinates = cmath.rect(modulus, phase)

Trigonometric Functions

import cmath

a = 14 + 3j

print('Sine is :', cmath.sin(a))
print('Cosine is :', cmath.cos(a))
print('Tangent is :', cmath.tan(a))

print('ArcSin is :', cmath.asin(a))
print('ArcCosine is :', cmath.acos(a))
print('ArcTan is :', cmath.atan(a))


Sine is : (9.973100027667153+1.3698163499280331j)
Cosine is : (1.3766240951593778-9.923780591523466j)
Tangent is : (0.0013494480654275465+1.0047826282517973j)
ArcSin is : (1.359201617569979+3.353538253537821j)
ArcCosine is : (0.21159470922491766-3.353538253537821j)
ArcTan is : (1.5025950245944228+0.014567227030993942j)

Hyperbolic Functions

Some Hyperbolic Functions in the the cmath module are as follows :

import cmath

a = 14 + 3j

print('Hyperbolic Sine value is :', cmath.sinh(a))
print('Hyperbolic Cosine value is :', cmath.cosh(a))
print('Hyperbolic Tangent value is :', cmath.tanh(a))

print('Inverse Hyperbolic Sine value is :', cmath.asinh(a))
print('Inverse Hyperbolic Cosine value is :', cmath.acosh(a))
print('Inverse Hyperbolic Tangent value is :', cmath.atanh(a))


Hyperbolic Sine value is : (-595284.6088509279+84855.76313714073j)
Hyperbolic Cosine value is : (-595284.608851751+84855.7631370234j)
Hyperbolic Tangent value is : (0.9999999999986721-3.8639811012496763e-13j)
Inverse Hyperbolic Sine value is : (3.3557631233109295+0.21059529257293394j)
Inverse Hyperbolic Cosine value is : (3.353538253537821+0.21159470922491766j)
Inverse Hyperbolic Tangent value is : (0.06838439126640407+1.556094682692894j)

Miscellaneous Functions

There are also some in-built functions to check if a the type of complex number. That is, whether it is finite, infinite or for that matter even nan. Are two such numbers close can also be checked.

>>> print(cmath.isfinite(14 + 3j))

>>> print(cmath.isinf(14 + 3j))

>>> print(cmath.isinf(cmath.inf + 3j))

>>> print(cmath.isnan(14 + 3j))

>>> print(cmath.isnan(cmath.nan + 3j))

>>> print(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05))

>>> print(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005))


In this article, we thus, got a deep dive into the world of complex numbers. Hence, we saw, how to handle complex number in Python. Also, what all functions, methods can be implemented on the complex numbers. What operation can be performed with these numbers and also the application of the cmath module. We have thus, seen all these through the help of quite a lot of amazing and knowledgeable examples. In hope and anticipation that the topics covered here are pretty clear and also come in quite handy for you.

Categorized in: