Fix Python – Complex numbers in python


Asked By – I159

Are complex numbers a supported data-type in python? If so, how do you use them?

Now we will see solution for issue: Complex numbers in python


In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literals easily:

>>> 1j
>>> 1J
>>> 1j * 1j

The ‘j’ suffix comes from electrical engineering, where the variable ‘i’ is usually used for current. (Reasoning found here.)

The type of a complex number is complex, and you can use the type as a constructor if you prefer:

>>> complex(2,3)

A complex number has some built-in accessors:

>>> z = 2+3j
>>> z.real
>>> z.imag
>>> z.conjugate()

Several built-in functions support complex numbers:

>>> abs(3 + 4j)
>>> pow(3 + 4j, 2)

The standard module cmath has more functions that handle complex numbers:

>>> import cmath
>>> cmath.sin(2 + 3j)

This question is answered By – rob mayoff

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0