复数的数学运算
一、 解决问题
使用复数来执行一些计算操作
二、解决方案
complex(real, imag), 带有后缀j的浮点数
三、代码说明
#!/usr/bin/env python
a = complex(2, 4)
b = 3 -5j
print (a) #->(2+4j)
print (b) #->(3-5j)
#属性
print (a.real) #->2.0
print (a.imag) #->4.0
#共轭
print (a.conjugate())#->(2-4j)
#+,-,*,/
print (a + b)#->(5-1j)
print (a - b)#->(-1+9j)
print (a * b)#->(26+2j)
print (a / b)#->(-0.4117647058823529+0.6470588235294118j)
#绝对值
print (abs(a))#->4.47213595499958
#正弦,余弦,正切 cmath包下(math包下面不支持复数)
import cmath
a = cmath.sqrt(-1)
print (a) #->1j
四、关联知识
五、总结
六、代码地址
github地址:https://github.com/weichen666/python_cookbook
目录/文件:third_selection/learn_data_date_time_complex.py