Python Numbers and Math · Lesson 19 of 149

Python Complex Numbers

What you will learn

  • What a complex number is, and how to write one with j
  • Its .real and .imag parts, abs() and .conjugate()
  • Arithmetic with complex numbers
  • Why you cannot compare them with <
  • The cmath module for square roots, polar form and more

Complex numbers are used in science, engineering, signal processing and electronics. If you do not need them yet, you can skip this lesson and return later.

1. What is a complex number?

Some equations have no answer among ordinary (real) numbers. For example, x * x = -1 has none, because no real number multiplied by itself is negative. Mathematicians invented a number, called the imaginary unit, whose square is -1. A complex number has two parts:

real part + imaginary part × (the imaginary unit)

Mathematicians write the unit as i. Python uses j (as engineers do) and puts it right after a number, with no space and no *:

z = 3 + 4j
print(z, type(z))
print(z.real, z.imag)
(3+4j) <class 'complex'>
3.0 4.0
  • 3 + 4j has real part 3 and imaginary part 4.
  • z.real and z.imag read the two parts. Both are floats.
  • A number such as 5j or 0.5j is a purely imaginary number (real part 0).

You can also build one with complex(real, imag) or from text:

print(complex(2, -1), complex("1+2j"))
(2-1j) (1+2j)
Quick check: what is the imaginary part of `7 - 2j`?

-2.0. The sign belongs to the part.

2. Arithmetic

The usual operators work:

print((1 + 2j) + (3 - 1j))
print((1 + 2j) * (3 - 1j))
print(1j * 1j)
print(2j ** 2)
(4+1j)
(5+5j)
(-1+0j)
(-4+0j)
  • Adding and subtracting work part by part.
  • Multiplying follows the rule that j * j is -1. In the second line: (1 + 2j)(3 - 1j) = 3 - 1j + 6j - 2j² and since j² is -1, this is 3 + 5j + 2, which is 5 + 5j.
  • 1j * 1j is -1, which is the definition of the unit, and Python shows it as (-1+0j) because the result is still a complex number.

Dividing works too, and abs gives the magnitude, the distance from zero:

z = 3 + 4j
print(abs(z))
print(z.conjugate())
print(z * z.conjugate())
5.0
(3-4j)
(25+0j)
  • abs(3 + 4j) is 5.0: the length of the long side of a triangle with sides 3 and 4.
  • z.conjugate() flips the sign of the imaginary part.
  • A number times its conjugate is real: 25 here, which is abs(z) ** 2.

3. Comparing

Complex numbers have no natural order, so < and > are errors. Only == and != work:

print((3 + 4j) == complex(3, 4))
print(3 + 0j == 3)
True
True
(1 + 2j) < (2 + 1j)
TypeError: '<' not supported between instances of 'complex' and 'complex'

Floor division is not defined either: (1 + 2j) // 2 raises a TypeError. To order complex numbers, compare their abs(...).

4. The cmath module

The math module works with real numbers only. For example, math.sqrt(1j) raises TypeError: must be real number, not complex. The cmath module offers the same tools for complex numbers:

import cmath

print(cmath.sqrt(-1), cmath.sqrt(-4))
print(cmath.exp(1j * cmath.pi))
1j 2j
(-1+1.2246467991473532e-16j)
  • cmath.sqrt(-1) is 1j, so square roots of negative numbers work here, unlike math.sqrt(-1) (lesson 3.6).
  • The second line is the famous identity "e to the power of iπ is -1". The tiny imaginary part is a floating-point effect (lesson 3.4).

A real number raised to a fractional power can also give a complex answer in Python:

print((-8) ** (1 / 3))
(1.0000000000000002+1.7320508075688772j)

This is one of three cube roots of -8, not the real value -2, which is why it may surprise you.

Polar form

A complex number can be described by its distance from zero and its angle (in radians) instead of its two parts:

import cmath

print(cmath.polar(1 + 1j))
print(cmath.phase(1j))
print(cmath.rect(2, 0))
(1.4142135623730951, 0.7853981633974483)
1.5707963267948966
(2+0j)
  • cmath.polar(z) gives (distance, angle).
  • cmath.phase(z) is just the angle.
  • cmath.rect(distance, angle) builds the number back.

5. Formatting

Format specifiers apply to both parts:

z = 3 + 4j
print(f"{z:.1f}", str(z), repr(1.5 + 2j))
3.0+4.0j (3+4j) (1.5+2j)

Worked example: a quadratic equation with no real answer

Solve x² + 2x + 5 = 0, with a = 1, b = 2, c = 5.

import cmath

a, b, c = 1, 2, 5
d = b * b - 4 * a * c
r1 = (-b + cmath.sqrt(d)) / (2 * a)
r2 = (-b - cmath.sqrt(d)) / (2 * a)
print(d, r1, r2)
print(a * r1 ** 2 + b * r1 + c)
-16 (-1+2j) (-1-2j)
0j
Step Calculation Result
1 discriminant b² - 4ac -16 (negative, so no real roots)
2 cmath.sqrt(-16) 4j
3 (-2 + 4j) / 2 and (-2 - 4j) / 2 -1+2j and -1-2j
4 check by putting r1 back in 0j

The two answers are conjugates of each other. With math.sqrt, step 2 would have stopped with a ValueError. The numbers in this example are chosen by us.

Common mistakes

  • Writing i instead of j. Python uses j.
  • Putting a space or * before j. 3 + 4j is a complex number; 4 j is an error; 4 * j looks for a name j.
  • Comparing with < or >. Compare abs(...) instead.
  • Using math functions on complex numbers. Use cmath.
  • Expecting an exact 0 from a calculation. Tiny floating-point leftovers such as 1.2e-16 are normal.

Key takeaways

  • A complex number is real part plus imaginary part, written 3 + 4j; read the parts with .real and .imag.
  • abs(z) is the distance from zero; z.conjugate() flips the sign of the imaginary part.
  • Arithmetic works, but <, > and // do not.
  • cmath provides square roots, exp, polar form and more for complex numbers.
  • Complex results appear when you take roots of negative numbers.

Practice

1. What are z.real and z.imag for z = 2 - 5j?

Show answer

2.0 and -5.0.

2. Calculate (2 + 3j) + (1 - 1j) and (2 + 3j) * 2.

Show answer

(3+2j) and (4+6j).

3. What is abs(6 + 8j)?

Show answer

10.0, because sqrt(6² + 8²) is sqrt(100).

4. Why does math.sqrt(-9) fail, and what works instead?

Show answer

math.sqrt accepts real numbers only and raises ValueError: math domain error. cmath.sqrt(-9) works and gives 3j.

5. Is (1 + 1j) < (2 + 2j) allowed? How can you tell which number is "bigger" in size?

Show answer

No: it raises a TypeError, because complex numbers have no natural order. Compare sizes with abs(1 + 1j) < abs(2 + 2j).

6. What does (1 + 2j).conjugate() give?

Show answer

(1-2j).