標籤:log 函數 function class UI post 運算 引入 常用函數
Python內建函數
四捨五入: round()
絕對值: abs()
1 >>> round(1.543,2) 保留兩位小數,四捨五入為1.54 2 1.54 3 >>> round(1.546,2) 保留兩位小數,四捨五入為1.55 4 1.55 5 >>> round(-1.536,2) 6 -1.54 7 >>> abs(5) 8 5 9 >>> abs(-5) 絕對值為510 5
math 模組
1 >>> import math 匯入math模組 2 >>> math.pi math的pi函數 3 3.141592653589793 4 >>> dir(math) 利用dir 查看math中的函數 5 [‘__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‘] 6 >>> help(math.fabs) 利用help 查看函數的詳細內容 7 Help on built-in function fabs in module math: 8 9 fabs(...)10 fabs(x)11 12 Return the absolute value of the float x. 返回浮點數x的絕對值13 14 >>>
>>> math.sqrt(9) 計算開平方3.0>>> math.floor(3.14) 地板,將某一個位置之後的全部取消掉3.0>>> math.floor(3.66)3.0
>>> math.fabs(2) 計算絕對值
2.0
>>> math.fmod(9,2) 計算餘數
1.0
解決浮點數運算問題Decimal
1 >>> from decimal import Decimal 引入Decimal 模組 2 >>> a = Decimal("0.1") 3 >>> b = Decimal("0.8") 4 >>> a + b 5 Decimal(‘0.9‘) 6 >>> from decimal import Decimal as D as 別名 7 >>> a = D("0.1") 8 >>> b = D("0.8") 9 >>> a + b10 Decimal(‘0.9‘)11 >>>
Python 學習筆記(五)常用函數