python🎁math
时间: 2023-12-25 19:29:41 浏览: 91
Python中的math模块提供了许多数学函数和常量。你可以使用这些函数来执行各种数学操作,例如计算平方根、对数、三角函数等。以下是一个使用math模块的例子:
```python
import math
# 计算平方根
num = 16
sqrt = math.sqrt(num)
print("Square root of", num, "is", sqrt) # 输出:Square root of 16 is 4.0
# 计算对数
x = 100
log = math.log(x)
print("Natural logarithm of", x, "is", log) # 输出:Natural logarithm of 100 is 4.605170185988092
# 计算三角函数
angle = math.pi / 4
sin = math.sin(angle)
cos = math.cos(angle)
print("Sine of", angle, "is", sin) # 输出:Sine of 0.7853981633974483 is 0.7071067811865476
print("Cosine of", angle, "is", cos) # 输出:Cosine of 0.7853981633974483 is 0.7071067811865476
# 获取常量
print("Value of pi is", math.pi) # 输出:Value of pi is 3.141592653589793
print("Value of e is", math.e) # 输出:Value of e is 2.718281828459045
```
阅读全文