數值運算常用函式

  1. abs 函式計算絕對值。
    pyuser@deyu:~/zzz$ python
    Python 3.12.9 (main, Aug 14 2025, 00:00:00) [GCC 14.2.1 20250110 (Red Hat 14.2.1-7)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x=-5.2
    >>> y=2.3
    >>> print(abs(x),abs(y))
    5.2 2.3
    
  2. pow(x,y) 函式計算 x 的 y 次方。
    >>> x=2
    >>> y=3.1
    >>> print(pow(x,y))
    8.574187700290345
    
  3. round(x) 函式取 x 的「四捨五入」值,回傳整數。
    >>> x=3.5
    >>> y=2.4
    >>> print(round(x),round(y))
    4 2
    >>> print(type(round(x)),type(round(y)))
    <class 'int'> <class 'int'>
    >>> quit()