Mathew K Analytics

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…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Python 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)
(3+4j)
<class 'complex'>
3.0
4.0

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))
5j
(2.5+1.5j)
(3+0j)
<class 'complex'>

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)
(4+2j)
(2+6j)
(11-2j)
(-1+2j)
(-7+24j)
try:
    (3 + 4j) // (1 + 1j)
except TypeError as e:
    print(f'Caught: {e}')
Caught: unsupported operand type(s) for //: 'complex' and 'complex'

Part 4: No Ordering Comparisons#

print((3 + 4j) == (3 + 4j))
print((3 + 4j) == (3 + 5j))
print((3 + 4j) != (3 + 5j))
True
False
True
try:
    (3 + 4j) < (1 + 1j)
except TypeError as e:
    print(f'Caught: {e}')
Caught: '<' not supported between instances of 'complex' and 'complex'

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))
(3+4j)
(3+0j)
0j
(3+4j)
(5+0j)
(1+0j)
try:
    complex('3 + 4j')
except ValueError as e:
    print(f'Caught: {e}')
Caught: complex() arg is a malformed string

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}')
5.0
-3.0
<class 'float'>
Real: 5.0, Imaginary: -3.0

Part 7: conjugate()#

z = 3 + 4j
print(z.conjugate())
w = 3 - 4j
print(w.conjugate())
print(z * z.conjugate())
(3-4j)
(3+4j)
(25+0j)

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()))
5.0
5.0
True

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)
5.0
0.9272952180016122
0.9272952180016122
(3.0000000000000004+3.9999999999999996j)

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))
math.sqrt caught: math domain error
1j
2j
(-1+1.2246467991473532e-16j)
0j
3.141592653589793j

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))
3.14 + 2.72j
3.14+2.72j
(3.14159+2.71828j)
(3.14159+2.71828j)

Part 12: Immutability#

z = 3 + 4j
try:
    z.real = 10
except AttributeError as e:
    print(f'Caught: {e}')
Caught: readonly attribute

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))
(1j, -1j)
((2+0j), (1+0j))

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.