如何在Python中写平方根
Python有一个预定义的 sqrt() 函数,用于返回一个数的平方根。它定义了一个值的平方根,即与其自身相乘得到一个数。不直接使用sqrt()函数来找到给定数的平方根,所以我们需要使用 math 模块在Python中调用sqrt()函数。
例如,144的平方根是12。
现在,让我们来看一下在Python中找到给定数的平方根的函数的语法:
语法:
math.sqrt(x)
参数:
x: 它是一个数字,数字应该大于0,可以是一个小数或整数。
返回值:
输出是平方根的值。
注意:
- sqrt()方法的输出将是一个浮点数值。
- 如果给定的输入是一个负数,那么输出将是一个ValueError。由于任何负数的平方根值都不被认为是一个实数,因此返回ValueError。
- 如果输入不是一个数字,那么sqrt()函数返回NaN。
示例:
在Python中使用sqrt()函数的示例用法。
代码:
import math
x = 16
y = math.sqrt(x)
print(y)
输出:
4.0
1. 使用math.sqrt()方法
sqrt()函数是一个内置函数,用于返回任意数的平方根。以下是找到一个数的平方根的步骤:
- 启动程序
- 定义要找到平方根的任意数
- 调用sqrt()函数并传递在步骤2中定义的值,并将结果存储在一个变量中
- 打印平方根
- 终止程序
Python math.sqrt()方法示例1
Python示例程序,用于找到给定整数的平方根。
代码
# import math module
import math
# define the integer value to the variable num1
num1 = 36
# use math.sqrt() function and pass the variable.
result = math.sqrt(num1)
# to print the square root of a given number n
print(" Square root of number 36 is : ", result)
# define the value
num2 = 625
result = math.sqrt(num2)
print(" Square root of value 625 is : ", result)
# define the value
num3 = 144
result = math.sqrt(num3)
print(" Square root of number 144 is : ", result)
# define the value
num4 = 64
result = math.sqrt(num4)
print(" Square root of number 64 is : ", result)
输出:
Square root of number 36 is : 6.0
Square root of number 625 is : 25.0
Square root of number 144 is : 12.0
Square root of number 64 is : 8.0
Python math.sqrt()方法示例2
让我们创建一个查找十进制数平方根的Python程序。
代码
# Import the math module
import math
# Calculate the square root of decimal numbers
print(" The square root of 4.5 is ", math.sqrt(4.5)) # Pass the decimal number
print(" The square root of 627 is ", math.sqrt(627)) # Pass the decimal number
print(" The square root of 6.25 is ", math.sqrt(6.25)) # Pass the decimal number
# Calculate the square root of 0
print(" The Square root of 0 is ", math.sqrt(0)) # Pass number as 0
输出:
The Square root of 4.5 is 2.1213203435596424
The Square root of 627 is 25.039968051096054
The Square root of 6.25 is 2.5
The Square root of 0 is 0.0
Python math.sqrt()方法示例3
在下面的程序中,我们从用户读取一个数字并找到其平方根。
代码
# import math module
import math
a = int(input(" Enter a number to get the Square root "))
# Use math.sqrt() function and pass the variable a.
result = math.sqrt(a)
print(" Square root of the number is ", result)
输出:
Enter a number to get the Square root: 25
Square root of the number is: 5.0
1. 使用math.pow()函数
pow()是Python中的内置函数,用于返回一个数的幂。它有两个参数。第一个参数定义了数值,第二个参数定义了该数值的幂次。
Python math.pow()方法示例
让我们看一个math.pow()函数的示例程序:
代码
# import the math module
import math
# take an input from the user
num = float(input(" Enter the number : "))
# Use the math.pow() function and pass the value and 0.5 (which is equal to ?) as an parameters
SquareRoot = math.pow( num, 0.5 )
print(" The Square Root of the given number {0} = {1}" .format( num, SquareRoot ))
输出:
Enter the number :628
The Square Root of the given number 628.0 = 25.059928172283335
3.使用Numpy模块
NumPy模块也是在python中找到平方根的一个选项。
Python Numpy示例
让我们看一个示例程序,用于在数组中找到给定列表中的数字的平方根。
代码
# import NumPy module
import numpy as np
# define an array of numbers
array_nums = np.array([ 1, 4, 9, 16, 25 ])
# use np.sqrt() function and pass the array
result = np.sqrt(array_nums)
print(" Square roots of the given array are: ", result)
输出:
Square roots of the given array are: [ 1. 2. 3. 4. 5. ]
4. 使用**运算符
我们可以使用指数运算符来找到一个数的平方根。该运算符可以应用于两个操作数之间。例如,x**y表示左操作数的y次幂。
以下是找出一个数的平方根的步骤。
步骤1. 定义一个函数并将值作为参数传递。
步骤2. 如果定义的数小于0或为负数,则返回空。
步骤3. 使用指数**符号来计算一个数的幂。
步骤4. 从用户处获取数值。
步骤5. 调用函数并将其输出存储到一个变量中。
步骤6. 在Python中显示一个数的平方根。
步骤7. 退出程序。
Python **运算符示例 1
让我们在Python程序中实现上述步骤并计算一个数的平方根。
代码
# import the math package or module
import math
# define the sqrt_fun() and pass the num as an argument
def sqrt_fun(num):
if num < 0: # if num is less than 0 or negative, it returns nothing
return
else:
return num ** 0.5 # Use the exponent operator
num = int(input (" Enter a numeric value: ") ) # take an input from the user
# call the sqrt_fun() to find the result
res = sqrt_fun(num)
# to print the Square Root of the variable
print(" Square Root of the {0} = {1} ".format(num, res))
输出:
Enter a numeric value: 256
Square Root of the 256 = 16.0
解释:
从上面的示例中可以看出,首先我们从用户那里获取一个输入(数字),然后使用指数**操作符来找到一个数字的乘方。其中0.5等于√(根号)来提高给定数字的乘方。最后,代码使用format()函数打印出num的值和比较的平方根值。如果用户输入一个负数,函数将不会返回任何东西,结果将为空白。
Python ** 操作符示例2
让我们创建一个Python程序,找到指定范围内的平方根。在下面的程序中,我们找到了0到30之间所有数字的平方根。
代码
# Import math module
import math
# Iterate through numbers from 0 to 29 and print their square roots
for i in range(30):
# Use the format() method to insert the values of i and its square root into the string
print(" Square root of a number {0} = {1} ".format( i, math.sqrt(i)))
输出:
Square root of a number 0 = 0.0
Square root of a number 1 = 1.0
Square root of a number 2 = 1.4142135623730951
Square root of a number 3 = 1.7320508075688772
Square root of a number 4 = 2.0
Square root of a number 5 = 2.23606797749979
Square root of a number 6 = 2.449489742783178
Square root of a number 7 = 2.6457513110645907
Square root of a number 8 = 2.8284271247461903
Square root of a number 9 = 3.0
Square root of a number 10 = 3.1622776601683795
Square root of a number 11 = 3.3166247903554
Square root of a number 12 = 3.4641016151377544
Square root of a number 13 = 3.605551275463989
Square root of a number 14 = 3.7416573867739413
Square root of a number 15 = 3.872983346207417
Square root of a number 16 = 4.0
Square root of a number 17 = 4.123105625617661
Square root of a number 18 = 4.242640687119285
Square root of a number 19 = 4.358898943540674
Square root of a number 20 = 4.47213595499958
Square root of a number 21 = 4.58257569495584
Square root of a number 22 = 4.69041575982343
Square root of a number 23 = 4.795831523312719
Square root of a number 24 = 4.898979485566356
Square root of a number 25 = 5.0
Square root of a number 26 = 5.0990195135927845
Square root of a number 27 = 5.196152422706632
Square root of a number 28 = 5.291502622129181
Square root of a number 29 = 5.385164807134504
Square root of a number 30 = 5.477225575051661
结论:
总之,在Python中追踪给定数字的平方根值有多种方法。我们可以根据我们的需求使用与数字相关的math模块、**运算符、pow()方法或NumPy模块。