python可以支持復數,復數的虛部用j或J來表示。
下面程序示范了復數的用法:
ac1=3+0.2j
print(ac1)
print(type(ac1))#輸出復數類型
ac2=4-0.1j
print(ac2)
#復數運行
print(ac1+ac2)#輸出(7+0.1j)
#導入cmath模塊
importcmath
#sqrt()是cmath模塊下的商數,用于計算平方根
ac3=cmath.sqrt(-1)
print(ac3)#輸出1j
以上內容為大家介紹了Python中怎么寫復數,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。