A complex number combines a real component with an imaginary component. Python writes the imaginary component with a trailing j, as in 3 + 2j or 4.2 + 6.3j. section 3.2.1 section 4.7.4
Use complex values through ordinary arithmetic and component access:
- Write an expression such as 3 + 2j; Python creates one complex-number object and displays it in parentheses. section 4.7.4
- Apply addition, subtraction, multiplication, division, exponentiation, and related arithmetic operators to complex values. section 3.2.1 section 4.7.4
- Read z.real and z.imag to obtain the components; both attributes return floating-point numbers. section 4.7.4
- Use cmath for functions such as square root, because math functions do not apply to complex numbers. section 4.7.5
Inspecting a computed value
z = (3 + 5j)\nz.real\n3.0\nz.imag\n5.0\n\nMultiplication preserves the complex type: (1 + 2j) * (3 + 4j) produces (-5 + 10j). section 4.7.4
import cmath cmath.sqrt(-1) 1j
Try cmath.sqrt(-1) to see a complex result produced by a dedicated complex-number function. section 4.7.5


