The cmath module provides mathematical functions for complex numbers in Python. It is similar to the math module, but all functions are designed to work with values that may have both real and imaginary parts.
Use cmath whenever you are dealing with operations like magnitude, phase, trigonometric, exponential, or logarithmic functions on complex numbers.
a + bj in Python (e.g., 3 + 4j).z.real and z.imag.a + bj is a - bj and is obtained using z.conjugate().abs(z).cmath.phase(z).cmath.polar() and cmath.rect().sin, cos, exp, log in cmath support complex inputs.To use the cmath module, you must first import it:
import cmath
A complex number can be created directly using the literal syntax:
z = 3 + 4j
For a complex number z = x + yj:
z.real → returns x (floating point).z.imag → returns y (floating point).z.conjugate() → returns x - yj.abs(z) → returns √(x² + y²), the magnitude.Polar representation of z is (r, θ), where:
r is the magnitude.θ is the phase angle in radians.
import cmath
z = 3 + 4j
print(z) # (3+4j)
z = 3 + 4j
print(z.real) # 3.0
print(z.imag) # 4.0
print(z.conjugate()) # (3-4j)
print(abs(z)) # 5.0 (magnitude)
import cmath
z = 3 + 4j
print(cmath.phase(z)) # 0.9272952180016122 (angle in radians)
r, theta = cmath.polar(z)
print(r, theta) # 5.0 0.9272952180016122
print(cmath.rect(r, theta)) # (3.0000000000000004+4j)
import cmath
z = 3 + 4j
print(cmath.sqrt(z)) # (2+1j)
print(cmath.exp(z)) # (-13.128783081462158-15.200784463067954j)
print(cmath.log(z)) # (1.6094379124341003+0.9272952180016122j)
print(cmath.sin(z)) # (3.853738037919377+27.016813258003932j)
print(cmath.cos(z)) # (-27.034945603074224-3.851153334811777j)
import cmath
z = 3 + 4j
print(cmath.sinh(z)) # (-6.548120040911002+7.61923172032141j)
print(cmath.cosh(z)) # (-6.580663718241853+7.581552742746153j)
print(cmath.tanh(z)) # (1.0001871061082424+0.0037640256415043743j)
z = 3 + 4jprint(z) shows the complex number in standard form: (3+4j).z.real returns 3.0 and z.imag returns 4.0.z.conjugate() flips the sign of the imaginary part, giving (3-4j).abs(z) computes √(3² + 4²) = 5.0, the magnitude of the vector.cmath.phase(z) returns the angle in radians between the positive real axis and the vector z.cmath.polar(z) returns (r, θ), where r = 5.0 and θ ≈ 0.9273.cmath.rect(r, θ) reconstructs the complex number from polar form back to approximately 3 + 4j.sqrt, exp, log, sin, and cos return complex outputs even if inputs are complex.abs(z) to get the magnitude of a complex number.cmath.phase(z) to get the angle in radians.cmath.polar(z) and cmath.rect(r, theta) to switch between polar and rectangular form.cmath over math when working with complex numbers; math is for real numbers only.cmath functions are in radians, not degrees.cmath functions always return a complex result, even if the imaginary part is 0j.5 + 12j and find its magnitude using abs() and phase using cmath.phase().5 + 12j into polar coordinates with cmath.polar() and then back to rectangular form with cmath.rect().sqrt, sin, and cos of a complex number 2 + 3j using cmath.exp(1 + 1j) and log(1 + 1j), and observe how real and imaginary parts change.