angrykola

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

math模块数学运算:

几种基本运算

+ - * / // % & | ~ ^ << >> **
加法 减法 乘法 除法 取整 取余 位与 位或 位翻转 位异或 左移 右移 幂次

 

 

>>> x,y = 3,7
>>> x/y;x//y;x%y   #除法、取整、取模
0.42857142857142855
0
3
>>> x*y;x**y    #乘法与幂乘
21
2187

math模块

#使用math模块
>>> import math
>>> dir
<built-in function dir>
>>> dir(math)
['__doc__', '__loader__', '__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', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

几个常用的函数:

ceil(x) 取顶
floor(x) 取底
fabs(x) 取绝对值
factorial(x) 阶乘
hypot(x,y) 直角三角形求斜边
pow(x,y) x的y次方
sqrt(x) 开平方
log(x) 以e为底x的对数
log10(x) 以10为底x的对数
trunc(x) 取整
isnan(x) 判断是否no a number
degree(x) 弧度转换角度
radians(x) 角度转换弧度

 

 

 

 

 

 

 

 

 

 

两个math模块提供的常量:

>>> math.e
2.718281828459045
>>> math.pi
3.141592653589793

 random模块

#random 随机模块
>>> import random
>>> random.random()
0.3378921408739398
>>> random.random()
0.1506362709021285
>>> random.random()
>>> from math import trunc
>>> for i in range(100):print( trunc(10 *random.random()),end=' ')
0 5 1 6 2 0 8 9 2 3 4 5 1 7 1 8 0 3 2 3 7 1 3 6 0 4 1 3 5 0 2 4 6 3 7 7 0 2 1 1 1 7 9 6 5 6 1 5 2 8 7 4 4 7 7 6 6 1 5 2 7 1 5 6 1 5 3 6 3 1 2 1 9 1 6 1 3 8 0 6 1 0 1 4 3 6 8 0 8 5 3 6 7 8 5 4 5 3 4 6 

 

posted on 2013-11-09 11:11  kolaman  阅读(462)  评论(0)    收藏  举报