complex()

Python uses `j` for the imaginary unit (engineering convention), not `i`. Two call forms: numeric or string.

Built-in function / typePython 1.0+Live demo
Common call
z = complex(3, 4)
Returns
a complex number — (3+4j)
Replaces
writing the literal `3+4j` when the components are variables
Watch out
Python uses `j` not `i`; the string form does NOT accept spaces around `+`
complex(realrealThe 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".type: float | str · default: 0=0, imagimagThe imaginary part. Only usable when real is a number. Default 0 gives a purely real complex.type: float · default: 0=0) / complex(string)
complex

Demo

Live evaluation
Try:
Inputs
realstrreal part or full "a+bj"
imagstrimag (leave empty for string form)
Output
complex('3', '4')
'(3+4j)'

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

NameTypeRequiredDescription
realfloat | strno (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".
imagfloatno (0)The imaginary part. Only usable when real is a number. Default 0 gives a purely real complex.

Return value

complexA 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

Construct from computed values
When real and imaginary come from calculations.
z = complex(magnitude * math.cos(angle),
            magnitude * math.sin(angle))
Parse a string literal
Round-trip: `str(z)` then `complex(str(z))` gives back the same number.
s = "3+4j"
z = complex(s)   # (3+4j)
Extract real / imag
Access parts as attributes.
z = complex(3, 4)
z.real   # 3.0
z.imag   # 4.0
Absolute value / magnitude
abs() on a complex number returns the magnitude.
abs(complex(3, 4))   # 5.0  (Pythagorean)

Examples

1. Two-arg
complex(3, 4)
Returns
(3+4j)
2. Real only
complex(5)
Returns
(5+0j)
3. Imag only
complex(0, 2)
Returns
2j
4. Negative imag
complex(3, -4)
Returns
(3-4j)
5. String form
complex("3+4j")
Returns
(3+4j)
6. Purely imag string
complex("-1.5j")
Returns
-1.5j
7. Literal
3 + 4j
Returns
(3+4j) # no constructor needed
8. Magnitude
abs(complex(3, 4))
Returns
5.0

Pitfalls

1. Python uses `j`, not `i`
The math convention writes complex numbers with `i` (a+bi). Python (following the engineering convention) uses `j` (a+bj). Using `i` is a NameError unless you have a variable named `i`.
Math convention
complex(3 + 4i)
NameError: name 'i' is not defined
j is the unit
complex(3, 4)
# or literal: 3 + 4j
(3+4j)
2. String form REJECTS spaces around the operator
complex("3+4j") works. complex("3 + 4j") fails. Python is strict about the string form — for numeric args, use the two-arg form instead.
Space fails
complex("3 + 4j")
ValueError: complex() arg is a malformed string
No spaces
complex("3+4j")
# or: complex(3, 4)
(3+4j)
3. Two-arg form does not take a string as first arg
The string form is ONLY the one-arg form. Passing a string as real with an imag argument raises TypeError.
Mixed forms
complex("3", 4)
TypeError: complex() can't take second arg if first is a string
Pick one
complex("3+4j")
# or: complex(3, 4)
(3+4j)
4. Imag part is always a float internally
Passing ints gives you a complex whose real and imag are actually floats. Comparisons and hashing behave accordingly.
Assumed int
complex(3, 4).real
3.0 # not 3
Cast if needed
int(complex(3, 4).real)
3

When to use

Use it
  • 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
Reach for something else
  • 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

Complexity
O(1) construction
Return
A complex object — immutable, hashable
CPython impl
Objects/complexobject.c :: complex_new
Memory
Two doubles per complex — 16 bytes plus object overhead
Thread-safe
Yes — complex objects are immutable

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.

History

1.0
complex numbers built into Python since the earliest versions.
2.6
complex.__format__ added for use with format() and f-strings.