python中sqrt的用法_Python math sqrt()用法及代码示例

news/2024/7/24 10:21:39 标签: python中sqrt的用法

sqrt()function是Python编程语言中的内置函数,可返回任何数字的平方根。

用法:

math.sqrt(x)

参数:

x is any number such that x>=0

返回:

It returns the square root of the number

passed in the parameter.

# Python3 program to demonstrate the

# sqrt() method

# import the math module

import math

# print the square root of  0

print(math.sqrt(0))

# print the square root of 4

print(math.sqrt(4))

# print the square root of 3.5

print(math.sqrt(3.5))

输出:

0.0

2.0

1.8708286933869707

错误:当x <0时,由于运行时错误而无法执行。

# Python3 program to demonstrate the error in

# sqrt() method

# import the math module

import math

# print the error when x<0

print(math.sqrt(-1))

输出:

Traceback (most recent call last):

File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in

print(math.sqrt(-1))

ValueError:math domain error

实际应用:给定一个数字,检查其是否为质数。

方法:运行从2到sqrt(n)的循环,并检查范围(2-sqrt(n))中是否有任何数字除以n。

# Python program for practical application of sqrt() function

# import math module

import math

# function to check if prime or not

def check(n):

if n == 1:

return False

# from 1 to sqrt(n)

for x in range(2, (int)(math.sqrt(n))+1):

if n % x == 0:

return False

return True

# driver code

n = 23

if check(n):

print("prime")

else:

print("not prime")

输出:

prime


http://www.niftyadmin.cn/n/1355255.html

相关文章

JS的数组遍历方法

JS的数组遍历方法 常用的有find、findIndex、filter、forEach、map、every、some 首先定义一个实验用的数组&#xff1a; let arr[{x:1},{x:2},{x:3}]1. find arr.find(item>item.x3) arr.find(item>item.x4)数组成员依次执行回调函数&#xff0c;直到找出第一个返回值…

中choice函数_fairseq源码系列(4):Bert中掩码的实现

介绍如果你看Bert的论文&#xff0c;会写Mask 15% of all tokens80% of the time, replace with [mask] token&#xff0c; 10% of the time, replace with random token, 10% of the time, keep the word unchangedpytorch 1.2已经自带transformers 模块了&#xff0c;如果自己…

JS树的遍历:广度优先遍历与深度优先遍历

JS树的遍历&#xff1a;广度优先遍历与深度优先遍历 先定义一颗简单的树&#xff1a; let tree [{label:a,children:[{label:b,children:[{label:d},{label:e}]},{label:c,children:[{label:f}]}]} ]树的广度优先遍历 广度优先遍历&#xff1a;从上往下对每一层依次访问&…

php 论坛_杜蕾斯发文庆祝PHP语言25周年,文案一直被模仿,从未被超越

来自&#xff1a;快科技 http://www.kkj.cn1995年6月8日&#xff0c;PHP 1.0版本正式问世&#xff0c;目前已经更迭到7.4版本&#xff0c;PHP 8.0 Alpha1版本也有望于今年6月中旬发布。25周年之际&#xff0c;杜蕾斯官方微博发文庆祝&#xff1a;“#PHP语言25周年#不管PHP是不是…

如何轻松检查dom、查看dom层级

如何轻松检查dom、查看dom层级 效果&#xff1a; 像这样&#xff0c;每一层dom都以不同的颜色被标出。方便查看。 实现&#xff1a; * { background-color: rgba(255,0,0,.2)!important; } * * { background-color: rgba(255,0,255,.2)!important; } * * * { background-c…

base64转文件_文件和文件格式知多少

相关知识点1、了解base64和blob2、图像的数据类型3、了解httpUrl、dataUrl、objectUrl(blobUrl)4、了解Blob、File、BlobURL、DataURL之间的关系以及互相转换5、如何获取Blob数据和File数据知识点介绍一、了解base64和blob1、base64Base64就是一种 基于64个可打印字符来表示二进…

js递归解耦(arguments.callee的使用)

js递归解耦&#xff08;arguments.callee的使用&#xff09; 起因&#xff1a; 递归时我们通常这么写&#xff1a; function recursion(num) {if(num>50){return num}else{return recursion(num 1);} }方法内必须保证方法名为‘recursion’&#xff0c;从而会导致紧密耦合…

模板类的析构函数如何写_如何写好领英LinkedIn的个人小结(附带模板)

许多人因为不知道如何写Summary于是干脆就不写&#xff0c;这个错过绝佳展示自己的舞台和做SEO的机会。Summary允许你写2000个字符&#xff0c;建议第一人称书写&#xff0c;这样看起来更加自然。大家可以通过一下几种模板写。第一种模板&#xff1a;直击客户痛点。作为一个外贸…