Complex number python

Python support complex number as one of the built-in data type.In mathematics we usually come across a complex number to be of the form ‘a+ib’ where ‘a’ is the real part and ‘b’ the imaginary part and ‘i’ the imaginary unit,which is a square root of -1.In Pyhton however,we will represent a complex number in the from ‘a+bj’ ,here instead of ‘i’,’j’ is use as the imaginary unit.And note ‘a+jb’ is not allowed in Python.

Any new method is not required to use the complex number you can directly write a complex in it’s form.After the complex number is written to get the real part you can use the form ‘complex_number_object.real‘ and to get the imaginary part you can use ‘complex_number_object.imag‘.An example is given below.

>>> com=12.5 - 45.09j #a complex number
>>> com.real #getting the real part
12.5
>>> com.imag #getting the imaginary part
-45.09
>>> c=2+j89 #error!!
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    c=2+j89
NameError: name 'j89' is not defined

If you want a conjugate(a+jb to a-jb) of the complex number use can use the function ‘conjugate()’.

>>> com=12.5 - 45.09j
>>> com.conjugate()
(12.5+45.09j)
>>> c1=90+9j
>>> c1.conjugate()
(90-9j)

Mathematical operation with complex number

With integers and floating point type value we can perform all the mathematical operation in Python likewise all mathematical operation is possible with complex number.

c1=23-90j
>>> c2=24+13j
>>> c1+c2 #addition
(47-77j)
>>> c1/2 #division
(11.5-45j)
>>> c1*23 #multiplication
(529-2070j)
>>> c2**2 #power of c2
(407+624j)


Leave a Reply

Your email address will not be published. Required fields are marked *