文章链接:https://blog.csdn.net/qq_40801709/article/details/86543515
- math模块
- Math.log(x, a) 以a为底的x的对数
- Math.pow(x, y) x的y次幂
- cmath模块
- Cmath.polar() 极坐标
- Cmath.rect() 笛卡尔坐标
- .real取复数实部 .imag取复数虚部
>>> import cmath
>>> z=1+2j
>>> (a,b) = cmath.polar(z)
>>> (a,b)
(2.23606797749979, 1.1071487177940904)
>>> cmath.rect(a,b)
(1.0000000000000002+2j)
>>> tmp = complex(round(c.real,2),c.imag)
>>> tmp 【复数的实部虚部都是只读的,所以只能重新赋值改变小数点后位数】
(1+2j)
- decimal模块
- from decimal import Decimal
- 用于浮点数
- fractions模块
- 生成分数
>>> import fractions
>>> fractions.Fraction(1,4)/ fractions.Fraction('0.25')
Fraction(1, 1)
>>> fractions.Fraction.from_float(1.75)
Fraction(7, 4)
- random模块
- 伪随机数 random()
- 随机数种子
random.seed(a) 【一般以时间作为种子】
>>> from random import *
>>> random() 范围【0,1)
0.9478274870593494
>>> uniform(1,100) 范围【0,100)浮点数
6.59858540495406
>>> randint(1,100) 范围【1,100】整数
11
>>> randrange(1,100,2) 范围【1,100)2递增整数
47
>>> getrandbits(5) 范围5位二进制整数范围内
26
- Choice() 从指定序列随机选择一个元素
- Sample() 能指定每次随机元素的个数
- Shuffle() 可以将可变序列中的所有元素随机排序
From random import *
>>> colors = ['r','b','y','g']
>>> random.choice(colors)
>>> choice(colors)
'b'
>>> sample(colors,2)
['y', 'b']
>>> shuffle(colors)
>>> colors
['g', 'r', 'y', 'b']