Programming Search⌘K

Featurecore

The complex type

Also called Python complex numbers, imaginary literal, complex(), cmath, z.real and z.imag, conjugate.

complex holds a complex number as two floats, z.real and z.imag. A literal with a j suffix is imaginary. Complex numbers have no order, //, % or divmod, and the cmath module has their math functions.

Language
Python

Synopsis

z = 3 + 4j
print(z.real, z.imag, abs(z), z.conjugate())
print(1j * 1j, complex(1, 2), complex("-1.23+4.5j"))
Output
3.0 4.0 5.0 (3-4j)
(-1+0j) (1+2j) (-1.23+4.5j)

3 + 4j adds an integer and an imaginary number. The parts are floats, and abs() gives the magnitude.

Semantics

r1. A complex number is "a pair of machine-level double precision floating-point numbers". The read-only attributes z.real and z.imag give the two parts.3 Example: complex-basics.

r2. A numeric literal with the suffix j or J gives an imaginary number, a complex number with a zero real part. Add an integer or a float to it to get a complex number with both parts.1

r3. complex() converts a string or a number, or makes a complex number from a real and an imaginary part. A string may have whitespace and parentheses around it, but no whitespace between the sign, the number and the j suffix: complex('1+2j') is valid and complex('1 + 2j') raises ValueError.2 Example: complex-string.

r4. Floor division, % and divmod() are "Not for complex numbers". Built-in Types says to convert to floats with abs() if appropriate.1 Complex numbers "do not support order comparison".6 Example: no-floor-division.

r5. The functions of the math module "cannot be used with complex numbers". The cmath module has functions of the same names for complex numbers.5 They accept integers, floats and complex numbers.4 Example: math-and-cmath.

r6. cmath.phase(z) returns the argument of z as a float in the range from -π to π, and cmath.polar(z) returns the pair (abs(z), phase(z)). The module has no function for the modulus, because the built-in abs() computes it.4 Example: math-and-cmath.

Examples

math.sqrt() rejects a negative number. cmath.sqrt() returns a complex result (rules r5 and r6).

import cmath
import math

print(cmath.sqrt(-1), cmath.polar(1j))
try:
    math.sqrt(-1)
except ValueError:
    print("math.sqrt(-1): ValueError")
Output
1j (1.0, 1.5707963267948966)
math.sqrt(-1): ValueError

// fails for a complex number. Use the real part or the magnitude, which are floats (rule r4).

z = 7 + 0j
try:
    z // 2
except TypeError:
    print("z // 2: TypeError")
print(z.real // 2, abs(z) // 2)
Output
z // 2: TypeError
3.0 3.0

Pitfalls

Spaces in a complex string

Code can write 1 + 2j with spaces, but complex() rejects a string with spaces around the sign (rule r3).

This code runs but gives a wrong answer on purpose.

for s in ["1+2j", "(1+2j)", "1 + 2j"]:
    try:
        print(complex(s))
    except ValueError:
        print("ValueError for", repr(s))
Output
(1+2j)
(1+2j)
ValueError for '1 + 2j'

Sorting complex numbers

< is not defined for complex numbers (rule r4), so sorted() of complex values fails. Sort with a key such as abs.

This code raises TypeError, on purpose.

print(sorted([2j, 1 + 1j]))

Comparison

Go has two complex types, complex64 and complex128, with float32 or float64 parts. The built-in function complex builds a value from a real and an imaginary part, and real and imag extract the parts.7 Python has one complex type with two float parts, and gets the parts with the attributes z.real and z.imag (rule r1).

References

  1. Built-in Types, Python 3.15 documentation, Numeric Types - int, float, complex
  2. Built-in Functions, Python 3.15 documentation, complex()
  3. The Python Language Reference, Python 3.15, 3.2.4.3. numbers.Complex (complex)
  4. cmath, Mathematical functions for complex numbers, Python 3.15 documentation, introduction; cmath.phase(), cmath.polar()
  5. math, Mathematical functions, Python 3.15 documentation, introduction
  6. The Python Language Reference, Python 3.15, 6.10.1. Value comparisons
  7. The Go Programming Language Specification, Numeric types; Manipulating complex numbers

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source