Python Glossary Русский

all Python 3Data typesterm

complex

numbers

Built-in type for complex numbers of the form a+bj. Supports arithmetic, the .real and .imag attributes and the conjugate() method.

Syntax

complex(real, imag)  a+bj  z.real  z.imag  abs(z)  z.conjugate()

The imaginary unit needs a coefficient — write 1j; a bare j is just a variable name (NameError). Complex numbers have no ordering, so < and > raise TypeError — only == and != work. The .real and .imag attributes are always floats, even when you built the number from ints.

Examples

z = 3 + 4j; print(z)           # → (3+4j)
print(complex(3, 4))            # → (3+4j)
print(z.real, z.imag)           # → 3.0 4.0
print(abs(z))                   # → 5.0
print((1+2j) * (3+4j))          # → (-5+10j)
print(z.conjugate())            # → (3-4j)
print(complex("3+4j"))          # → (3+4j) (разбор строки)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy