In python, you can put 'j or' J after the number to make it imaginary so you can easily write complex literals:
>>> 1j 1j >>> 1J 1j >>> 1j * 1j (-1+0j)
The suffix 'j comes from electrical engineering, where the variable' I is usually used for current. ( Reasoning is found here. )
The type of a complex number is complex , and you can use it as a constructor if you want:
>>> complex(2,3) (2+3j)
A complex number has some built-in accessors:
>>> z = 2+3j >>> z.real 2.0 >>> z.imag 3.0 >>> z.conjugate() (2-3j)
Several built-in functions support complex numbers:
>>> abs(3 + 4j) 5.0 >>> pow(3 + 4j, 2) (-7+24j)
The standard cmath module has more functions that handle complex numbers:
>>> import cmath >>> cmath.sin(2 + 3j) (9.15449914691143-4.168906959966565j)
rob mayoff Dec 03 '11 at 20:20 2011-12-03 20:20
source share