SciPy - unit() 方法



SciPy unit() 方法用於透過詞典從物理常量中訪問特定單位。此方法包含值和單位。它定義在 scipy.constants 模組中。此函式的主要用途允許使用者計算準確的科學計算。

語法

以下是 SciPy unit() 方法的語法 −

unit(key)

引數

此方法僅接受一個引數 −

  • key:此引數優先作為指定物理常量名稱的字串。

返回值

此方法重新執行一個整數。

示例 1

以下是基本的 SciPy unit() 方法,它展示了物理常量(“真空中的光速”)的用法。

from scipy.constants import physical_constants

key = 'speed of light in vacuum'
unit = physical_constants[key][2]

print(f"The unit of {key} is {unit}.")

輸出

上述程式碼產生以下結果 −

The unit of speed of light in vacuum is 0.0.

示例 2

在這裡,我們使用物理常量的鍵為 “普朗克常數”,它有助於以單位形式顯示結果。

from scipy.constants import physical_constants

key = 'Planck constant'
unit = physical_constants[key][0]

print(f"The unit of {key} is {unit}.")

輸出

上述程式碼產生以下結果 −

The unit of Planck constant is 6.62607015e-34.
scipy_reference.htm
廣告
© . All rights reserved.