python中pow函数_pow()函数以及Python中的示例

python中pow函数

Python pow()函数 (Python pow() function)

pow() function is a library function in Python, is used to get the x to the power of y, where x is the base and y is the power – in other words we can say that pow() function calculates the power i.e. x**y – it means x raised to the power y.

pow()函数是Python中的一个库函数,用于将x赋予y的幂,其中x是基数, y是幂–换句话说,我们可以说pow()函数计算了幂,即x ** y –表示x升为y的幂。

Syntax:

句法:

    pow(x, y [,z])

Parameter(s):

参数:

  • x – A base number which is to be powered

    x –要加电的基数

  • y – A value of the power

    y –幂的值

  • z – It's an optional parameter, it is used to define/fine the modules of the result of (x**y).

    z –这是一个可选参数,用于定义/优化(x ** y)结果的模块。

Return value: numer – returns result.

返回值: numer –返回结果。

Example:

例:

    Input:
x = 2   # base
y = 3   # power
z = 3   # value for modulus
print(pow(x, y))
print(pow(x, y, z))
Output:
8
2

Note:

注意:

  • pow() function with two arguments (x,y) – it is equivalent to x**y

    具有两个参数(x,y)的 pow()函数 –等效于x ** y

  • pow() function with three arguments (x,y,z) – it is equivalent to (x**y) % z

    具有三个参数(x,y,z)的 pow()函数 –等效于(x ** y)%z

pow() function results with different types of the values, consider the below given table,

pow()函数结果具有不同类型的值,请考虑以下给定的表,

XYZ
Negative or Non Negative IntegerNon-negative IntegerMay or may not be present
Negative or Non Negative IntegerNegative IntegerShould not be present
X ÿ ž
负整数或非负整数 非负整数 可能存在或可能不存在
负整数或非负整数 负整数 不应该出现

Example1:

范例1:

# python code to demonstrate example of 
# pow() function 
x = 2   # base
y = 3   # power
z = 3   # value for modulus
# calcilating power with two arguments
result1 = pow(x, y)
# calcilating power & modulus with three arguments
result2 = pow(x, y, z)
# printing the values
print("result1: ", result1)
print("result2: ", result2)

Output

输出量

result1:  8
result2:  2

Note: pow() can return integer and float both values depend on the condition/ values.

注意: pow()可以返回整数,并且两个浮点数都取决于条件/值。

Example2:

范例2:

# python code to demonstrate example of 
# pow() function 
x = 4   # base
y = 3   # power
z = 6   # value for modulus
print("With 2 args:", pow(x,y))     #first taking 2 args only
print("With 3 args:", pow(x,y,z))   #then all the 3 args
print("Return float values:", pow(2,-3))
print('Random numbers power:' , pow(5,5,6))

Output

输出量

With 2 args: 64
With 3 args: 4
Return float values: 0.125
Random numbers power: 5 

计算任何数量幂的不同方法 (Different approaches to calculate the power of any number)

  1. By using simple approach: (x**y)

    通过使用简单的方法:(x ** y)

  2. By using pow() function: pow(x,y)

    通过使用pow()函数:pow(x,y)

  3. By using math.pow() function – which is a function of "math" library

    通过使用math.pow()函数 –这是“数学”库的函数

Note: pow() function takes three arguments (the last one is optional), where math.pow() takes only two arguments. In this, there is no third parameter present else all the functionality is the same as simple pow(). But the math.pow() always returns float values. So if you, for some reason, want to make sure you get float as a result back, then math.pow() will provide that benefit to the user.

注意: pow()函数带有三个参数(最后一个是可选的),其中math.pow()仅带有两个参数。 在此,不存在第三个参数,否则所有功能都与simple pow()相同。 但是math.pow()始终返回浮点值。 因此,如果由于某种原因要确保返回结果为float,则math.pow()将为用户提供这一好处。

Example 1: Calculating X to the power Y using different approaches

示例1:使用不同的方法将X乘以Y

