How to create a complex number in Python?


Complex number is made up of real and imaginary parts. Real part is a float number, and imaginary part is any float number multiplied by square root of -1 which is defined as j.

>>> no=5+6j
>>> no.real
5.0
>>> no.imag
6.0
>>> type(no)
<class 'complex'>

The resulting object is of complex data type. Python library also has complex() function, which forms object from two float arguments

>>> no=complex(5,6)
>>> no
(5+6j)
>>> no.real
5.0
>>> no.imag
6.0
>>> type(no)
<class 'complex'>


Updated on: 24-Feb-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements