Python 學習筆記(五)常用函數

來源:互聯網
上載者:User

標籤: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 學習筆記(五)常用函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.