3.6. Math Complex Numbers
Complex number with real and imaginary parts
Engineering notation
j
not mathematicali
No space inside the expression
3.6.1. Defining complex
complex() # 0j
complex(real=0, imag=0) # 0+0j
complex(1) # (1+0j)
complex(imag=1) # 1j
complex(real=1) # (1+0j)
complex(real=1, imag=2) # (1+2j)
complex(1, 2) # (1+2j)
complex(1.12, 2.34) # (1.12+2.34j)
complex(1, 2.34) # (1+2.34j)
complex(1+2j) # (1+2j)
complex(1+2j, 3+4j) # (-3+5j)
complex('1+2j') # (1+2j)
complex('1.5+2.7j') # (1.5+2.7j)
complex('1 + 2j') # ValueError: complex() arg is a malformed string
3.6.2. Arithmetic on complex
a = complex(1, 2) # (1+2j)
b = 3+4j # (3+4j)
a + b # (4+6j)
a - b # (-2-2j)
a * b # (-5+10j)
a / b # (0.44+0.08j)
3.6.3. Builtin functions
3.6.4. Absolute
abs(1+2j)
# 2.23606797749979
abs(3+4j)
# 5.0
3.6.5. cmath
mathematical functions for complex numbers
Python complex number z is stored internally using rectangular or Cartesian coordinates