complex() is a built-in Python function that creates a complex number — either from two real parts (real + imaginary) or by parsing a string in the form 'a+bj'. Complex numbers are essential in scientific computing, signal processing, electrical engineering, physics simulations, and advanced data analysis (e.g., Fourier transforms, quantum computing, control systems). In 2026, complex remains a core type for numerical work — supported natively in NumPy (arrays of complex), Dask (parallel complex arrays), SciPy (signal/FFT), and PyTorch (complex tensors) — enabling vectorized operations, phase/magnitude calculations, and complex-valued machine learning.
Here’s a complete, practical guide to using complex() in Python: creation & parsing, basic operations, NumPy/Dask vectorization, real-world patterns (earthquake signal processing, frequency analysis), and modern best practices with type hints, performance, error handling, and integration with NumPy/Polars/Dask/SciPy/PyTorch.
Creating complex numbers — from reals, strings, or defaults.
# From real and imaginary parts
c1 = complex(2, 3) # 2 + 3j
print(c1) # (2+3j)
# From string (real+imagj format)
c2 = complex('2+3j') # parses 'real+imagj' or 'real' or 'imagj'
print(c2) # (2+3j)
c3 = complex('4j') # pure imaginary
print(c3) # 4j
c4 = complex('5') # real only
print(c4) # (5+0j)
# Default (0+0j)
c5 = complex()
print(c5) # 0j
Basic complex operations — arithmetic, magnitude, phase, conjugate.
c = complex(3, 4)
print(c.real) # 3.0
print(c.imag) # 4.0
print(abs(c)) # 5.0 (magnitude)
print(c.conjugate()) # (3-4j)
print(c ** 2) # (-7+24j)
print(c * 1j) # (-4+3j)
# Phase (argument) in radians
import cmath
print(cmath.phase(c)) # 0.9272952180016122 (?53.13°)
Vectorized complex in NumPy & Dask — arrays of complex numbers.
import numpy as np
import dask.array as da
# NumPy complex array
z = np.array([1+2j, 3+4j, 5+6j])
print(np.abs(z)) # [2.23606798 5. 7.81024968]
print(np.angle(z)) # [1.10714872 0.92729522 0.87605805] radians
# Dask: lazy parallel complex operations
dz = da.from_array(z, chunks=2)
mag_dask = da.abs(dz)
phase_dask = da.angle(dz)
print(mag_dask.compute()) # same as NumPy
Real-world pattern: earthquake signal processing — complex Fourier analysis for frequency content.
import numpy as np
from scipy.fft import fft
# Simulated earthquake acceleration time series (real signal)
time_series = np.sin(np.linspace(0, 10, 1000)) + 0.5 * np.random.randn(1000)
# Compute FFT (complex spectrum)
spectrum = fft(time_series)
# Magnitude & phase
freq_magnitude = np.abs(spectrum)
freq_phase = np.angle(spectrum)
print(f"Dominant frequency magnitude: {freq_magnitude.max():.2f}")
# Dask version for very large signals
import dask.array as da
d_time_series = da.from_array(time_series, chunks=200)
d_spectrum = da.fft.fft(d_time_series)
d_mag = da.abs(d_spectrum)
print(d_mag.max().compute())
Best practices for complex() in Python & data workflows. Use complex(re, im) — for explicit creation; complex('a+bj') — for parsing strings. Modern tip: use NumPy np.complex128 — default complex type; Dask/Polars for large arrays. Prefer 1j literal — 3 + 4j over complex(3, 4). Use cmath — for complex-aware math (phase, polar, rect). Add type hints — def process_signal(z: complex) -> complex. Handle type errors — complex() raises ValueError on invalid strings. Use z.conjugate() — for complex conjugate. Use abs(z) — magnitude. Use cmath.phase(z) — argument in radians. Use cmath.polar(z) — (magnitude, phase). Use cmath.rect(r, phi) — from polar to rectangular. Profile performance — complex ops are fast; avoid in ultra-tight loops. Use np.iscomplexobj(arr) — check array type. Use np.complex64/np.complex128 — memory vs precision. Use torch.complex(real, imag) — in PyTorch for GPU. Use jax.numpy.complex — in JAX for autodiff.
complex() creates complex numbers from reals or strings — enabling arithmetic, magnitude, phase, and conjugate operations. In 2026, use for signal processing, FFT, quantum simulation, and integrate with NumPy/Dask/Polars/PyTorch for vectorized complex workflows. Master complex, and you’ll handle complex-valued data cleanly and efficiently in any scientific or engineering application.
Next time you need complex numbers — use complex(). It’s Python’s cleanest way to say: “Give me real and imaginary parts — together as one powerful number.”