python基础查漏补缺1--算数、字符串与变量

1. math相关函数

函  数 描     述
ceil(x)  大于或等于x的整数
cos(x) 

x的余弦

degrees(x) 将x的弧度转换为度数
exp(x) e的x次方
factorial(n) 计算n的阶乘(n!),n 必须为整数
log(x) 以e为底的x的对数
log(x,b) 以b为底的x的对数
pow(x,y) x的y次方
radians(s) 将x转换为弧度数
sin(x) x的正弦
sqrt(x) x的平方根
tan(x) x的正切

>>> math.ceil(1.1) 2 >>> math.cos(1) 0.5403023058681398 >>> math.degrees(1) 57.29577951308232 >>> math.exp(1) 2.718281828459045 >>> math.factorial(5) 120 >>> math.log(1) 0.0 >>> math.log(2,4) 0.5 >>> math.pow(2,3) 8.0 >>> math.sin(90) 0.8939966636005579 >>> math.sqrt(3) 1.7320508075688772 >>> math.tan(90) -1.995200412208242

2. 字符串拼接相关方法 +  *

>>> 'hot' + 'dog'
'hotdog'
>>> 10 * 'haha'
'hahahahahahahahahahahahahahahahahahahaha'
>>> 3 * 'hee' + 2 * '!'
'heeheehee!!'

3. 列出模块中的函数 dir(module)

>>> 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', 'trunc']

4. 查看函数的帮助字符串 help(func)

>>> help(math.floor)
Help on built-in function floor in module math:

floor(...)
    floor(x)
    
    Return the floor of x as an Integral.
    This is the largest integer <= x.

5. 查看文档字符串 func.__doc__

>>> print(math.floor.__doc__)
floor(x)

Return the floor of x as an Integral.
This is the largest integer <= x.

6. 将整数和字符串转换为浮点数 float(x), x 为str 或 int类型

>>> float(3)
3.0
>>> float('3')
3.0

7. 浮点数转换为整数,int(x)舍去小数,round(x)为银行家圆整--最接近的偶数

>>> int(8.5)
8
>>> round(8.5)
8
>>> int(9.5)
9
>>> round(9.5)
10

8. 变量的多行赋值

>>> x, y, z = 1, 'two', 3.0
>>> x, y, z
(1, 'two', 3.0)
posted @ 2017-09-11 17:36  燃灯胡同  阅读(179)  评论(0编辑  收藏  举报