Python math.log() 方法

定义和用法

math.log() 方法返回一个数的自然对数,或者是对某个底数的对数。

实例

求不同数的自然对数:

# 导入 math 库
import math 

# 返回不同数的自然对数:
print(math.log(2.7183))
print(math.log(2))
print(math.log(1))

亲自试一试

语法

math.log(x, base)

参数

参数 描述
x

必需。指定要计算对数的值。

如果该值为 0 或负数,它将返回 ValueError。

如果该值不是数字,它将返回 TypeError。

base 可选。要使用的对数底数。默认为 'e'。

技术细节

返回值: 浮点数,表示数的自然对数,或者是对某个底数的对数。
Python 版本: 已在 2.3 版本中更改。
Python 更新日志: 增加了 base 参数。