博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python中平方根_如何在Python中找到平方根
阅读量:2521 次
发布时间:2019-05-11

本文共 2717 字,大约阅读时间需要 9 分钟。

python中平方根

Hello everyone, in this tutorial, I’ll show 3 ways to find square root in Python.

大家好,在本教程中,我将展示3种在Python中找到平方根的方法。

Let’s see how to do it.

让我们来看看如何做。

1. Using Exponent

1.使用指数

number = int(input("enter a number: "))sqrt = number ** 0.5print("square root:", sqrt)

Output:

输出:

enter a number: 64 square root: 8.0

输入数字:64 平方根:8.0

In above program, first we’re taking a number from user as input and the entered value will be converted to int from string and will store into variable called number. Then we are using  exponent (**) sign, which is used to find out the power of a number. But we know that if a number have power ½ or 0.5 then it will represent to the square root of that number. That’s why we are using 0.5 as power here. So the number**0.5 will give us square root of that number and will be stored in variable named as sqrt. In last line we’re just printing the square root of the number.

在上面的程序中,首先我们从用户那里得到一个数字作为输入,输入的值将从字符串转换为int并将存储到名为number的变量中 然后,我们使用指数(**)符号,该符号用于找出数字的幂。 但是我们知道,如果一个数字的幂为½或0.5,那么它将代表该数字的平方根。 这就是为什么我们在这里使用0.5作为幂。 因此, 数字** 0.5将为我们提供该数字的平方根,并将其存储在名为sqrt的变量中 在最后一行中,我们只是打印数字的平方根。

2. Using math.sqrt() Method

2.使用math.sqrt()方法

import mathnumber = int(input("enter a number:"))sqrt = math.sqrt(number)print("square root:" , sqrt)

sqrt() is the predefined method used to find square root in python. But we have to import math module to use the sqrt() method. In first line, we’re importing math module, then in next line, taking input from user. After that we’re finding the square root of the number using sqrt() method and result will be stored into sqrt variable. In last line, just printing the square root as in first program.

sqrt()是用于在python中查找平方根的预定义方法。 但是我们必须导入数学模块才能使用sqrt()方法。 在第一行中,我们要导入数学模块,然后在下一行中,从用户那里获取输入。 之后,我们使用sqrt()方法找到数字的平方根 ,结果将存储到sqrt变量中。 在最后一行,只需像在第一个程序中一样打印平方根。

3. Using math.pow() Method

3.使用math.pow()方法

import mathnumber = int(input("enter a number:"))sqrt = math.pow(number, 0.5)print("square root: ", sqrt)

pow() is also a predefined method to find out power of a number, it takes two arguments as input, first is the number itself and second one is power of that number. The program is same as first program where we’re using (**) sign to find out the square root but only different is this that here we’re using a predefined method pow() instead of  (**) sign to get the power of that number.

pow()也是一种预定义方法,用于找出数字的幂,它以两个参数作为输入,第一个是数字本身,第二个是该数字的幂。 该程序与第一个程序相同,在第一个程序中我们使用(**)符号来找出平方根,但是不同之处在于,这里我们使用预定义的方法pow()而不是(**)符号来获取平方根。该数字的力量。

If you’ve any problem related with article or have any other possible way to find square root in python then please let us know in comments.

如果您有任何与文章相关的问题,或者有其他可能的方法可以在python中找到平方根,请在评论中告知我们。

翻译自:

python中平方根

转载地址:http://xaggb.baihongyu.com/

你可能感兴趣的文章
UDP编程
查看>>
ecos中断机制分析(1)
查看>>
Plus One
查看>>
11.4 iftop:动态显示网络接口流量信息
查看>>
jQuery API中文文档
查看>>
【架构】MVC模式
查看>>
事件(三)
查看>>
迟到 的2018年终总结
查看>>
学习委托
查看>>
Spring之旅
查看>>
Tree
查看>>
Java学习之i/o系统
查看>>
laravel 视图组件
查看>>
[转发]Oauth 1.0 1.0a 和 2.0 的之间的区别有哪些?
查看>>
animation
查看>>
mysqldump的几个主要选项探究
查看>>
程序员修炼之路
查看>>
动态规划的基本模型
查看>>
php生成xml文件方法
查看>>
.Net邮箱的使用
查看>>