How can we use complex numbers in Python?


A complex number is a pair of real numbers a and b, most often written as a+bi, or a+ib, where i is called the imaginary unit and acts as a label for the second term. Mathematically,  i2 = -1. Sometimes, j is used instead of i.

Here’s how a complex number is assigned to a variable:

>>> a=5+6j
>>> a
(5+6j)
>>> type(a)
<class 'complex'>

Python has a built-in function complex() which returns a complex data type.

complex(x) returns a complex number with x as real part and imaginary part as zero. complex(x,y) returns a complex number with x as real part and y as imaginary part.

>>> x=5
>>> complex(x)
(5+0j)
>>> x=5
>>> y=6
>>> complex(x,y)
(5+6j)

Updated on: 18-Feb-2020

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements