python 平方根

There are many ways to find the square root of a number in Python.

有许多方法可以在Python中找到数字的平方根。

  • Exponent Operator指数算子
  • Math.sqrt() functionMath.sqrt()函数
  • Math.pow() functionMath.pow()函数

1.使用指数运算符计算数字的平方根 (1. Using Exponent Operator for Square Root of a Number)

num = input("Please enter a number:\n")sqrt = float(num) ** 0.5print(f'{num} square root is {sqrt}')

Output:

输出:

Please enter a number:
4.344
4.344 square root is 2.0842264752180846Please enter a number:
10
10 square root is 3.1622776601683795

I am using float() built-in function to convert the user-entered string to a floating-point number.

我正在使用float()内置函数将用户输入的字符串转换为浮点数。

The input() function is used to get the user input from the standard input.

input()函数用于从标准输入获取用户输入。

2.平方根的数学sqrt()函数 (2. Math sqrt() function for square root)

Python math module sqrt() function is the recommended approach to get the square root of a number.

建议使用Python数学模块 sqrt()函数来获取数字的平方根。

import mathnum = 10
num_sqrt = math.sqrt(num)print(f'{num} square root is {num_sqrt}')

Output:

输出:

Python Square Root Of Number

Python的数字平方根

3.平方根的数学pow()函数 (3. Math pow() function for square root)

It’s not a recommended approach. But, the square root of a number is the same as the power of 0.5.

这不是推荐的方法。 但是,数字的平方根等于0.5的幂。

>>> import math
>>>
>>> math.pow(10, 0.5)
3.1622776601683795
>>>

4.复数的平方根 (4. Square Root of Complex Number)

We can use cmath module to get the square root of a complex number.

我们可以使用cmath模块获取复数的平方根。

import cmathc = 1 + 2jc_sqrt = cmath.sqrt(c)print(f'{c} square root is {c_sqrt}')

Output:

输出:

(1+2j) square root is (1.272019649514069+0.7861513777574233j)

5.矩阵/多维数组的平方根 (5. Square Root of a Matrix / Multidimensional Array)

We can use NumPy sqrt() function to get the square root of a matrix elements.

我们可以使用NumPy sqrt()函数来获取矩阵元素的平方根。

翻译自: https://www.journaldev.com/32175/python-square-root-number

python 平方根

python 平方根_数的Python平方根相关推荐

  1. python 切片_全面解读Python高级特性切片

    大家好,欢迎来到Crossin的编程教室! 众所周知,我们可以通过索引值(或称下标)来查找序列类型(如字符串.列表.元组-)中的单个元素,那么,如果要获取一个索引区间的元素该怎么办呢? 切片(slic ...

  2. 学习python课程_想学习Python吗? 这是我们的免费4小时互动课程

    学习python课程 Python is a popular, versatile and easy-to-learn language. It's the go-to language for AI ...

  3. 2018年python薪资_最好的Python:2017和2018年至今我最喜欢的文章集

    2018年python薪资 我打算发布此收藏集 (My intention with publishing this collection) Last year I only used Medium ...

  4. python竞赛_浅谈Python在信息学竞赛中的运用及Python的基本用法

    浅谈Python在信息学竞赛中的运用及Python的基本用法 前言 众所周知,Python是一种非常实用的语言.但是由于其运算时的低效和解释型编译,在信息学竞赛中并不用于完成算法程序.但正如LRJ在& ...

  5. python 串口_如何使用Python开发串口通讯上位机(一)

    用Python开发串口通讯型上位机,其实并非最优解,本系列更新只为个人学习与总结.如果有C语言底子,嵌入式层面的上位机开发,C++ Builder,C#才是更加好用的利器. 1 什么是上位机 从事过嵌 ...

  6. 爬虫python入门_如何入门Python爬虫?爬虫原理及过程详解

    "入门"是良好的动机,但是可能作用缓慢.如果你手里或者脑子里有一个项目,那么实践起来你会被目标驱动,而不会像学习模块一样慢慢学习. 另外如果说知识体系里的每一个知识点是图里的点,依 ...

  7. python股市_如何使用python和破折号创建仪表板来主导股市

    python股市 始终关注大局 (Keep Your Eyes on the Big Picture) I've been fascinated with the stock market since ...

  8. python马尔可夫链_[译] 用 Python 实现马尔可夫链的初级教程

    马尔可夫链是通常用一组随机变量定义的数学系统,可以根据具体的概率规则进行状态转移.转移的集合满足 马尔可夫性质 ,也就是说,转移到任一特定状态的概率只取决于当前状态和所用时间,而与其之前的状态序列无关 ...

  9. python 堆排序_堆排序用python

    Python天天美味(30) - python数据结构与算法之快速排序 快速排序的原理是将取出第一个数,将整个数组分为两波,一拨都大于这个数,另一波都小于这个数,然后递归用同样的方法处理第一波数字和第 ...

最新文章

  1. Windows 2008 ×××与 CA
  2. python3 pymysql 查询结果转字典dict
  3. mysql+只读参数_mysql只读变量
  4. CodeForces - 1419E Decryption(质因子分解+构造)
  5. Node — 第九天 (ES6降级 and 发布属于自己的[第三方模块]包)
  6. 安装Ubunutu音频视频库
  7. RS 学习笔记 3-1
  8. 计算机桌面保护怎么办,电脑屏幕保护怎么设置
  9. oracle初级系列教程
  10. mysql 收藏表如何建立_代码收藏系列--mysql创建数据库、数据表、函数、存储过程命令...
  11. 排序算法—归并排序(思维导图思路整理)
  12. 基于HTML5功能强大的滑块幻灯片
  13. 华为海思芯片自立自强!
  14. 编程珠玑第一章习题解答
  15. django读取mysql事实数据_Django读取Mysql数据并显示在前端的实例
  16. c语言编码rna翻译,哪位大牛有哈夫曼编码的C语言源程序,麻烦帮帮忙啦!
  17. 人人5功盖世时候,我在支持国产
  18. chmod777到底是什么意思
  19. android声音编辑器,音频视频编辑器app下载-Audio Video Editorv1.1.0 安卓版-腾牛安卓网...
  20. 三相MMC并网逆变器/模块化多电平变换器/相间环流抑制/子模块电容电压均衡/载波移相调制

热门文章

  1. http的response遇到illegalstateexception解决办法
  2. 谷歌技术quot;三宝quot;之MapReduce
  3. 一步步学习微软InfoPath2010和SP2010--第十二章节--管理和监控InfoPath Form Services(IPFS)(4)--监控含图片控件的Products表单...
  4. 设置窗体的可见性无效
  5. 最新实战讲解.htaccess文件rewrite规则
  6. [转载] python radians函数_Python numpy.radians() 使用实例
  7. [转载] python中的eval函数
  8. jQuery碎语(2) 事件
  9. try catch finally的理解
  10. Confluence自启动脚本