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
>>> com.imag #getting the imaginary part
>>> c=2+j89 #error!!
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
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()
>>> c1=90+9j
>>> c1.conjugate()

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.

>>> c2=24+13j
>>> c1+c2 #addition
>>> c1/2 #division
>>> c1*23 #multiplication
>>> c2**2 #power of c2

Leave a Reply

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