1.math模組
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
math.e
math.pi
math.copysign(x,y):返回與y同號的x值。
math.ceil(x);返回<=x的最小整數
math.floor(x):返回>=x 的最大整數
math.hypot(x,y):返回sqrt(x*x + y*y)
math.modf(x);返回x的小數部分和整數部分
math.trunc(x);返回x的整數部分
math.pow(x,y):返回x**y
math.log(x,a):返回返回以a為底x的對數,若不指定參數a,預設為e
math.log10(x)
2.random模組
random.random():產生一個0<=x,1的隨機浮點數
random.uniform();產生一個指定範圍內的隨機浮點數
random.randint(a,b):產生一個a<=x<=b之間的整數
random.randrange(a,b,step):產生一個隨機數
random.choice(sequence):從序列中擷取一個隨機元素,列表元組字串
random.shuffle(list):打亂列表
random.sample(sequence,k):從序列中隨機擷取指定長度為k的片段。
3.decimal模組
getcontext().prec,設定小數點的精度。
>>> from decimal import Decimal,getcontext>>> print(Decimal('1.0')/Decimal('3.0'))0.3333333333333333333333333333>>> getcontext().prec = 6>>> print(Decimal('1.0')/Decimal('3.0'))0.333333
4.fractions模組
用於表現和處理分數
>>> from fractions import Fraction>>> print Fraction(1,3)1/3