共計 801 個字符,預計需要花費 3 分鐘才能閱讀完成。
Python 中調用數學函數需要導入 math 模塊,然后使用該模塊提供的內置函數。
以下是一些常用的數學函數的調用方法:
- 平方根函數 – sqrt(x)
import math
result = math.sqrt(16)
print(result) # 輸出:4.0
- 絕對值函數 – fabs(x)
import math
result = math.fabs(-5)
print(result) # 輸出:5.0
- 向上取整函數 – ceil(x)
import math
result = math.ceil(3.7)
print(result) # 輸出:4
- 向下取整函數 – floor(x)
import math
result = math.floor(3.7)
print(result) # 輸出:3
- 四舍五入函數 – round(x)
import math
result = round(3.7)
print(result) # 輸出:4
- 冪函數 – pow(x, y)
import math
result = math.pow(2, 3)
print(result) # 輸出:8.0
- 自然對數函數 – log(x)
import math
result = math.log(10)
print(result) # 輸出:2.302585092994046
- 指數函數 – exp(x)
import math
result = math.exp(1)
print(result) # 輸出:2.718281828459045
- 正弦函數 – sin(x)
import math
result = math.sin(math.pi/2)
print(result) # 輸出:1.0
- 余弦函數 – cos(x)
import math
result = math.cos(math.pi)
print(result) # 輸出:-1.0
這些是常用的數學函數的調用方法,math 模塊還提供了其他數學函數,可以通過閱讀 Python 官方文檔或 math 模塊的文檔了解更多函數的用法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完