As you probably know only Pythonista, I was impressed by the fact that Python can handle complex numbers normally.
You can create a complex number (Complex
) by writing like 12 + 3j
. Add j to the imaginary part.
To retrieve the real and imaginary parts, use real
and ʻimag` as follows:
comp = 12 + 3j
print(comp.real)
# 12.0
print(comp.imag)
# 3.0
Multiplication and division are perfect.
print((1 + 2j) * (3 + 4j))
# (-5+10j)
print((1 + 2j) / (3 + 4j))
# (0.44+0.08j))
Recommended Posts