python中math模組的使用方法

2021-07-22 08:50:45 字數 3343 閱讀 1420

函式說明例項

math.e

自然常數e

>>> math.e

2.718281828459045

math.pi

圓周率pi

>>> math.pi

3.141592653589793

math.degrees(x)

弧度轉度

>>> math.degrees(math.pi)

180.0

math.radians(x)

度轉弧度

>>> math.radians(45)

0.7853981633974483

math.exp(x)

返回e的x次方

>>> math.exp(2)

7.38905609893065

math.expm1(x)

返回e的x次方減1

>>> math.expm1(2)

6.38905609893065

math.log(x[, base])

返回x的以base為底的對數,base預設為e

>>> math.log(math.e)

1.0>>> math.log(2, 10)

0.30102999566398114

math.log10(x)

返回x的以10為底的對數

>>> math.log10(2)

0.30102999566398114

math.log1p(x)

返回1+x的自然對數(以e為底)

>>> math.log1p(math.e-1)

1.0math.pow(x, y)

返回x的y次方

>>> math.pow(5,3)

125.0

math.sqrt(x)

返回x的平方根

>>> math.sqrt(3)

1.7320508075688772

math.ceil(x)

返回不小於x的整數

>>> math.ceil(5.2)

6.0math.floor(x)

返回不大於x的整數

>>> math.floor(5.8)

5.0math.trunc(x)

返回x的整數部分

>>> math.trunc(5.8)

5math.modf(x)

返回x的小數和整數

>>> math.modf(5.2)

(0.20000000000000018, 5.0)

math.fabs(x)

返回x的絕對值

>>> math.fabs(-5)

5.0math.fmod(x, y)

返回x%y(取餘)

>>> math.fmod(5,2)

1.0math.fsum([x, y, ...])

返回無損精度的和

>>> 0.1+0.2+0.3

0.6000000000000001

>>> math.fsum([0.1, 0.2, 0.3])

0.6math.factorial(x)

返回x的階乘

>>> math.factorial(5)

120math.isinf(x)

若x為無窮大,返回true;否則,返回false

>>> math.isinf(1.0e+308)

false

>>> math.isinf(1.0e+309)

true

math.isnan(x)

若x不是數字,返回true;否則,返回false

>>> math.isnan(1.2e3)

false

math.hypot(x, y)

返回以x和y為直角邊的斜邊長

>>> math.hypot(3,4)

5.0math.copysign(x, y)

若y<0,返回-1乘以x的絕對值;

否則,返回x的絕對值

>>> math.copysign(5.2, -1)

-5.2

math.frexp(x)

返回m和i,滿足m乘以2的i次方

>>> math.frexp(3)

(0.75, 2)

math.ldexp(m, i)

返回m乘以2的i次方

>>> math.ldexp(0.75, 2)

3.0math.sin(x)

返回x(弧度)的三角正弦值

>>> math.sin(math.radians(30))

0.49999999999999994

math.asin(x)

返回x的反三角正弦值

>>> math.asin(0.5)

0.5235987755982989

math.cos(x)

返回x(弧度)的三角余弦值

>>> math.cos(math.radians(45))

0.7071067811865476

math.acos(x)

返回x的反三角余弦值

>>> math.acos(math.sqrt(2)/2)

0.7853981633974483

math.tan(x)

返回x(弧度)的三角正切值

>>> math.tan(math.radians(60))

1.7320508075688767

math.atan(x)

返回x的反三角正切值

>>> math.atan(1.7320508075688767)

1.0471975511965976

math.atan2(x, y)

返回x/y的反三角正切值

>>> math.atan2(2,1)

1.1071487177940904

math.sinh(x)

返回x的雙曲正弦函式

math.asinh(x)

返回x的反雙曲正弦函式

math.cosh(x)

返回x的雙曲余弦函式

math.acosh(x)

返回x的反雙曲余弦函式

math.tanh(x)

返回x的雙曲正切函式

math.atanh(x)

返回x的反雙曲正切函式

math.erf(x)

返回x的誤差函式

math.erfc(x)

返回x的餘誤差函式

math.gamma(x)

返回x的伽瑪函式

math.lgamma(x)

返回x的絕對值的自然對數的伽瑪函式

python中math模組的使用方法

函式 說明例項 math.e 自然常數e math.e 2.718281828459045 math.pi 圓周率pi math.pi 3.141592653589793 math.degrees x 弧度轉度 math.degrees math.pi 180.0 math.radians x 度轉...

Python中的optparse模組的使用

optparse模組主要用來為指令碼傳遞命令引數,採用預先定義好的選項來解析命令列引數。例項化乙個 optionparser 物件 可以帶參,也可以不帶引數 帶參的話會把引數變數的內容作為幫助資訊輸出。示例 import optparse usage python prog h p p 用於顯示幫助...

python模組使用 python的模組使用

模組是python組織 的基本方式。乙個python指令碼可以單獨執行,也可以匯入到另乙個指令碼中執行,當指令碼被匯入執行時,我們將其稱為模組 module 所有的.py檔案都可以作為乙個模組匯入 模組名與指令碼的檔名相同 例如我們編寫了乙個名為hello.py的指令碼,則可以在另乙個指令碼中用im...