共計 555 個字符,預計需要花費 2 分鐘才能閱讀完成。
Python 中的 math
庫是一個提供數學運算函數和常量的標準庫。要使用 math
庫,需要先導入它:
import math
然后就可以使用 math
庫中的函數和常量了。以下是一些常用的 math
庫函數和常量的示例:
math.sqrt(x)
:返回 x 的平方根。
import math
print(math.sqrt(16)) # 輸出:4.0
math.ceil(x)
:返回不小于 x 的最小整數。
import math
print(math.ceil(4.1)) # 輸出:5
math.floor(x)
:返回不大于 x 的最大整數。
import math
print(math.floor(4.9)) # 輸出:4
math.pow(x, y)
:返回 x 的 y 次冪。
import math
print(math.pow(2, 3)) # 輸出:8.0
math.pi
:常量,表示圓周率 π。
import math
print(math.pi) # 輸出:3.141592653589793
math.e
:常量,表示自然對數的底數 e。
import math
print(math.e) # 輸出:2.718281828459045
這只是一部分 math
庫提供的函數和常量,還有其他很多函數和常量可供使用。可以通過查閱 Python 官方文檔來了解更多詳細的使用方法和功能。
丸趣 TV 網 – 提供最優質的資源集合!
正文完