日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/1 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中pow函数_pow()函数以及Python中的示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python中pow函數(shù)

Python pow()函數(shù) (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()函數(shù)是Python中的一個庫函數(shù),用于將x賦予y的冪,其中x是基數(shù), y是冪–換句話說,我們可以說pow()函數(shù)計算了冪,即x ** y –表示x升為y的冪。

Syntax:

句法:

pow(x, y [,z])

Parameter(s):

參數(shù):

  • x – A base number which is to be powered

    x –要加電的基數(shù)

  • 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 –這是一個可選參數(shù),用于定義/優(yōu)化(x ** y)結(jié)果的模塊。

Return value: numer – returns result.

返回值: numer –返回結(jié)果。

Example:

例:

Input:x = 2 # basey = 3 # powerz = 3 # value for modulusprint(pow(x, y))print(pow(x, y, z))Output:82

Note:

注意:

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

    具有兩個參數(shù)(x,y)的 pow()函數(shù) –等效于x ** y

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

    具有三個參數(shù)(x,y,z)的 pow()函數(shù) –等效于(x ** y)%z

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

pow()函數(shù)結(jié)果具有不同類型的值,請考慮以下給定的表,

XYZ
Negative or Non Negative IntegerNon-negative IntegerMay or may not be present
Negative or Non Negative IntegerNegative IntegerShould not be present
X ? ?
負(fù)整數(shù)或非負(fù)整數(shù) 非負(fù)整數(shù) 可能存在或可能不存在
負(fù)整數(shù)或非負(fù)整數(shù) 負(fù)整數(shù) 不應(yīng)該出現(xiàn)

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()可以返回整數(shù),并且兩個浮點數(shù)都取決于條件/值。

Example2:

范例2:

# python code to demonstrate example of # pow() function x = 4 # base y = 3 # power z = 6 # value for modulusprint("With 2 args:", pow(x,y)) #first taking 2 args only print("With 3 args:", pow(x,y,z)) #then all the 3 argsprint("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

計算任何數(shù)量冪的不同方法 (Different approaches to calculate the power of any number)

  • By using simple approach: (x**y)

    通過使用簡單的方法:(x ** y)

  • By using pow() function: pow(x,y)

    通過使用pow()函數(shù):pow(x,y)

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

    通過使用math.pow()函數(shù) –這是“數(shù)學(xué)”庫的函數(shù)

  • 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()函數(shù)帶有三個參數(shù)(最后一個是可選的),其中math.pow()僅帶有兩個參數(shù)。 在此,不存在第三個參數(shù),否則所有功能都與simple pow()相同。 但是math.pow()始終返回浮點值。 因此,如果由于某種原因要確保返回結(jié)果為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 yimport math x = 2 # base y = 3 # powerresult1 = 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:帶有第三個參數(shù)的pow()vs math.pow()

    # python code to demonstrate different # approaches to calculate x to the power yimport math x = 2 # base y = 3 # power z = 3 # for modulsprint("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函數(shù)

    總結(jié)

    以上是生活随笔為你收集整理的python中pow函数_pow()函数以及Python中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。