Python Complex Numbers: Real and Imaginary Parts
Represent two numeric components
A complex number has a real part and an imaginary part. The imaginary unit has square −1; Python writes it as 1j. A value such as 3 + 2j has real component 3 and imaginary component 2. The dot syntax .real and .imag reads these attributes (named properties of the value).
The two attributes are floats. You can also construct the same value with complex(3, 2), supplying the real and imaginary components in that order.
Add and multiply complex values
a = complex(2, 3)b = complex(4, -1)print(a + b)print(a * b)print(1j * 1j)Addition combines matching components: (2 + 4) and (3 − 1). Multiplication distributes across both parts, using j² = −1: the real part is 8 + 3 = 11 and the imaginary part is −2 + 12 = 10. Python displays a complex result with j for the imaginary term.
Complex values are not ordered real numbers
This example intentionally raises TypeError. The < operator asks whether the left value is smaller; Python does not define this ordering for complex values. Use .real or .imag only when your task explicitly calls for comparing those components. Equality and ordering are explored later.
Practice
Add complex(5, -2) and complex(-1, 4). Predict the two components, then print them. Next chapter: text foundations.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Complex components
Check your understanding of complex values.
What is printed?
print((2 - 3j).imag)Which expression constructs a complex value with real part 5 and imaginary part 2?
What is printed?
print((2 + 4j) + (3 - 1j))What is printed?
print((1j * 1j).real)What happens when the code runs?
print((1 + 1j) < (2 + 1j))