如何在Python中查詢指數值?


在本文中,我們將向您展示如何在Python中查詢指數值。以下是完成此任務的各種方法:

  • 使用指數運算子(**)

  • 使用exp()函式

  • 使用內建數字中的exp()值

  • 使用pow()函式

使用指數運算子(**)

可以使用**指數運算子(**)** 來計算數字的冪。

由於不需要匯入模組或呼叫函式,因此這是最方便使用的方法。

x的**指數值**是**e的x次冪**,e是尤拉常數,是一個無理數,稱為尤拉數,等於**2.718281**。

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟:

  • 使用import關鍵字匯入**math**模組。

  • 獲取數字的指數值,例如這裡的e的2次冪(其中e=尤拉常數,即2.718281),使用指數運算子(**)。在Python中,我們使用**math.e**獲取e的值。

示例

下面的程式使用**指數運算子(**)** 返回數字的指數值。

# importing math module import math # getting the exponential value of e power 2 # (where e= Euler's constant i.e 2.718281) print("Exponential value of e power 2(e^2):", math.e**2)

輸出

執行上述程式後,將生成以下輸出:

Exponential value of e power 2(e^2): 7.3890560989306495

使用exp()函式

math模組的**exp()**函式用於計算**e的冪**,即**e^x**或x的指數。e的值大約為**2.71828…**。

語法

math.exp(x)

引數

  • **x(必需)** - 任何正數或負數。如果x的值不是數字,則會丟擲錯誤。

**返回值** - 計算e^x並返回一個浮點數。

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟:

  • 匯入Math模組

  • 使用math模組的**exp()**函式獲取作為引數傳遞的正數的指數值,例如這裡的e的2次冪(其中e=尤拉常數,即2.718281)。

  • 列印輸出。

示例

下面的程式使用exp()函式返回正數的指數值:

# importing math module import math # getting the exponential value of e power 2 using the exp() function # (where e= Euler's constant i.e 2.718281) print("Exponential value of e power 2(e^2):", math.exp(2))

輸出

執行上述程式後,將生成以下輸出:

Exponential value of e power 2(e^2): 7.38905609893065

對於負數和浮點數

示例

下面的程式使用**exp()**函式返回負數和浮點數的指數值:

# importing math module import math # getting the exponential value of a negative number using exp() print("Exponential value of e power -2(e^-2):", math.exp(-2)) # getting the exponential value of a floating-point number print("Exponential value of e power 1.5(e^1.5):", math.exp(1.5)) # getting the exponential value of 0 print("Exponential value of e power 0(e^0):", math.exp(0))

輸出

執行上述程式後,將生成以下輸出:

Exponential value of e power -2(e^-2): 0.1353352832366127
Exponential value of e power 1.5(e^1.5): 4.4816890703380645
Exponential value of e power 0(e^0): 1.0

使用內建數字中的exp()值

示例

下面的程式使用exp()函式返回內建數字(如**math.pi**、**math.e**)的指數值:

# importing math module import math # getting the exponential value of inbuilt number math.pi # using exp() function print ("Exponential value of inbuilt number math.pi:", math.exp(math.pi)) # getting the exponential value of inbuilt number math.e print ("Exponential value of inbuilt number math.e:", math.exp(math.e))

輸出

執行上述程式後,將生成以下輸出:

Exponential value of inbuilt number math.pi: 23.140692632779267
Exponential value of inbuilt number math.e: 15.154262241479262

使用pow()函式

在Python中,**pow()**函式計算任何正整數的冪。它返回x的y次冪 (x^y) 的值。

語法

pow(x,y)

引數

  • **x** - 數值(**底數**)

  • **y** - 數值的冪(**指數**)

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟:

  • 建立一個變數來儲存輸入的底數值,這裡為**e**(其中e=尤拉常數,即**2.718281**)。使用**math.e**獲取e的值。

  • 建立另一個變數來儲存指數值。

  • 使用**pow()**函式透過將輸入的底數和指數值作為引數傳遞給它來列印數字的冪,即**底數^指數**,並列印結果冪。

示例

下面的程式使用pow()函式返回輸入數字的指數值:

# importing math module import math # input base value which is e (where e= Euler's constant i.e 2.718281) inputBase = math.e # input exponent value inputExponent = 3 # printing the resultant power value using the pow() function print("Value of e raised to the power 3(e^3)=", pow(inputBase, inputExponent))

輸出

執行上述程式後,將生成以下輸出:

Value of e raised to the power 3(e^3)= 20.085536923187664

結論

在本教程中,我們學習瞭如何使用多種方法在Python中查詢指數數。我們還研究了exp()函式如何與各種型別的數字一起工作。

更新於:2022年11月9日

9000+ 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.