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.
Visual reference
What you will learn
- Complex notation —
a + bjanda - bj - Real and imaginary — access with
.realand.imag - Pure imaginary numbers — values like
5j - Common mistakes — why
iis 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:
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 |
# z = 3i
# SyntaxError: invalid decimal literalGeneral form
Every complex number follows:
a + bj| Symbol | Meaning | Example |
|---|---|---|
a |
Real part | 2 |
b |
Imaginary coefficient | 3 |
j |
Imaginary unit (√−1) | attached to b |
a = 2 + 3j
b = -2 - 3j
c = 10 + 2.5j
d = 2.3 + 4.6j
print(a)
print(b)
print(c)
print(d)(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.
x = 5j
y = -2.3j
print(x)
print(y)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).
Accessing real and imaginary parts
Use two built-in attributes (from your notes):
| Attribute | Returns |
|---|---|
.real |
Real component |
.imag |
Imaginary component |
z = 2 + 3j
print(z.real)
print(z.imag)2.0
3.0Important observation
You may write integers (2 and 3), but .real and .imag return floats. Internally, both parts are treated as float type.
print(type(z.real), type(z.imag))<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
z = 3.4j
print(z.real)
print(z.imag)0.0
3.4The value lies on the imaginary axis; real part is 0.0.
From your notes:
a = -3.5j
print(a.real, a.imag)-0.0 -3.5Common mistakes
Mistake 1 — using i:
# z = 5i
# SyntaxError: invalid decimal literalMistake 2 — using .imaginary:
z = 2 + 3j
# z.imaginary
# AttributeError: 'complex' object has no attribute 'imaginary'Correct:
z = 5j
print(z.imag)Where are complex numbers used?
Beginners use them less often, but they are essential in many fields.
Electrical engineering — AC circuits and impedance
Signal processing — Fourier transform
Quantum physics — wave functions
Medical imaging — MRI and reconstruction
Practice exercises
Exercise 1 — create z = 1 - 1j and print parts:
z = 1 - 1j
print(z.real)
print(z.imag)1.0
-1.0Exercise 2 — add two complex numbers:
a = 2 + 3j
b = 1 + 1j
print(a + b)(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
-
Imaginary unit in Python?
j, noti. -
Attributes for components?
.realand.imag. -
Type of
z.realfor2 + 3j?
float(2.0).
Summary
complexstores numbers with real and imaginary parts:a + bj.- Python uses
j(engineering convention), noti. - Read parts with
.realand.imag(both return float). 5jis valid; real part is0.0.- Used in EE, signal processing, physics, and scientific computing.
Next lesson: str — string data type (text, quotes, and operations).
Loading playground…