python基础(五)------正则表达式re中的match、search、sub、compile、findall方法及使用
math模块及其常用方法- 必需先导入再使用
>>> import math
- 常量
# math.pi 数学常数 π = 3.141592...,精确到可用精度
# math.e 数学常数 e = 2.718281...,精确到可用精度
# math.tau 数学常数 τ = 6.283185...,精确到可用精度,Tau 是一个圆周常数,等于 2π,圆的周长与半径之比
# math.inf 浮点正无穷大
# -math.inf 浮点负无穷大
# math.nan 一个浮点的 "非数字"(NaN)值,要检查一个数字是否为NaN,使用 isnan() 函数
>>> import math
>>> math.nan == math.nan
False
>>> float('nan') == float('nan')
False
>>> math.isnan(math.nan)
True
>>> math.isnan(float('nan'))
True
- 常用方法
# math.ceil(x)返回 x 的向上取整,即大于或等于 x 的最小的整数 >>> math.ceil(1.2) 2 >>> math.ceil(5) 5 # math.copysign(x, y)返回一个基于 x 的绝对值和 y 的符号的浮点数 >>> math.copysign(-4.456789,0.0) 4.456789 >>> math.copysign(-4.456789,-0.0) -4.456789 # math.fabs(x)返回 x 的绝对值 >>> math.fabs(8) 8.0 >>> math.fabs(-8) 8.0 # math.factorial(x)以一个整数返回 x 的阶乘,如果 x 不是整数或为负数时则将引发 ValueError >>> math.factorial(3) # 1*2*3 6 >>> math.factorial(4) # 1*2*3*4 24 # math.floor(x)返回 x 的向下取整,小于或等于 x 的最大整数 >>> math.floor(2.8) 2 >>> math.floor(-2.8) -3 >>> math.floor(6) 6 # math.fmod(x, y)求余,符号有第一个数字的符号决定 >>> math.fmod(10,3) 1.0 >>> math.fmod(10.5,3) 1.5 >>> math.fmod(10,-3) 1.0 >>> math.fmod(-10,3) -1.0 >>> math.fmod(-10,-3) -1.0 # math.frexp(x)以 (m, e) 对的形式返回 x 的尾数和指数, m 是一个浮点数, e 是一个整数,正好是 x == m * 2**e ,如果 x 为零,则返回 (0.0, 0) >>> math.frexp(6) # 6 = 0.75 * 2^3 = 0.75 * 2 * 2 * 2 (0.75, 3) # math.fsum(iterable)返回迭代中的精确浮点值 >>> math.fsum([1.2,1.5,2,1.4]) 6.1 # math.gcd(*integers)返回给定的整数参数的最大公约数 >>> math.gcd(8,16) 8 >>> math.gcd(6,16) 2 # math.lcm(*integers)返回给定的整数参数的最小公倍数 >>> math.lcm(2,5) 10 # math.pow(x, y)将返回 x 的 y 次幂, pow(1.0, x) 和 pow(x, 0.0) 总是返回 1.0 ,即使 x 是零或NaN,如果 x 和 y 都是有限的, x 是负数, y 不是整数那么 pow(x, y) 是未定义的,并且引发 ValueError >>> math.pow(2,3) 8.0 >>> math.pow(-2,3) -8.0 >>> math.pow(2,-3) 0.125 # math.sqrt(x)对x进行开平方 >>> math.sqrt(4) 2.0 # math.isqrt(n)返回非负整数 n 的整数平方根 >>> math.isqrt(2) 4 # math.remainder(x, y)返回 x 相对于 y 的余数 >>> math.remainder(10,3) # 10 / 3 = 3......1 1.0 >>> math.remainder(10,2) # 10 / 2 = 5......0 0.0 # math.trunc(x)返回去除小数部分的 x ,只留下整数部分 >>> math.trunc(2.8) 2 >>> math.trunc(-2.8) -2 # math.modf(x)返回 x 的小数和整数部分,两个结果都带有 x 的符号并且是浮点数。 >>> math.modf(3.45) (0.4500000000000002, 3.0) >>> math.modf(-3.45) (-0.4500000000000002, -3.0) # math.prod(iterable, *, start=1)计算输入的 iterable 中所有元素的积,积的默认 start 值为 1 >>> math.prod([1,2,3]) # 1*1*2*3 6 >>> math.prod([1,2,3],start = 2) # 2*1*2*3 12
- 判断方法
# math.isfinite(x)如果 x 既不是无穷大也不是NaN,则返回 True ,否则返回 False ,0.0被认为是有限的 # math.isinf(x)如果 x 是正或负无穷大,则返回 True ,否则返回 False # math.isnan(x)如果 x 是 NaN(不是数字),则返回 True ,否则返回 False
- 三角函数相关
# math.acos(x)返回以弧度为单位的 x 的反余弦值 # math.asin(x)返回以弧度为单位的 x 的反正弦值 # math.atan(x)返回以弧度为单位的 x 的反正切值 # math.atan2(y, x)以弧度为单位返回 atan(y / x) # math.cos(x)返回 x 弧度的余弦值 # math.sin(x)返回 x 弧度的正弦值 # math.tan(x)返回 x 弧度的正切值 # math.degrees(x)将角度 x 从弧度转换为度数 # math.radians(x)将角度 x 从度数转换为弧度 # math.acosh(x)返回 x 的反双曲余弦值 # math.asinh(x)返回 x 的反双曲正弦值 # math.atanh(x)返回 x 的反双曲正切值 # math.cosh(x)返回 x 的双曲余弦值 # math.sinh(x)返回 x 的双曲正弦值 # math.tanh(x)返回 x 的双曲正切值
- 其他方法
# math.ldexp(x, i)返回 x * (2**i),是函数 frexp() 的反函数 # math.nextafter(x, y)返回 x 趋向于 y 的最接近的浮点数值,如果 x 等于 y 则返回 y # math.exp(x)返回 e 次 x 幂,其中 e = 2.718281... 是自然对数的基数 # math.expm1(x)返回 e 的 x 次幂 减1 # math.log(x[, base]) 使用一个参数,返回 x 的自然对数(底为 e );使用两个参数,返回给定的 base 的对数 x ,计算为 log(x)/log(base) 。 # math.log1p(x)返回 1+x 的自然对数(以 e 为底) # math.log2(x)返回 x 以2为底的对数 # math.log10(x)返回 x 底为10的对数 # math.dist(p, q)返回 p 与 q 两点之间的欧几里得距离,以一个坐标序列(或可迭代对象)的形式给出,两个点必须具有相同的维度 # math.hypot(*coordinates)返回欧几里得范数,这是从原点到坐标给定点的向量长度 # math.gamma(x)返回 x 处的伽马函数值 # math.lgamma(x)返回Gamma函数在 x 绝对值的自然对数
python模块(二)------random模块及其常用方法



