complex()
Python uses `j` for the imaginary unit (engineering convention), not `i`. Two call forms: numeric or string.
Demo
complex has two call forms. NUMERIC form: complex(a, b) gives real=a and imaginary=b. STRING form: complex("3+4j") parses a Python-style literal — no spaces allowed around the `+`. Python uses `j` for the imaginary unit (engineering convention), not `i` (math convention). Displayed as `(a+bj)` for non-zero imaginary parts.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| real | float | str | no (0) | The real part. Also accepts a string; when a string is given, imag must be omitted. String form: Python-style like "3+4j" or "-1.5j". |
| imag | float | no (0) | The imaginary part. Only usable when real is a number. Default 0 gives a purely real complex. |
Return value
complex — A complex number with real and imaginary parts. Two call forms: `complex(a, b)` for real=a and imag=b; `complex("3+4j")` to parse a string in Python's notation.
Common patterns
z = complex(magnitude * math.cos(angle), magnitude * math.sin(angle))
s = "3+4j" z = complex(s) # (3+4j)
z = complex(3, 4) z.real # 3.0 z.imag # 4.0
abs(complex(3, 4)) # 5.0 (Pythagorean)
Examples
Pitfalls
complex(3 + 4i)
complex(3, 4) # or literal: 3 + 4j
complex("3 + 4j")
complex("3+4j") # or: complex(3, 4)
complex("3", 4)
complex("3+4j") # or: complex(3, 4)
complex(3, 4).real
int(complex(3, 4).real)
When to use
- Signal processing, DSP, and engineering computations
- 2D geometry using complex arithmetic (rotation, translation)
- Fourier transforms and other math involving complex values
- Parsing complex literals from strings
- You only need a 2D point → tuple or dataclass is often clearer
- You need precise decimal arithmetic → decimal.Decimal
- Vector math with three or more dimensions → numpy
- Simple integer arithmetic → int / float are enough
Notes
FAQ
Historical — the engineering convention (electrical, signal processing) uses `j` because `i` is often reserved for electric current. Python inherited this from the electrical engineering community; the choice is baked into the language.