Python的数字函数随机函数指数对数
- 常见的数字函数
- ads 和 fabs 的区别
- dir()和 dir(参数):
- 指数和对数
- 1.random.random()
- 2.random.uniform(a,b)
- 3.random.randint(a,b)
- 4、random.randrange([start],[stop],[step])
- 5、random.choice(sequence)
- 6、random.shuffle(x[,random])
- 7、random.sample(sequence,k);
- 字符串
-
常见的数字函数
| 函数名 | 描述 |
|---|
| 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) | 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1 |
ads 和 fabs 的区别
- fads()需要导入数学模块;
- ads()不需要;
- 返回的数据类型不同 ads()返回 int,fads()返回float;
#没有导包
Traceback (most recent call last):
File "", line 1, in
NameError: name 'fabs' is not defined>>> import math
>>> a=abs(-9)
>>> b=math.fabs(-9)
>>> a,type(a)
(9, )
>>> b,type(b)
(9.0, )
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',
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!