最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python sqrt()函數(shù)用法說明

 更新時間:2021年03月11日 09:44:42   作者:東師小鎮(zhèn)  
這篇文章主要介紹了Python sqrt()函數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1、開平方

函數(shù) sqrt() 返回 x 的平方根(x > 0)

語法:

import math
math.sqrt( x )

注意:此函數(shù)不可直接訪問,需要導(dǎo)入math模塊,然后需要使用math靜態(tài)對象調(diào)用此函數(shù)。

參數(shù) x -- 數(shù)值表達(dá)式

返回結(jié)果是浮點(diǎn)數(shù)。

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)
 
# 輸出結(jié)果
math.sqrt(100) : 10.0 # 浮點(diǎn)
math.sqrt(7) : 2.64575131106
math.sqrt(math.pi) : 1.77245385091

實(shí)例1.

請利用filter()過濾出1~100中平方根是整數(shù)的數(shù),即結(jié)果應(yīng)該是:

[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))

實(shí)例2.

def find_next_square(sq):
 import math
 n = math.sqrt(sq)
 if int(sq) == int(n) * int(n): #此處解決了(整數(shù)與浮點(diǎn)數(shù)的問題)
 return (int((n+1)*(n+1)))
 else:
 return -1
print(find_next_square(4.0))
 
#輸出結(jié)果

2、開n次方

利用pow(a, b)函數(shù)即可。需要開 a 的 r 次方則pow(a, 1.0/ r )。

補(bǔ)充:python中sqrt函數(shù)用法_sqrt是什么函數(shù)

sqrt是什么函數(shù)?

sqrt()是用于計(jì)算數(shù)字x的平方根的函數(shù)。

語法

以下是 sqrt() 方法的語法:import math

math.sqrt( x )

注意:sqrt()是不能直接訪問的,需要導(dǎo)入 math 模塊,通過靜態(tài)對象調(diào)用該方法。

參數(shù)

x -- 數(shù)值表達(dá)式。

返回值

返回?cái)?shù)字x的平方根。

實(shí)例

以下展示了使用 sqrt() 方法的實(shí)例:

#!/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)

以上實(shí)例運(yùn)行后輸出結(jié)果為:

math.sqrt(100) : 10.0
math.sqrt(7) : 2.64575131106
math.sqrt(math.pi) : 1.77245385091

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論

定陶县| 原平市| 长宁区| 渭南市| 黄浦区| 郸城县| 吉首市| 自贡市| 富源县| 嘉义县| 尖扎县| 屏东县| 南岸区| 焦作市| 鄄城县| 江源县| 邵武市| 怀远县| 康平县| 嫩江县| 新竹市| 白水县| 体育| 南丰县| 苏尼特右旗| 绥江县| 阳朔县| 顺昌县| 岳普湖县| 海伦市| 贵州省| 宿松县| 久治县| 安义县| 民和| 海口市| 阿勒泰市| 繁昌县| 通山县| 民勤县| 福州市|