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)