欢迎访问宙启技术站
智能推送

Python中的数学函数列表及用法

发布时间:2023-06-19 14:07:44

Python是一种广泛使用的编程语言,拥有丰富的数学函数库。这些函数使数学计算变得更加简单和高效。让我们探索一些 Python 中的数学函数及其使用方法。

1. abs(x): 返回x的绝对值。

print(abs(-10)) #输出10

2. pow(x, y): 返回x的y次方。

print(pow(2, 3)) #输出8

3. sqrt(x): 返回x的平方根。

import math
print(math.sqrt(25)) #输出5.0

4. floor(x): 返回不大于x的最大整数。

import math
print(math.floor(3.7)) #输出3

5. ceil(x): 返回不小于x的最小整数。

import math
print(math.ceil(3.2)) #输出4

6. round(x): 返回四舍五入后的整数。

print(round(3.2)) #输出3
print(round(3.7)) #输出4

7. sin(x): 返回x的正弦值。

import math
print(math.sin(0)) #输出0.0

8. cos(x): 返回x的余弦值。

import math
print(math.cos(0)) #输出1.0

9. tan(x): 返回x的正切值。

import math
print(math.tan(0)) #输出0.0

10. degrees(x): 将x从弧度转换为角度。

import math
print(math.degrees(math.pi)) #输出180.0

11. radians(x): 将x从角度转换为弧度。

import math
print(math.radians(180)) #输出3.141592653589793

12. log(x, base): 返回以base为底的x的对数。如果未指定base,则返回以自然对数e为底的x的对数。

import math
print(math.log(2)) #输出0.6931471805599453
print(math.log(100, 10)) #输出2.0

13. factorial(x): 返回x的阶乘。

import math
print(math.factorial(5)) #输出120

14. gcd(x, y): 返回x和y的最大公约数。

import math
print(math.gcd(24, 36)) #输出12

15. hypot(x, y): 返回x和y的欧式距离。

import math
print(math.hypot(3, 4)) #输出5.0

上述函数只是 Python 中数学函数的一小部分,还有很多其他函数,如特殊函数、统计函数、金融函数等。熟练掌握这些函数,可以使数学计算更加轻松和高效。