# pow () in Python

Python Methods and Functions

Naive method for calculating power:

 ` # Python code to demonstrate a naive method ` ` # to calculate power ` ` n ` ` = ` ` 1 ` ` for ` ` i ` ` in ` ` range ` ` (` ` 1 ` `, ` ` 5 ` `): ` ` n ` ` = ` ` 3 ` ` * ` ` n `   ` print ` ` (` `" The value of 3 ** 4 is: "` `, end ` ` = ` `" ") ` ` print ` ` (n) `

Output:

` The value of 3 ** 4 is: 81 `

Using pow ()

1. float pow (x, y): this function computes x ** y . This function first converts its arguments to float and then calculates the cardinality.

`  Declaration:  float pow (x, y)  Parameters:   x:  Number whose power has to be calculated.  y:  Value raised to compute power.  Return Value:  Returns the value x ** y in float. `

` `

` # Python code to demonstrate pow () # version 1   print ( "The value of 3 ** 4 is:" , end = "")   # Returns 81 print ( pow ( 3 , 4 )) `

Output:

` The value of 3 ** 4 is: 81.0 `

2. float pow (x, y, mod): this function calculates (x ** y)% mod . This function first converts its arguments to float and then calculates the cardinality.

`  Declaration:  float pow (x, y, mod)  Parameters:   x:  Number whose power has to be calculated.  y:  Value raised to compute power.  mod:  Value with which modulus has to be computed.  Return Value:  Returns the value (x ** y)% mod in float. `

` `

` # Python code to demonstrate pow () # version 2   print ( "The value of (3 ** 4)% 10 is:" , end = " ")    # Returns 81% 10 # Returns 1 print ( pow ( 3 , 4 , 10 )) `

` ` Output:

` The value of (3 ** 4)% 10 is: 1 `

Pow () implementations:

 ` # Python code for discussing negative ` ` # and non-negative cases `   ` # positive x, positive y (x ** y) ` ` print ` ` (` ` "Positive x and positive y:" ` `, end ` ` = ` ` "") ` ` print ` ` (` ` pow ` ` (` ` 4 ` `, ` ` 3 ` `)) `   ` print ` ` (` ` "Negative x and positive y:" ` `, end ` ` = ` ` "") ` ` # negative x, positive y (-x ** y) ` ` print ` ` (` ` pow ` ` (` ` - ` ` 4 ` `, ` ` 3 ` `)) `   ` print ` ` (` ` "Positive x and negative y : "` `, end ` ` = ` `" ") ` ` # positive x, negative y (x ** - y) `   ` print ` ` (` ` pow ` ` (` ` 4 ` `, ` ` - ` ` 3 ` `)) ` ` `  ` print ` ` (` ` "Negative x and negative y:" ` `, end ` ` = ` `" ") ` ` # negative x, negative y (-x ** - y) ` ` print ` ` (` ` pow ` ` (` ` - ` ` 4 ` `, ` ` - ` ` 3 ` `)) `

Output:

` Positiv ex and positive y: 64 Negative x and positive y: -64 Positive x and negative y: 0.015625 Negative x and negative y: -0.015625 `

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.