# python code to demonstrate different 
# approaches to calculate x to the power y
import math 
x = 2   # base 
y = 3   # power
result1 = x**y
result2 = pow(x,y)
result3 = math.pow(x,y)
print("result1 (normal approach): ", result1)
print("result2 (using pow() function): ", result2)
print("result3 (using math.pow() function): ", result3)

Output

输出量

result1 (normal approach):  8
result2 (using pow() function):  8
result3 (using math.pow() function):  8.0

Example2: pow() vs math.pow() with third parameter

示例2:带有第三个参数的pow()vs math.pow()

# python code to demonstrate different 
# approaches to calculate x to the power y
import math 
x = 2   # base 
y = 3   # power
z = 3   # for moduls
print("pow(x,y,z): ", pow(x,y,z))
# following statement will throw an error
print("math.pow(x,y,z): ", math.pow(x,y,z))

Output

输出量

pow(x,y,z):  2
Traceback (most recent call last):
File "/home/main.py", line 12, in print("math.pow(x,y,z): ", math.pow(x,y,z))
TypeError: pow expected 2 arguments, got 3

翻译自: https://www.includehelp.com/python/pow-function-with-example-in-python.aspx

python中pow函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380784.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php多维数组

php多维数据我的理解是一个数组的元素是数组&#xff0c;然后又嵌套&#xff0c;就变成多维数组了&#xff0c;比如说二维数组是一维数组的元素变成数组就成二维数组了 <?phpheader(content-type:text/html;charsetutf-8);$cars array(array("Volvo",100,96),a…

python函数与模块学习_Python函数与模块学习1

函数实现具有特定功能的代码自定义函数 内置函数函数特点隐藏实现功能的细节重用代码提高可读性&#xff0c;便于调试函数的定义def 函数名(形式参数1&#xff0c;形式参数2&#xff0c;……形参n)&#xff1a;要运行的代码(函数体)return 输出的数据(返回值)形式参数&#xff…

math.atan2_带有Python示例的math.atan2()方法

math.atan2Python math.atan2()方法 (Python math.atan2() method) math.atan2() method is a library method of math module, it is used to get the arc tangent value of "y/x" (i.e. atan(y/x)), it accepts two numbers and returns arc tangent of "y/x&…

Mapx的VC开发实践

摘 要 阐述了在VC环境下引入MapX控件的方法&#xff0c;以及在文档视图架构下如何使用MapX控件的问题&#xff0c;介绍了MapX数据绑定的方法及其与MapX专题图创建的关系&#xff0c;阐明了创建MapX专题图的一般方法&#xff0c;并给出了具体实例。 关键词 MapX&#xff1b;V…

php的特殊类型

资源 PHP引用的外部数据称为资源&#xff0c;所以资源只能读取&#xff0c;不能创建 <?phpheader(content-type:text/html;charsetutf-8);$mysql_linkmysql_connect(localhost,root,123456);var_dump($mysql_link);echo <br>;$filefopen(./1.txt,r);var_dump($file…

java jni linux_java jni实现linux环境下绑定硬件的License

由于系统运行在Linux环境中&#xff0c;该License绑定服务器的cpuid和mac等信息&#xff0c;而java实现起来不太方便所以就利用了JNI问题及解决方法&#xff1a;1、System.loadLibrary("License");时出错解决&#xff1a;libLicense.so文件要放到正确的目录下&#x…

Python中abs()和fabs()方法之间的区别

In python, abs() method and fabs() method both are used to find the absolute value of a number. They are used for the same purpose but they have a difference, which we are going to discuss in this tutorial. 在python中&#xff0c; abs()方法和fabs()方法都用于…

php中自动转换、强制转换、其他数据类型和bool转换

0x01 自动转换 运算过程需要的数据类型和提供的数据类型不一致&#xff0c;将数据类型转为自己需要的类型 <?phpheader(content-type:text/html;charsetutf-8);echo 1aa7c;echo <br>; ?>加号做数字运算&#xff0c;会将字符串转为数字 0x02 强制转换 强制将…

tf.acos_带有Python示例的math.acos()方法

tf.acosPython math.acos()方法 (Python math.acos() method) math.acos() method is a library method of math module, it is used to get the arc cosine, it accepts a number between -1 to 1 and returns the arc cosine value (in radians) of the given number. math.a…

