## 常见的数学函数
| 函数名 | 描述 |
|---|---|
| abs(x) | 返回数字的绝对值,如abs(-10) 返回 10 |
| fabs(x) | 返回数字的绝对值,如math.fabs(-10) 返回10.0 |
| ceil(x) | 返回数字的上入整数,如math.ceil(4.1) 返回 5 |
| floor(x) | 返回数字的下舍整数,如math.floor(4.9)返回 4 |
| round(x [,n]) | 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。 |
| exp(x) | 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045 |
| log(x) | 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 |
| log10(x) | 返回以10为基数的x的对数,如math.log10(100)返回 2.0 |
| max(x1, x2,…) | 返回给定参数的最大值,参数可以为序列。 |
| min(x1, x2,…) | 返回给定参数的最小值,参数可以为序列。 |
| modf(x) | 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。 |
| pow(x, y) | x**y 运算后的值。 |
| sqrt(x) | 返回数字x的平方根 |
| cmp(x, y) | py2,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
举例:
- abs和fabs的区别
- fabs()的使用需要导入数学模块(math),而fabs()不需要
- 返回类型不同
import math
>>> a = abs(-9)
>>> b =math.fabs(-9)
>>> a,type(a)
(9, <class 'int'>)
>>> b,type(b)
(9.0, <class 'float'>)
>>>
拓展(dir()和dir(参数)):
- dir()不带参数:查看当前环境下所有的变量名称
- dir(参数):返回对应的书或者方法
>>> import math
>>> 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'

本文介绍了Python中常见的数学函数,包括`abs`和`fabs`的区别,以及指数和对数的基础概念。此外,还详细讲解了`random`模块的各种随机数生成函数,如`random.random()`、`random.uniform()`等。最后提到了字符串这一数据类型,说明了如何创建和使用字符串。
最低0.47元/天 解锁文章
856

被折叠的 条评论
为什么被折叠?



