久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

Python sqrt()函數用法說明

瀏覽:4日期: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 編程
相關文章:
主站蜘蛛池模板: 桂东县| 凤庆县| 宁城县| 内黄县| 黑水县| 衡水市| 油尖旺区| 龙陵县| 公安县| 台州市| 晋宁县| 广南县| 毕节市| 宁化县| 稻城县| 乌兰察布市| 汉川市| 永州市| 怀化市| 平安县| 烟台市| 卫辉市| 靖安县| 新化县| 建瓯市| 邻水| 黄骅市| 岢岚县| 濮阳市| 林甸县| 建始县| 延长县| 上思县| 东乡族自治县| 班玛县| 昂仁县| 江津市| 镇江市| 武邑县| 大方县| 高陵县|