Module 3 · Variables and Data Types — topic 9 of 15
Complex numbers
Python’s built-in complex type for numbers with real and imaginary parts: writing them with j, reading their parts, doing arithmetic, and where they are used in science and engineering.
Learning objectives
- Write complex numbers with the j suffix
- Read the real and imaginary parts
- Do basic complex arithmetic and use cmath
Definition
Python’s built-in complex type for numbers with real and imaginary parts: writing them with j, reading their parts, doing arithmetic, and where they are used in science and engineering.
Introduction
A complex number has a real part and an imaginary part, written like 3 + 4j. The imaginary unit j is defined so that j × j = −1. Python supports complex numbers natively, which is unusual among programming languages.
Why it matters
Most everyday programs never need complex numbers, but electrical engineering, signal processing and physics rely on them. Knowing they exist also explains why a calculation such as (-1) ** 0.5 returns a complex value instead of an error.
Detailed explanation
Write the imaginary part with a j (or J) suffix: 2 + 3j. Python uses j, the engineering convention, rather than the mathematician’s i.
complex(real, imag) builds one from two numbers: complex(2, 3) is (2+3j).
.real and .imag give the parts as floats; .conjugate() flips the sign of the imaginary part.
abs(z) gives the magnitude, the distance from zero: abs(3 + 4j) is 5.0.
+, -, * and / work as in mathematics. Complex numbers cannot be ordered, so < and > raise TypeError.
The cmath module provides square roots, phases and other functions for complex values.
A complex number can also be pictured as a point on a plane, with the real part along the horizontal axis and the imaginary part along the vertical axis. abs() is then the distance from the origin and cmath.phase() is the angle, which is how engineers describe the size and timing of an alternating current.
Syntax
- j marks the imaginary part.
- complex() builds a value from two numbers.
- Use .real, .imag and abs() to inspect it.
Code examples
Parts and magnitude
3.0 4.0 5.0 complex
j times j
(-1+0j) (2-3j)
More examples
Solving x² + 2x + 5 = 0
discriminant root: 4j roots: (-1+2j) (-1-2j)
Step-by-step explanation
- The discriminant b² − 4ac is 4 − 20 = −16, which is negative.
- cmath.sqrt(-16) returns 4j instead of raising an error like math.sqrt would.
- The quadratic formula then produces two complex roots.
- Python prints complex numbers in parentheses, such as (-1+2j).
Expected output
Running “Solving x² + 2x + 5 = 0” prints exactly:
Practical applications
- Electrical engineers represent AC voltages and impedances as complex numbers.
- Signal-processing libraries such as NumPy’s FFT return complex results.
- Quantum computing simulations store amplitudes as complex numbers.
Best practices
- Use cmath, not math, for functions of complex values.
- Compare magnitudes with abs() when you need an ordering.
- Use NumPy for large amounts of complex arithmetic.
Common mistakes
Using i instead of j
Avoid
Prefer
Python uses j for the imaginary unit; 4i is a SyntaxError.
Comparing complex numbers
Avoid
Prefer
Complex numbers have no natural order; compare magnitudes instead.
Key takeaways
- complex numbers have a real part and an imaginary part marked with j.
- .real, .imag, abs() and .conjugate() inspect them.
- cmath handles complex square roots and related functions.
Try it yourself
- 1Multiply (1 + 2j) by (3 − 1j) and check the result by hand.
- 2Find the roots of x² + 4 = 0 with cmath.sqrt().
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?