這篇文章主要介紹了Python中用于計算對數的log()方法,是Python入門基礎中的必會的方法,需要的朋友可以參考下
log()方法返回x的自然對數,對于x>0。
語法
以下是log()方法的語法:
importmath
math.log(x)
注意:此函數是無法直接訪問的,所以我們需要導入math模塊,然后需要用math的靜態對象來調用這個函數。
參數
x--這是一個數值表達式。
返回值
此方法返回x的自然對數,對于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)
當我們運行上面的程序,它會產生以下結果:
math.log(100.12):4.60636946656
math.log(100.72):4.61234438974
math.log(119L):4.77912349311
math.log(math.pi):1.14472988585
以上內容為大家介紹了python中的對數怎么寫,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。