Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

complex — Complex Number Data Type

Understand complex numbers, the j notation, real and imaginary parts, and why Python uses j instead of the mathematical i.

Beginner15 min

Visual reference

Complex plane and De Moivre's theoremComplex plane and De Moivre's theorem
Imaginary numbers in electrical circuitsImaginary numbers in electrical circuits
Python numeric types including complexPython numeric types including complex

What you will learn

  • Complex notation — a + bj and a - bj
  • Real and imaginary — access with .real and .imag
  • Pure imaginary numbers — values like 5j
  • Common mistakes — why i is invalid in Python

Big picture: what is a complex number?

So far in the fundamental types:

Type Example Role
int 5 Whole numbers
float 3.14 Decimal numbers
bool True Logical values
complex 2 + 3j Real + imaginary parts

A complex number has two parts:

Real part plus imaginary part

Together they form 2 + 3j.

complex is a pre-defined class and a fundamental type. Purpose: store complex data in the form a + bj or a - bj.

Why does Python use j instead of i?

In mathematics, the imaginary unit is often written i, where i² = −1.

In Python (and much of engineering), the imaginary unit is j, where j² = −1.

Reason: in electrical engineering, i often means electric current, so j avoids confusion.

Valid Invalid
3j, 2 + 3j 3i, 10 + 2i
python
# z = 3i
# SyntaxError: invalid decimal literal

General form

Every complex number follows:

text
a + bj
Symbol Meaning Example
a Real part 2
b Imaginary coefficient 3
j Imaginary unit (√−1) attached to b
python
a = 2 + 3j
b = -2 - 3j
c = 10 + 2.5j
d = 2.3 + 4.6j

print(a)
print(b)
print(c)
print(d)
text
(2+3j)
(-2-3j)
(10+2.5j)
(2.3+4.6j)

Python prints complex values in parentheses.

Pure imaginary numbers

A complex number does not need a visible real part. 0 + 5j can be written as 5j.

python
x = 5j
y = -2.3j

print(x)
print(y)
text
5j
(-0-2.3j)

Why -0? The real part is 0.0 and the imaginary coefficient is negative — Python’s display shows (-0-2.3j) (your notes mark this as important).

Breaking -2.3j into real 0.0 and imag -2.3

Accessing real and imaginary parts

Use two built-in attributes (from your notes):

Attribute Returns
.real Real component
.imag Imaginary component
python
z = 2 + 3j

print(z.real)
print(z.imag)
text
2.0
3.0

Important observation

You may write integers (2 and 3), but .real and .imag return floats. Internally, both parts are treated as float type.

python
print(type(z.real), type(z.imag))
text
<class 'float'> <class 'float'>

Interactive: build your own complex number

In the playground below, change z = 2 + 3j to any a + bj, run, and inspect .real and .imag. Try (2 + 3j) + (1 + 1j) to get (3+4j).

Imaginary-only example

python
z = 3.4j

print(z.real)
print(z.imag)
text
0.0
3.4
3.4j on the imaginary axis

The value lies on the imaginary axis; real part is 0.0.

From your notes:

python
a = -3.5j
print(a.real, a.imag)
text
-0.0 -3.5

Common mistakes

Mistake 1 — using i:

python
# z = 5i
# SyntaxError: invalid decimal literal

Mistake 2 — using .imaginary:

python
z = 2 + 3j
# z.imaginary
# AttributeError: 'complex' object has no attribute 'imaginary'

Correct:

python
z = 5j
print(z.imag)

Where are complex numbers used?

Beginners use them less often, but they are essential in many fields.

AC circuits and impedanceAC circuits and impedance

Electrical engineering — AC circuits and impedance

Fourier transform and spectraFourier transform and spectra

Signal processing — Fourier transform

Quantum wave functionsQuantum wave functions

Quantum physics — wave functions

MRI scannerMRI scanner

Medical imaging — MRI and reconstruction

Practice exercises

Exercise 1 — create z = 1 - 1j and print parts:

python
z = 1 - 1j
print(z.real)
print(z.imag)
text
1.0
-1.0

Exercise 2 — add two complex numbers:

python
a = 2 + 3j
b = 1 + 1j
print(a + b)
text
(3+4j)

Exercise 3 — why does 5j have real == 0.0?

Because 5j is 0 + 5j. The real component exists; it is 0.0.

Quick knowledge check

  1. Imaginary unit in Python?
    j, not i.

  2. Attributes for components?
    .real and .imag.

  3. Type of z.real for 2 + 3j?
    float (2.0).

Summary

  • complex stores numbers with real and imaginary parts: a + bj.
  • Python uses j (engineering convention), not i.
  • Read parts with .real and .imag (both return float).
  • 5j is valid; real part is 0.0.
  • Used in EE, signal processing, physics, and scientific computing.

Next lesson: str — string data type (text, quotes, and operations).

Loading playground…