這篇文章主要介紹了Python中用于計(jì)算對(duì)數(shù)的log()方法,是Python入門基礎(chǔ)中的必會(huì)的方法,需要的朋友可以參考下
log()方法返回x的自然對(duì)數(shù),對(duì)于x>0。
語(yǔ)法
以下是log()方法的語(yǔ)法:
importmath
math.log(x)
注意:此函數(shù)是無(wú)法直接訪問(wèn)的,所以我們需要導(dǎo)入math模塊,然后需要用math的靜態(tài)對(duì)象來(lái)調(diào)用這個(gè)函數(shù)。
參數(shù)
x--這是一個(gè)數(shù)值表達(dá)式。
返回值
此方法返回x的自然對(duì)數(shù),對(duì)于x>0。
例子
下面的例子顯示了log()方法的用法。
#!/usr/bin/python
importmath#Thiswillimportmathmodule
print"math.log(100.12):",math.log(100.12)
print"math.log(100.72):",math.log(100.72)
print"math.log(119L):",math.log(119L)
print"math.log(math.pi):",math.log(math.pi)
當(dāng)我們運(yùn)行上面的程序,它會(huì)產(chǎn)生以下結(jié)果:
math.log(100.12):4.60636946656
math.log(100.72):4.61234438974
math.log(119L):4.77912349311
math.log(math.pi):1.14472988585
以上內(nèi)容為大家介紹了python中的對(duì)數(shù)怎么寫,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。