国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python sqrt()函數用法說明

瀏覽:2日期:2022-06-25 14:05:47
1、開平方

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

語法:

import mathmath.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.64575131106math.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 -1print(find_next_square(4.0)) #輸出結果2、開n次方

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

補充:python中sqrt函數用法_sqrt是什么函數

sqrt是什么函數?

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

語法

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

math.sqrt( x )

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

參數

x -- 數值表達式。

返回值

返回數字x的平方根。

實例

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

#!/usr/bin/pythonimport math # This will import math moduleprint '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.0math.sqrt(7) : 2.64575131106math.sqrt(math.pi) : 1.77245385091

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 茂名市| 通许县| 天柱县| 巧家县| 木里| 新竹市| 剑阁县| 宝丰县| 华宁县| 嘉善县| 平远县| 奎屯市| 福海县| 德昌县| 临江市| 布尔津县| 绥芬河市| 怀柔区| 鄱阳县| 天峨县| 东山县| 织金县| 长葛市| 和林格尔县| 永清县| 沙雅县| 轮台县| 永德县| 祁阳县| 濉溪县| 荥经县| 济阳县| 永城市| 林芝县| 卓尼县| 上蔡县| 涡阳县| 临汾市| 阳江市| 陆丰市| 临沂市|