Python: Complex Numbers

By Xah Lee. Date: . Last updated: .

Complex number can be written as complex(3,4) or 3 + 4j.

A number with “j” appended, for example: 4j is the same as complex(0,4).

# python 3

cc = complex(3, 4)

# alternative notation
cc2 = 3 + 4j

print(cc)
# (3+4j)

print(cc2)
# (3+4j)

print(cc==cc2)
# True

Get Real and Imaginary Parts

# python 3

cc = complex(3, 4)

print(cc.real)
# 3.0

print(cc.imag)
# 4.0

Addition

# python 3

# addition
print(( complex(2, 3) + complex(4, 5) ))
# (6+8j)

# adding a scalar adds to the real part
print(( complex(3, 4) + 1) )
# (4+4j)

Multiplication

# python 3

# multiplication
print(( complex(1, 0) * complex(0, 1) ))
# (1j)

# scalar multiplication
print(( complex(3, 4) * 2) )
# (6+8j)

Length

abs(z) → length of a complex number z. That is, Sqrt[ real^2 + img^2]

# python 3

print( abs(complex(3, 4)) )
# 5.0

Angle

cmath.phase(z) → return angle of z in radians.

# python 3

import cmath

# gets angle. return in radians, between  [-π, π]
print( cmath.phase(0+1j) )
# 1.5707963267948966

Get Polar Coordinates

cmath.polar(z) → rectangular to polar coordinates. Return a tumple (length, angle).

#python 3

import cmath

z1 = complex(0, 1)

# get polar coordinates. Returns (length, angle).
print( cmath.polar(z1) )
# (1.0, 1.5707963267948966)

Polar To Rectangular

cmath.rect(length, angle_in_radians) → polar to rectangular. Returns a complex number.

# python 3

import cmath

# polar to rectangular. Input is (length, ‹angle in radians›). Returns a complex number
z2 = cmath.rect(1, cmath.pi)
print(z2)
# (-1+1.2246467991473532e-16j)
# really is just -1 + 0j

Constans π and e

# python 3

import cmath

print(cmath.pi)
# 3.141592653589793

print(cmath.e)
# 2.718281828459045

for a refresher on complex numbers, see Understanding Complex Numbers

If you have a question, put $5 at patreon and message me.

Python

  1. Python 3 Basics
  2. Python 2 Basics
  3. Python 2 and 3 Difference
  4. Print Version
  5. Builtin Help
  6. Quote String
  7. String Methods
  8. Format String
  9. Operators
  10. Complex Numbers
  11. True, False
  12. if then else
  13. Loop
  14. List Basics
  15. Loop Thru List
  16. Map f to List
  17. Copy Nested List
  18. List Comprehension
  19. List Methods
  20. Sort
  21. Dictionary
  22. Loop Thru Dict
  23. Dict Methods
  24. Tuple
  25. Sets
  26. Function
  27. Closure
  28. 2 Closure
  29. Decorator
  30. Class
  31. Object, ID, Type
  32. List Modules
  33. Write a Module
  34. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. File Path
  4. Process Unicode
  5. Convert File Encoding
  6. Find Replace in dir
  7. Find Replace by Regex
  8. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Append String in Loop
  7. Timing f timeit
  8. Keyword Arg Default Value Unstable
  9. Check Page Load Size
  10. Thumbnail Generation