Python的数字函数随机函数指数对数

本文介绍了Python中的数字函数,包括`ads`和`fabs`的区别,`dir()`的使用,以及指数和对数的相关概念。同时,详细阐述了`random`模块的各种随机数生成方法,如`random.random()`、`random.uniform()`、`random.randint()`等,并提到了字符串的基础知识和常见操作。
摘要由CSDN通过智能技术生成

常见的数字函数

函数名 描述
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 "<stdin>", line 1, in <module>
NameError: name 'fabs' is not defined

>>> 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', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'math']

>>> from math import *
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值