Next / Previous / Contents / TCC Help System / NM Tech homepage

7.5. Type complex: Imaginary numbers

Mathematically, a complex number is a number of the form A+Bi where i is the imaginary number, equal to the square root of -1.

Complex numbers are quite commonly used in electrical engineering. In that field, however, because the symbol i is used to represent current, they use the symbol j for the square root of -1. Python adheres to this convention: a number followed by “j” is treated as an imaginary number. Python displays complex numbers in parentheses when they have a nonzero real part.

>>> 5j
5j
>>> 1+2.56j
(1+2.5600000000000001j)
>>> (1+2.56j)*(-1-3.44j)
(7.8064-6j)

Unlike Python's other numeric types, complex numbers are a composite quantity made of two parts: the real part and the imaginary part, both of which are represented internally as float values. You can retrieve the two components using attribute references. For a complex number C:

>>> a=(1+2.56j)*(-1-3.44j)
>>> a
(7.8064-6j)
>>> a.real
7.8064
>>> a.imag
-6.0

To construct a complex value from two float values, see Section 20.9, “complex(): Convert to complex type”.