前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python中sqrt函数用法_Python : sqrt() 函数

python中sqrt函数用法_Python : sqrt() 函数

作者头像
全栈程序员站长
发布2022-09-05 14:33:24
8360
发布2022-09-05 14:33:24
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

开平方

函数 sqrt() 返回 x 的平方根(x > 0)

语法:

import math

math.sqrt( x )

注意: 此函数不可直接访问,需要导入math模块,然后需要使用math静态对象调用此函数。

参数 x — 数值表达式

返回结果是浮点数。

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)

# 输出结果

math.sqrt(100) : 10.0 # 浮点

math.sqrt(7) : 2.64575131106

math.sqrt(math.pi) : 1.77245385091

实例1. 请利用filter()过滤出1~100中平方根是整数的数,即结果应该是:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

import math

def is_sqr(x):

r = int(math.sqrt(x))

return r * r == x

print filter(is_sqr, range(1, 101))

实例2.

def find_next_square(sq):

import math

n = math.sqrt(sq)

if int(sq) == int(n) * int(n): #此处解决了(整数与浮点数的问题)

return (int((n+1)*(n+1)))

else:

return -1

print(find_next_square(4.0))

#输出结果

9

2. 开n次方

利用pow(a, b)函数即可。需要开 a 的 r 次方则pow(a, 1.0/ r )。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/136856.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档