Python math 模块
先来看一下math模块中包含内容,如下所示:
>>>importmath>>>dir(math)['__doc__','__loader__','__name__','__package__','__spec__','acos','acosh','asin','asinh','atan','atan2','atanh','ceil','copysign','cos','cosh','degrees','e','erf','erfc','exp','expm1','fabs','factorial','floor','fmod','frexp','fsum','gamma','gcd','hypot','inf','isclose','isfinite','isinf','isnan','ldexp','lgamma','log','log10','log1p','log2','modf','nan','pi','pow','radians','sin','sinh','sqrt','tan','tanh','tau','trunc']
接下来具体看一下该模块的常用函数和常量。
ceil(x)返回x的上限,即大于或者等于x的最小整数。看下示例:
importmathx=-1.5print(math.ceil(x))
floor(x)返回x的向下取整,小于或等于x的最大整数。看下示例:
importmathx=-1.5print(math.floor(x))
fabs(x)返回x的绝对值。看下示例:
importmathx=-1.5print(math.fabs(x))
fmod(x,y)返回x/y的余数,值为浮点数。看下示例:
importmathx=3y=2print(math.fmod(x,y))
factorial(x)返回x的阶乘,如果x不是整数或为负数时则将引发ValueError。看下示例:
importmathx=3print(math.factorial(3))
pow(x,y)返回x的y次幂。看下示例:
importmathx=3y=2print(math.pow(x,y))
fsum(iterable)返回迭代器中所有元素的和。看下示例:
importmathprint(math.fsum((1,2,3,4,5)))
gcd(x,y)返回整数x和y的最大公约数。看下示例:
importmathx=9y=6print(math.gcd(x,y))
sqrt(x)返回x的平方根。看下示例:
importmathx=9print(math.sqrt(x))
trunc(x)返回x的整数部分。看下示例:
importmathx=1.1415926print(math.trunc(x))
exp(x)返回e的x次幂。看下示例:
importmathx=2print(math.exp(2))
log(x[,base])返回x的对数,底数默认为e。看下示例:
importmathx=10y=10#不指定底数print(math.log(x))#指定底数print(math.log(x,y))
常量
importmath#常量eprint(math.e)#常量πprint(math.pi)
tan(x)返回x弧度的正切值。看下示例:
importmathprint(math.tan(math.pi/3))
atan(x)返回x的反正切值。看下示例:
importmathprint(math.atan(1))
sin(x)返回x弧度的正弦值。看下示例:
importmathprint(math.sin(math.pi/3))
asin(x)返回x的反正弦值。看下示例:
importmathprint(math.asin(1))
cos(x)返回x弧度的余弦值。看下示例:
importmathprint(math.cos(math.pi/3))
acos(x)返回x的反余弦值。看下示例:
importmathprint(math.acos(1))
以上内容为大家介绍了Pythonmath模块,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
如何使用python中的add函数?
如何使用python中的add函数?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。add函数使用方法1、numpy中加法运算使用实例importnump...详情>>
2023-11-14 14:11:16
python如何将九九乘法表写入到Excel?
python如何将九九乘法表写入到Excel?现在使用python去输出九九乘法表,已经不再稀奇,我们经常输出的环境是文本,但是今天教大家更为复杂一点的...详情>>
2023-11-14 12:11:28
python中altair可视化库怎么用?
python中altair可视化库怎么用?作为六大python可视化库,基本上学会都是可以通吃任何领域的存在,本章要给大家介绍的Altair就是其中之一的可视...详情>>
2023-11-14 09:40:29
python中最小二乘法如何理解?
python中最小二乘法如何理解?python中在实现一元线性回归时会使用最小二乘法,那你知道最小二乘法是什么吗。其实最小二乘法为分类回归算法的基...详情>>
2023-11-14 06:58:12热门推荐
如何使用python中的add函数?
沸python中dir函数如何使用?
热python中merge函数如何使用?
热python中str内置函数总结归纳
新python如何将九九乘法表写入到Excel?
Python的scikit-image模块是什么?
python timedelta函数是什么?
python中如何使用np.concatenate()拼接numpy数组
Python jieba库分词模式怎么用?
python中altair可视化库怎么用?
TCP在python中如何连接服务器?
python中使用__slots__定义类属性
python中的unittest框架是什么?
python字典获取对应键的方法
技术干货






