如何在 Python 中計算一個數的平方根?
在本文中,我們將向您展示如何在 Python 中計算一個數的平方根。以下是完成此任務的各種方法:
- 使用 sqrt() 計算平方根
- 使用 pow() 函式計算平方根
- 使用指數運算子計算平方根
- 使用 cmath 模組計算平方根
什麼是數的平方根?
一個數的平方根是指一個值,當它自身相乘時會返回相同的數。
示例:5 x 5 = 25,因此 25 的平方根是 5。但是 -5 x -5 也等於 25,所以 -5 也是 25 的平方根。
使用 sqrt() 計算平方根
使用 Python 庫的 math 模組中定義的 sqrt() 函式是計算一個數的平方根最簡單的方法。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
- 使用 import 關鍵字匯入 math 模組。
- 建立一個變數來儲存輸入數字。
- 使用 math 模組的 sqrt() 函式透過將數字作為引數傳遞給它來獲取輸入數字的平方根。
- 列印輸入數字的平方根結果。
示例
以下程式使用 math.sqrt() 函式返回一個數的平方根:
# importing math module import math # input number inputNumber = 25 # getting the square root of an input number squareRoot = math.sqrt(inputNumber) # printing the square root of a number print ("Square root of",inputNumber,"is: ", squareRoot)
輸出
執行以上程式後,將生成以下輸出:
Square root of 25 is: 5.0
使用 pow() 函式計算平方根
在 Python 中,可以使用 pow() 函式快速確定平方根。
它返回 x 的 y 次冪(x^y)。
語法
pow(x,y)
引數
- x - 它是數值(底數)
- y - 它是數值的冪(指數)
演算法(步驟)
以下是執行所需任務的演算法/步驟:
- 使用 math 模組的 pow() 函式透過將數字(底數)和冪(指數)作為引數傳遞給它來獲取輸入數字的平方根。這裡 0.5 是表示平方根的指數值。
- 列印輸入數字的平方根結果。
示例
以下程式使用 pow() 函式返回一個數的平方根:
# importing math module import math # input number inputNumber = 25 # getting the square root of an input number # here 0.5 is the exponent value which represents the square root squareRoot = math.pow(inputNumber, 0.5) # printing the square root of a number print ("Square root of",inputNumber,"is: ", squareRoot)
輸出
執行以上程式後,將生成以下輸出:
Square root of 25 is: 5.0
使用指數運算子計算平方根
與 pow() 函式類似,指數運算子(用 ** 表示)執行平方根運算。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 def 關鍵字建立一個函式 squareRoot(),它返回傳遞給它的數字的平方根。
使用 if 條件語句檢查數字是否小於 0(負數)。
如果條件為真,即數字小於 0,則使用 return 語句不返回任何值。
否則,使用指數運算子 (**)返回數字的平方根。
建立一個變數來儲存輸入數字。
透過將輸入數字作為引數傳遞給上面建立的 squareRoot() 函式,並列印數字的平方根結果。
示例
以下程式使用指數運算子 (**) 返回一個數的平方根。
# creating a function that returns the square root of a number passed to it def squareRoot(num): # returning nothing if the number is less than 0(negative number) if num < 0: return else: # else returning the square root of a number using the exponential operator(**) return num**0.5 # input number inputNumber = 16 # calling the squareRoot() function by passing the input number as an argument print ("Square root of",inputNumber,"is: ", squareRoot(inputNumber))
輸出
執行以上程式後,將生成以下輸出:
Square root of 16 is: 4.0
使用 cmath 模組計算平方根
在 Python 中,可以使用 cmath 模組來確定實數或複數的平方根。
對於所有正實數,我們到目前為止採用的各種方法都能正常工作。但對於負數或複數,cmath 模組似乎很有用。
cmath.sqrt() 也用於獲取負數的平方根。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
- 使用 import 關鍵字匯入 cmath 模組。
- 建立一個變數來儲存輸入複數。
- 使用 cmath 模組的 sqrt() 函式透過將數字作為引數傳遞給它來獲取複數的平方根。
- 列印輸入複數的平方根
示例
以下程式使用 cmath.sqrt() 函式返回複數的平方根:
# importing cmath module import cmath # input complex number complexNumber = 2 + 3j # getting the square root of a complex number using cmath.sqrt() function squareRoot = cmath.sqrt(complexNumber) # printing the square root of an input complex number print ("Square root of",complexNumber,"is: ", squareRoot)
輸出
執行以上程式後,將生成以下輸出:
Square root of (2+3j) is: (1.6741492280355401+0.8959774761298381j)
注意:您還可以使用 eval() 函式將輸入轉換為複數。
對於負數
示例
# importing cmath module import cmath # input complex number(negative number) complexNumber = -16 # getting the square root of a complex number using cmath.sqrt() function squareRoot = cmath.sqrt(complexNumber) # printing the square root of an input complex number print ("Square root of",complexNumber,"is: ", squareRoot)
輸出
執行以上程式後,將生成以下輸出:
Square root of -16 is: 4j
結論
在本文中,我們學習了四種不同的 Python 方法來計算給定數字的平方根。我們還學習瞭如何使用 cmath 模組來計算負數的平方根。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP