Python 3 reference · Built-in Functions

Python complex() Function

Create a complex number from a real and an imaginary part, or parse one from a string such as '1+2j'.

Python 3 Since 3.0

Syntax

complex(real=0, imag=0)
complex(string)

Parameters

ParameterDescription
real optional The real part, a number or a string. Default: 0
imag optional The imaginary part. Default: 0

Return value

A complex number.

Examples

Create one and read its partsTry it yourself
z = complex(3, 4)
print(z, z.real, z.imag)
print(abs(z))

Output

(3+4j) 3.0 4.0
5.0
Parse and calculateTry it yourself
print(complex("1+2j"))
print((1 + 2j) * (3 - 1j))
print((1 + 2j).conjugate())

Output

(1+2j)
(5+5j)
(1-2j)

Good to know

  • Python writes the imaginary unit as j: 2 + 3j.
  • Strings must have no spaces: complex("1+2j") works, complex("1 + 2j") raises ValueError.
  • For complex maths functions (sqrt of negatives, exp, phase) use the cmath module.

Version history

VersionChange
Python 3.0Added