Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 · Getting Started

The Imaginary Unit and Complex Arithmetic

Learn the imaginary unit i, how to add, subtract, multiply and divide complex numbers, and how to check your answers with Python's built-in complex type.

Every real number squared is zero or positive, so the equation x² = -1 has no real solution. Mathematicians fixed this by inventing a new number, i, with the defining property i² = -1. Numbers of the form a + bi, where a and b are real, are called complex numbers. They obey the same algebra rules as real numbers, with one extra rule: whenever i² appears, replace it with -1.

This lesson sets up the arithmetic you will use everywhere in the track.

Anatomy of a complex number

For z = a + bi:

  • a is the real part, written Re(z).
  • b is the imaginary part, written Im(z). Note that Im(z) is the real number b, not bi.
  • Two complex numbers are equal exactly when both their real parts and their imaginary parts are equal.

Real numbers are the special case b = 0. Numbers with a = 0 and b ≠ 0 are called purely imaginary.

Powers of i cycle with period 4:

Power Value
i⁰ 1
i¹ i
i² -1
i³ -i
i⁴ 1

To compute i^n, divide n by 4 and look at the remainder. For example i^27: 27 = 4·6 + 3, so i^27 = i³ = -i.

Addition, subtraction and multiplication

Addition and subtraction work part by part:

Text
(a + bi) + (c + di) = (a + c) + (b + d)i
(a + bi) - (c + di) = (a - c) + (b - d)i

Multiplication uses the distributive law and i² = -1:

Text
(a + bi)(c + di) = ac + adi + bci + bd·i²
                 = (ac - bd) + (ad + bc)i

Worked example: (2 + 3i)(4 - i) = 8 - 2i + 12i - 3i² = 8 + 10i + 3 = 11 + 10i.

Division

To divide, multiply the top and bottom by the conjugate of the denominator. The conjugate of c + di is c - di, and the product (c + di)(c - di) = c² + d² is a real number.

Text
(a + bi)/(c + di) = (a + bi)(c - di) / (c² + d²)
                  = [(ac + bd) + (bc - ad)i] / (c² + d²)

Worked example: (1 + 2i)/(3 - i). Multiply by (3 + i)/(3 + i). Top: (1 + 2i)(3 + i) = 3 + i + 6i + 2i² = 1 + 7i. Bottom: 9 + 1 = 10. Result: 1/10 + (7/10)i.

Let us build these four operations as plain functions on pairs (a, b) and compare them with Python's own complex arithmetic.

Python Arithmetic from the formulas

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each line prints our pair first and Python's result second. They should agree, and the division line should match the hand calculation above, 0.1 + 0.7i.

Why the rules are consistent

You might worry that inventing i could break arithmetic. It does not. Formally, a complex number is just an ordered pair (a, b) of reals, with addition and multiplication defined by the formulas above. Then (0, 1) · (0, 1) = (-1, 0), which is the pair for the real number -1. So i = (0, 1) really does square to -1, and nothing is imaginary in a mystical sense. This is the construction the code above used.

The complex numbers form a field: addition and multiplication are commutative, associative and distributive, every nonzero number has a reciprocal, and so on. The set of all complex numbers is written ℂ. One thing you lose compared with the reals is a natural order: you cannot say that i is bigger or smaller than 1 in a way that respects arithmetic.

Do not write √-1 · √-1 = √((-1)(-1)) = 1. The rule √a · √b = √(ab) only holds for nonnegative reals. Always say i² = -1 rather than manipulating √-1.

Solving a quadratic with negative discriminant

The quadratic formula now always works. For z² + 2z + 5 = 0, the discriminant is 4 - 20 = -16, so √-16 = 4i and the roots are z = (-2 ± 4i)/2 = -1 ± 2i. Confirm with code.

Python A quadratic with complex roots

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The module cmath is the complex version of math; it will appear in later lessons.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is i^27?

    27 divided by 4 leaves remainder 3, and i³ = -i.
  2. 2. What is (2 + 3i)(4 - i)?

    8 - 2i + 12i - 3i² = 8 + 10i + 3 = 11 + 10i.
  3. 3. Why do we multiply by the conjugate when dividing?

    (c + di)(c - di) = c² + d², which is real, so the quotient can be split into a real and an imaginary part.
  4. 4. What is Im(5 - 7i)?

    The imaginary part is the real coefficient of i, which is -7.

Summary

  • i² = -1, and powers of i repeat every four steps.
  • Add and subtract part by part; multiply with the distributive law and replace i² by -1.
  • Divide by multiplying top and bottom by the conjugate of the denominator.
  • A complex number is an ordered pair of reals, which is why the arithmetic is consistent.

Next: Why Complex Numbers Exist: A Short History