新买的锅要怎么处理?-新锅开锅处理

最近很忙&#xff0c;年底刚找了新的住处&#xff0c;刚过完圣诞假就一刻不停地打扫卫生、置办东西。这里天高皇帝远的&#xff0c;行政比较弱&#xff0c;啥东西都要自己买。据说这里出租房子一般连橱柜都不带的&#xff0c;基本上只有墙壁、地板和门。万幸&#xff0c;找到一…

php字符串连接符、三元运算符

字符串连接符&#xff1a;. <?phpheader(content-type:text/html;charsetutf-8);echo my name is. .DL_one; ?>三元运算符 形式&#xff1a;表达式&#xff1f;值1&#xff1a;值2 表达式为true&#xff0c;返回值1&#xff0c;为false&#xff0c;返回值2 <?ph…

java多线程知识_学习知库丨Java多线程知识大全

进程&#xff1a;每个进程都有独立的代码和数据空间(进程上下文)&#xff0c;进程间的切换会有较大的开销&#xff0c;一个进程包含1--n个线程。线程&#xff1a;同一类线程共享代码和数据空间&#xff0c;每个线程有独立的运行栈和程序计数器(PC)&#xff0c;线程切换开销小。…

螺旋遍历_螺旋形式的水平阶遍历

螺旋遍历Problem statement: 问题陈述&#xff1a; Write a program to print Level Order Traversal in spiral form of a binary tree. 编写一个程序以二叉树的螺旋形式打印Level Level Traversal 。 Example: 例&#xff1a; For the above tree:Basic level order trave…

SharePoint2007安装图文详解二:安装AD(活动目录)及DNS

在上一篇SharePoint2007安装图文详解一&#xff1a;安装IIS及相关组件中已经介绍了IIS及相关组件的安装&#xff0c;本篇将详细介绍AD&#xff08;活动目录&#xff09;的安装。 打开“管理您的服务器”&#xff0c;点击“添加或删除角色” 点击“添加或删除角色”后弹出“配置…

php的foreach

作用&#xff1a;遍历数组 索引数组 形式&#xff1a;foreach(数组 as 值){ //操作 } <?phpheader(content-type:text/html;charsetutf-8);$personarray(DL_one,18,man);foreach($person as $chara){echo $chara,<br>;} ?>2. 关联数组 形式&#xff1a;foreach…

strcmp java_C语言中strcmp的实现原型

C语言中strcmp的实现原型实现代码&#xff1a;int __cdecl strcmp (const char * src,const char * dst){int ret 0 ;while( ! (ret *(unsigned char *)src - *(unsigned char *)dst) && *dst)src, dst;if ( ret < 0 )ret -1 ;else if ( ret > 0 )ret 1 ;ret…

带有Python示例的math.sin()方法

Python math.sin()方法 (Python math.sin() method) math.sin() method is a library method of math module, it is used to get the sine of the number in radians, it accepts a number returns the cosine of the given number in radians. math.sin()方法是数学模块的库方…

ExtJs实践(3)——xtype名称与控件对应

xtype可作为Ext控件的简写&#xff0c;都会对应一个Ext控件。当然这里你也可以自定义这个xtype&#xff0c;通过自定义的Ext控件来绑定&#xff0c;主要由Ext.reg方法去注册xtype。Ext.all.js里面包含的xtype包含&#xff1a; xtype Class ------------- -------…

fmax()函数以及C ++中的示例

C fmax()函数 (C fmax() function) fmax() function is a library function of cmath header, it is used to find the maximum value of the given numbers, it accepts two number and returns the larger one. fmax()函数是cmath标头的库函数&#xff0c;用于查找给定数字的…

java date传输类型错误_转换日期格式:Java中的转换错误?

我正在尝试将此日期转换为其他格式。不幸的是&#xff0c;他们成功地解析了日期并正确地保留了所有信息。06-Dec-2017 07&#xff1a;14&#xff1a;56.656PM至2017-12-06 19&#xff1a;14&#xff1a;56.656如果我尝试解析输入日期LocalDateTime.parse("06-Dec-2017 07:…