数学上,一个特定数的立方根被定义为当这个数连续三次被自己除时所得到的值。它是一个立方数的反向操作。例如,216的立方根是6,因为6 × 6 × 6 = 216。本文的任务是使用Python找到给定数的立方根。
The cube root is represented using the symbol “$mathrm{sqrt[3]{a}}$”. The 3 in the symbol denotes that the value is divided thrice in order to achieve the cube root.
在Python中,有多种方法可以计算一个数的立方根。让我们逐个来看一下它们:
使用一个简单的数学公式。
使用math.pow()函数。
使用 cbrt() 函数在 numpy 中。
输入输出场景
现在让我们来看一些输入输出场景,以计算给定数字的立方根 -
假设给定的输入数字为正数,输出显示为 −
Input: 8 Result: 2
假设给定的输入是负数,则输出显示为 −
Input: -8 Result: -2
假设输入是一个元素列表,输出是通过以下方式获得的 -
Input: [8, -125] Result: [2, -5]
使用数学方程
让我们从简单开始;我们使用一个简单的数学方程在Python中找到一个数的立方根。在这里,我们找到输入数字的$mathrm{frac{1}{3}}$次方。
示例1:对于正数
给定的是一个计算正数立方根的Python程序。
#take an input number num = 216 #calculate cube root cube_root = num ** (1/3) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
输出
The output of the above python code is −
Cube root of 216 is 5.999999999999999
例子2:对于负数
给出下面的Python程序,计算一个负数的立方根。
#take an input number num = -216 #calculate cube root cube_root = -(-num) ** (1/3) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
输出
Cube root of -216 is -5.999999999999999
使用 math.pow() 函数
math.pow(x, y)函数返回x的y次幂的值,其中x的值始终为正数。所以在这种情况下,我们使用这个函数将输入的数字提高到其$mathrm{frac{1rd}{3}}$次幂。
示例1:对于正数
在下面的Python程序中,我们找到一个正输入数的立方根
import math #take an input number num = 64 #calculate cube root cube_root = math.pow(num, (1/3)) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
输出
实现的输出为−
Cube root of 64 is 3.9999999999999996
例子2:对于负数
在下面的Python程序中,我们找到了一个负输入数的立方根。
import math #take an input number num = -64 #calculate cube root cube_root = -math.pow(-num, (1/3)) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
输出
实现的输出为−
Cube root of -64 is -3.9999999999999996
使用numpy的cbrt()函数
cbrt()是numpy库中的一个内置函数,它返回输入数组中每个元素的立方根。该方法在计算负数的立方根时不会引发错误,因此比之前的方法更高效。
Example
在下面的Python示例中,我们使用Python列表获取输入,并使用 cbrt() 函数找到立方根。
#import numpy library to access cbrt() function import numpy as np #take an input list num = [64, -729] #calculate cube root of each element in the list cube_root = np.cbrt(num) #display the output print("Cube root of ", str(num), " is ", str(cube_root))
输出
在编译和执行上述Python代码时,可以获得以下输出 -
Cube root of [64, -729] is [ 4. -9.]
以上是Python程序计算给定数字的立方根的详细内容。更多信息请关注PHP中文网其他相关文章!

Inpython,YouAppendElementStoAlistusingTheAppend()方法。1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware

调试shebang问题的方法包括:1.检查shebang行确保是脚本首行且无前置空格;2.验证解释器路径是否正确;3.直接调用解释器运行脚本以隔离shebang问题;4.使用strace或truss跟踪系统调用;5.检查环境变量对shebang的影响。

pythonlistscanbemanipulationusesseveralmethodstoremovelements:1)theremove()MethodRemovestHefirStocCurrenceOfAstePecifiedValue.2)thepop()thepop()methodremovesandremovesandurturnturnsananelementatagivenIndex.3)

pythristssupportnumereperations:1)addingElementSwithAppend(),Extend(),andInsert()。2)emovingItemSusingRemove(),pop(),andclear(),and clear()。3)访问andmodifyingandmodifyingwithIndexingAndexingAndSlicing.4)

使用NumPy创建多维数组可以通过以下步骤实现:1)使用numpy.array()函数创建数组,例如np.array([[1,2,3],[4,5,6]])创建2D数组;2)使用np.zeros(),np.ones(),np.random.random()等函数创建特定值填充的数组;3)理解数组的shape和size属性,确保子数组长度一致,避免错误;4)使用np.reshape()函数改变数组形状;5)注意内存使用,确保代码清晰高效。

播放innumpyisamethodtoperformoperationsonArraySofDifferentsHapesbyAutapityallate AligningThem.itSimplifififiesCode,增强可读性,和Boostsperformance.Shere'shore'showitworks:1)较小的ArraySaraySaraysAraySaraySaraySaraySarePaddedDedWiteWithOnestOmatchDimentions.2)

forpythondataTastorage,choselistsforflexibilityWithMixedDatatypes,array.ArrayFormeMory-effficityHomogeneousnumericalData,andnumpyArraysForAdvancedNumericalComputing.listsareversareversareversareversArversatilebutlessEbutlesseftlesseftlesseftlessforefforefforefforefforefforefforefforefforefforlargenumerdataSets; arrayoffray.array.array.array.array.array.ersersamiddreddregro


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。