Lesson 3 · Python data types deep dive
Python Complex Numbers Explained: Real & Imaginary | Data Types #3
Video three of the twelve-part series: the third and final built-in numeric type. Every literal form, every operator, all three public complex methods, and…
- CoursePython data types deep dive
- Lesson3 of 12
- Video20 min
- FormatJupyter notebook · 16 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Data Types Deep-Dive, Video 3: Complex Numbers (complex)#
- Video three of the twelve-part series: the third and final built-in numeric type.
- Every literal form, every operator, all three public complex methods, and the full cmath module that complements them.
- Let's get into it.
Part 1: What Makes complex Different#
z = 3 + 4j
print(z)
print(type(z))
print(z.real)
print(z.imag)
Part 2: Complex Literals#
purely_imaginary = 5j
also_valid = 5J
with_decimal = 2.5 + 1.5j
zero_imaginary = 3 + 0j
print(purely_imaginary)
print(with_decimal)
print(zero_imaginary)
print(type(zero_imaginary))
Part 3: Arithmetic Operators#
a = 3 + 4j
b = 1 - 2j
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** 2)
try:
(3 + 4j) // (1 + 1j)
except TypeError as e:
print(f'Caught: {e}')
Part 4: No Ordering Comparisons#
print((3 + 4j) == (3 + 4j))
print((3 + 4j) == (3 + 5j))
print((3 + 4j) != (3 + 5j))
try:
(3 + 4j) < (1 + 1j)
except TypeError as e:
print(f'Caught: {e}')
Part 5: Converting Values to complex#
print(complex(3, 4))
print(complex(3))
print(complex())
print(complex('3+4j'))
print(complex(5))
print(complex(True, False))
try:
complex('3 + 4j')
except ValueError as e:
print(f'Caught: {e}')
Part 6: real and imag#
z = 5 - 3j
print(z.real)
print(z.imag)
print(type(z.real))
print(f'Real: {z.real}, Imaginary: {z.imag}')
Part 7: conjugate()#
z = 3 + 4j
print(z.conjugate())
w = 3 - 4j
print(w.conjugate())
print(z * z.conjugate())
Part 8: abs() for Magnitude#
z = 3 + 4j
print(abs(z))
import math
print(math.sqrt(z.real ** 2 + z.imag ** 2))
print(abs(z) == abs(z.conjugate()))
Part 9: The cmath Module: Polar Form#
import cmath
z = 3 + 4j
magnitude, angle = cmath.polar(z)
print(magnitude)
print(angle)
print(cmath.phase(z))
rebuilt = cmath.rect(magnitude, angle)
print(rebuilt)
Part 10: The cmath Module: Roots, Exponents, and Logarithms#
import cmath
import math
try:
math.sqrt(-1)
except ValueError as e:
print(f'math.sqrt caught: {e}')
print(cmath.sqrt(-1))
print(cmath.sqrt(-4))
print(cmath.exp(1j * cmath.pi))
print(cmath.log(1))
print(cmath.log(-1))
Part 11: Formatting Complex Numbers#
z = 3.14159 + 2.71828j
print(f'{z.real:.2f} + {z.imag:.2f}j')
print(f'{z:.2f}')
print(str(z))
print(repr(z))
Part 12: Immutability#
z = 3 + 4j
try:
z.real = 10
except AttributeError as e:
print(f'Caught: {e}')
Part 13: Common Patterns and Real Uses#
def solve_quadratic(a, b, c):
discriminant = complex(b ** 2 - 4 * a * c)
root = cmath.sqrt(discriminant)
x1 = (-b + root) / (2 * a)
x2 = (-b - root) / (2 * a)
return x1, x2
import cmath
print(solve_quadratic(1, 0, 1))
print(solve_quadratic(1, -3, 2))
Wrap-Up: What You Learned#
- Complex literals: the j suffix, purely imaginary values, and why a zero imaginary part still stays complex.
- Arithmetic operators, and why floor division and modulo are unsupported.
- No ordering comparisons, only equality, and using abs() for magnitude comparisons instead.
- Converting to complex with complex(), from numbers or from strict string syntax.
- All three public complex methods: real, imag, and conjugate, where conjugate genuinely does real work.
- abs() for magnitude, and the cmath module for polar form, roots of negative numbers, exponentials, and logarithms.
- Formatting, immutability, and a real quadratic-solver example that never fails on a negative discriminant.
- That's the numeric trio complete: int, float, and complex. Next up: bool and None, and the truthiness rules every other type follows.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



