Python複數屬性和方法運算操作樣本,python複數
本文執行個體講述了Python複數屬性和方法運算操作。分享給大家供大家參考,具體如下:
#coding=utf8'''''複數是由一個實數和一個虛數組合構成,表示為:x+yj一個負數時一對有序浮點數(x,y),其中x是實數部分,y是虛數部分。Python語言中有關負數的概念:1、虛數不能單獨存在,它們總是和一個值為0.0的實數部分一起構成一個複數2、複數由實數部分和虛數部分構成3、表示虛數的文法:real+imagej4、實數部分和虛數部分都是浮點數5、虛數部分必須有尾碼j或J複數的內建屬性:複數對象擁有資料屬性,分別為該複數的實部和虛部。複數還擁有conjugate方法,調用它可以返回該複數的共軛複數對象。複數屬性:real(複數的實部)、imag(複數的虛部)、conjugate()(返回複數的共軛複數)'''class Complex(object): '''''建立一個靜態屬性用來記錄類版本號碼''' version=1.0 '''''建立個複數類,用於操作和初始化複數''' def __init__(self,rel=15,img=15j): self.realPart=rel self.imagPart=img #建立複數 def creatComplex(self): return self.realPart+self.imagPart #擷取輸入數字部分的虛部 def getImg(self): #把虛部轉換成字串 img=str(self.imagPart) #對字串進行切片操作擷取數字部分 img=img[:-1] return float(img)def test(): print "run test..........." com=Complex() Cplex= com.creatComplex() if Cplex.imag==com.getImg(): print com.getImg() else: pass if Cplex.real==com.realPart: print com.realPart else: pass #原複數 print "the religion complex is :",Cplex #求取共軛複數 print "the conjugate complex is :",Cplex.conjugate()if __name__=="__main__": test()
運算結果:
PS:這裡再為大家推薦幾款計算工具供大家進一步參考借鑒:
線上一元函數(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學計算機線上使用_進階計算機線上計算:
http://tools.jb51.net/jisuanqi/jsqkexue
線上計算機_標準計算機:
http://tools.jb51.net/jisuanqi/jsq