Python 中的 'math' 模块是一个内置模块,提供了对数学运算的支持。它包含了各种常用的数学函数和常量。

常见的数学函数包括:

  • abs(x):返回 x 的绝对值。
  • sqrt(x):返回 x 的平方根。
  • pow(x, y):返回 x 的 y 次幂。
  • log(x, base):返回以 base 为底的 x 的对数。
  • sin(x)、cos(x)、tan(x):返回 x 的正弦、余弦、正切值。
  • degrees(x)、radians(x):将角度 x 转换为弧度,或将弧度 x 转换为角度。

除了数学函数,'math' 模块还提供了一些常用的数学常量,如 π 和 e,可以通过 'math.pi' 和 'math.e' 进行访问。

要使用 'math' 模块,需要在代码中导入它:'import math'。然后可以通过 'math.函数名' 的方式调用相应的数学函数。

示例代码:

import math

print(math.sqrt(16))  # 输出:4.0
print(math.pow(2, 3))  # 输出:8.0
print(math.sin(math.pi / 2))  # 输出:1.0
print(math.degrees(math.pi / 2))  # 输出:90.0
Python Math 模块:数学运算的利器

原文地址: https://www.cveoy.top/t/topic/pea7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录