如何用 Python 求平方根?

Python 有一个预定义的 sqrt()函数,返回一个数字的平方根。它定义了一个值的平方根,这个值将自身相乘得到一个数。 sqrt()函数不是直接用来求给定数的平方根的,所以我们需要使用一个数学模块来调用 Python 中的 sqrt()函数。

例如,144 的平方根是 12。

使用 math.sqrt()方法

sqrt()函数是一个内置函数,它返回任何数字的平方根。以下是求一个数的平方根的步骤。

  1. 启动程序
  2. 定义任何要求平方根的数。
  3. 调用 sqrt()函数,传递您在步骤 2 中定义的值,并将结果存储在变量中。
  4. 打印平方根。
  5. 终止程序。

让我们创建一个 Python 程序来查找一个数字的平方根。

sqro ot . py


import math # import math module
N = 25 # define the value to the variable N 
result = math.sqrt(N) # use math.sqrt() function and pass the variable.
print(" Square root of 25 is :", result) # prints the square root of a given number 
M = 625 # define the value
result = math.sqrt(M) # use math.sqrt() function and pass the variable
print(" Square root of 625 is :", result) # prints the square root of a given number 

P = 144 # define the value
result = math.sqrt(P) # use math.sqrt() function and pass the variable
print(" Square root of 144 is :", result) # prints the square root of a given number 

S = 64 # define the value
result = math.sqrt(S) # use math.sqrt() function and pass the variable
print(" Square root of 64 is :", result) # prints the square root of a given number

输出:

让我们创建一个 python 程序来查找十进制数的平方根。

sqro ot . py


import math
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

print(" The Square root of 0 is", math.sqrt(0)) # Pass number as 0

输出:

在下面的程序中,我们已经从用户那里读取了一个数字,并找到了平方根。

SqRoot_Usr.py


import math # import math module
a = int(input("Enter a number to get the Square root")) # take an input 
res = math.sqrt(a) # Use math.sqrt() function and pass the variable a.
print("Square root of the number is", res) # print the Square Root

输出:

使用 math.pow()函数

幂()是 Python 中用来返回一个数的幂的内置函数。它有两个参数。第一个参数定义了数字,第二个参数定义了该数字的功率提升。

Pow_Sqrt.py


import math # import the math module
num = float(input("Enter the number :")) # take an input
SquareRoot = math.pow(num, 0.5) # Use the math.pow() function and pass the value and 0.5 (which is equal to √) as an parameters
print(" The Square Root of the given number {0} = {1}" .format(num, SquareRoot)) # print the Square Root.

输出:

使用**运算符

我们也可以用指数算符来求这个数的平方根。运算符可以应用于两个操作数之间。例如,x**y .表示左操作数升到右的幂。

以下是求一个数的平方根的步骤。

第一步。定义一个函数,并将该值作为参数传递。

第二步。如果定义的数字小于 0 或为负数,则不返回任何内容。

第三步。用指数**符号求一个数的幂。

第四步。从用户处获取数值。

第五步。调用函数并将其输出存储到变量中。

第六步。在 Python 中显示数字的平方根。

第七步。退出程序。

让我们在 Python 程序中实现上述步骤,并计算一个数字的平方根。

SqrtFun.py


import math # import the math package or module
def sqrt_fun(num): # define the sqrt_fun() and pass the num as an argument
    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

res = sqrt_fun(num) # call the sqrt_fun() to find the result
print("  Square Root of the {0} = {1}".format(num, res)) # print the Square Root of the variable

输出:

正如我们在上面的例子中所看到的,首先我们从用户那里获取一个输入(数字),然后使用指数**运算符找出一个数字的幂。其中 0.5 等于√(根符号)以提高给定数字的幂。

让我们创建一个 Python 程序,它可以找到指定范围之间的平方根。在下面的程序中,我们已经找到了 0 到 50 之间所有数字的平方根。

sqrlop . py


import math
for i in range(50):
    print("Square root of a number {0} = {1}".format(i,math.sqrt(i)))

输出:


原创文章,作者:U0RVB,如若转载,请注明出处:https://www.506064.com/n/128543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
U0RVBU0RVB
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相关推荐

  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29

发表回复

登录后才能评论