python中的math模块

import math

圆周率

print(math.pi)

e返回数学常数

print(math.e)

向上取整

x=-5.3
print(math.ceil(x))(x))

向下取整

print(math.floor(x))

返回e的x次幂

print(math.exp(2))

返回x的y次幂:返回结果是浮点类型

print(math.pow(2,5))

返回以base为底,x的对数,base默认以e为底

print(math.log(30))
print(math.log(400,20))

返回float类型的绝对值

print(math.fabs(-3))
print(math.fabs(-3.5))
print(abs(-3.5))

x的阶乘

print(math.factorial(3))

取余

正数,商向下取整,负数,商向上取整

print(math.fmod(5,3))

累加和,可以取浮点数

print(math.fsum([1,2,3,4]))

最大公约数

print(math.gcd(12,15))

返回平方根

print(math.sqrt(2))

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythonmath模块提供了许多用于数学运算的函数,包括三角函数、指数函数、对数函数、常数pi和e等。下面是一些常用的函数及其用法: 1. math.pi:返回常数π的值。 ```python import math print(math.pi) # 3.141592653589793 ``` 2. math.e:返回常数e的值。 ```python import math print(math.e) # 2.718281828459045 ``` 3. math.sqrt(x):返回x的平方根。 ```python import math print(math.sqrt(16)) # 4.0 ``` 4. math.pow(x, y):返回x的y次幂。 ```python import math print(math.pow(2, 3)) # 8.0 ``` 5. math.exp(x):返回e的x次方。 ```python import math print(math.exp(1)) # 2.718281828459045 ``` 6. math.log(x, [base]):返回以base为底数的x的对数。如果不写base,则默认为e的底数。 ```python import math print(math.log(10)) # 2.302585092994046 print(math.log(10, 2)) # 3.3219280948873626 ``` 7. math.sin(x):返回x的正弦值。 ```python import math print(math.sin(math.pi/2)) # 1.0 ``` 8. math.cos(x):返回x的余弦值。 ```python import math print(math.cos(math.pi)) # -1.0 ``` 9. math.tan(x):返回x的正切值。 ```python import math print(math.tan(math.pi/4)) # 0.9999999999999999 ``` 10. math.degrees(x):将弧度转换为角度。 ```python import math print(math.degrees(math.pi/2)) # 90.0 ``` 11. math.radians(x):将角度转换为弧度。 ```python import math print(math.radians(90)) # 1.5707963267948966 ``` 除了上述函数,math模块还提供了许多其他的数学函数。可以通过Python官方文档查看更多详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值