我是靠谱客的博主 震动海燕,这篇文章主要介绍Python求算数平方根和约数的方法汇总,现在分享给大家,希望可以做个参考。

一、求算术平方根

复制代码
1
2
3
4
5
6
7
8
9
10
11
a= x=int(raw_input('Enter a number:')) if x >= : while a*a < x: a = a + if a*a != x: print x,'is not a perfect square' else: print a else: print x,'is a negative number'

二、求约数

方法一:

复制代码
1
2
3
4
5
6
7
8
divisor = [ ] x=int(raw_input('Enter a number:')) i= while i<=x: if x%i ==: divisor.append(i) i = i + print 'divisor:',divisor

方法二:

复制代码
1
2
3
4
5
6
divisor = [ ] x=int(raw_input('Enter a number:')) for i in range(,x+): if x%i ==: divisor.append(i) # 此行也可以换成 divisor = divisor + [i] print 'divisor:',divisor

下面给大家介绍下Python sqrt() 函数

描述

sqrt() 方法返回数字x的平方根。

语法

以下是 sqrt() 方法的语法:

复制代码
1
2
import math math.sqrt( x )

注意:sqrt()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。

参数

x -- 数值表达式。

返回值

返回数字x的平方根。

实例

以下展示了使用 sqrt() 方法的实例:

复制代码
1
2
3
4
5
#!/usr/bin/python import math # This will import math module print "math.sqrt(100) : ", math.sqrt(100) print "math.sqrt(7) : ", math.sqrt(7) print "math.sqrt(math.pi) : ", math.sqrt(math.pi)

以上实例运行后输出结果为:

复制代码
1
2
3
math.sqrt(100) : 10.0 math.sqrt(7) : 2.64575131106 math.sqrt(math.pi) : 1.77245385091

最后

以上就是震动海燕最近收集整理的关于Python求算数平方根和约数的方法汇总的全部内容,更多相关Python求算数平方根和约数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(180)

评论列表共有 0 条评论

立即
投稿
返回
顶部