>>> math.pi 3.141592653589793
>>> dir(math) ['doc', 'name', 'package', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
Help on built-in function pow in module math: pow(...) pow(x, y) Return x**y (x to the power of y).
>>> 4**2 16 >>> math.pow(4,2) 16.0 >>> 4*2 8
特别注意,4**2 和 4*2 是有很大区别的。
用类似的方法,可以查看 math 模块中的任何一个函数的使用方法。
关于“函数”的问题,在这里不做深入阐述,看管姑且按照自己在数学中所学到去理解。后面会有专门研究函数的章节。
下面是几个常用的 math 模块中函数举例,看官可以结合自己调试的进行比照。
>>> math.sqrt(9) 3.0 >>> math.floor(3.14) 3.0 >>> math.floor(3.92) 3.0 >>> math.fabs(-2) # 等价于 abs(-2) 2.0 >>> abs(-2) 2 >>> math.fmod(5,3) # 等价于 5%3 2.0 >>> 5%3 2
几个常见函数
有几个常用的函数,列一下,如果记不住也不要紧,知道有这些就好了,用的时候就 google。
求绝对值
>>> abs(10) 10 >>> abs(-10) 10 >>> abs(-1.2) 1.2
四舍五入
>>> round(1.234) 1.0 >>> round(1.234,2) 1.23 >>> # 如果不清楚这个函数的用法,可以使用下面方法看帮助信息 >>> help(round)
Help on built-in function round in module builtin: round(...) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.
运算优先级
从小学数学开始,就研究运算优先级的问题,比如四则运算中“先乘除,后加减”,说明乘法、除法的优先级要高于加减。
对于同一级别的,就按照“从左到右”的顺序进行计算。
下面的表格中列出了 Python 中的各种运算的优先级顺序。不过,就一般情况而言,不需要记忆,完全可以按照数学中的去理解,因为人类既然已经发明了数学,在计算机中进行的运算就不需要从新编写一套新规范了,只需要符合数学中的即可。
上面的表格将 Python 中用到的与运算符有关的都列出来了,是按照从低到高的顺序列出的。虽然有很多还不知道是怎么回事,不过先列出来,等以后用到了,还可以回来查看。
最后,要提及的是运算中的绝杀:括号。只要有括号,就先计算括号里面的。这是数学中的共识,无需解释。
以上就是Python的math模块中的常用数学函数整理总结的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python的math模块中的常用数学函数整理总结的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did85691