# Python Power pow() Tutorial

Python provides different power functions and operators in order to make mathematical power calculations. Power is also called as exponentiation. Simple power operations will multiple given numbers with itself with a specified time. For example, a power b will multiply a with itself for b times. In mathematics power generally expressed as a b or a**b . Python provides a built-in, math module and 3rd party modules in order to calculate power.

## Built-in pow() Method Syntax

The easiest and practical way to calculate power is by using the Butlin method pow(). It is provided by PYthon as Butlin and there is no need to load any module or install the extra library. The pow() method has the following syntax.

``float pow( x , y , m )``
• x is the number in which power will be calculated. a can be float number. x can be negative or positive.
• y is the value raised to compute power.
• m is mod where the result can be taken as mod. The m parameter is optional.
• float is the return type of the power calculation even provided a and b are integers.

## math.pow() Method Syntax

Another way to calculate power of the given using the math module math.pow() method. Its syntax is very similar to the builtin pow() method where there is not mod parameter.

``pow( x , y )``
• x is the number in which power will be calculated. a can be float number. x can be negative or positive.
• y is the value raised to compute power.

## Power Operator **

Python also provides the `**` power operator. The power operator is more readable with the two astersk signs.

``a ** b``
• x is the number in which power will be calculated. a can be float number. x can be negative or positive.
• y is the value raised to compute power.

## Calculate Power

Lets start calculating power of the given numbers. Upto now we have seen that there is 3 basic method to calculate mathematical power of the given number.

``````import math

a = 3
b = -3
c = 2

print ( a ** c )
print ( 3 ** 2 )

print( pow( a , c ) )
print( pow( 3 , c ) )

print( math.pow( a , c ) )
print( math.pow( 3 , c ) )
``````

## Calculate Power and Mod

The pow() function provides the ability to calculate power and mod. The mod parameter will be specified as 3rd parameter where the mod value will be provided. math.pow() method does not provide the mod parameter.

``````a = 3
b = -3
c = 2

mod = 4

print( pow( a , c , mod ) )
print( pow( 3 , c , mod ) )
``````

## NumPy Library power() Method

NumPy is a popular 3rd party Python library which also provides the pow() method. The pow() mehtod provided directly from the numpy module like numpy.pow(). The syntax is the same with the built-in numpy and math.numpy() methods.

``````import numpy

a = 3
b = -3
c = 2

print( numpy.pow( a , c ) )
print( numpy.pow( 3 , c ) )
``````

## Scipy Library power() Method

Scipy is another popular Python library which provides science related modules where pow() is also provided. We can use the pow() method from the Scipy like below.

``````import scipy

a = 3
b = -3
c = 2

print( scipy.pow( a , c ) )
print( scipy.pow( 3 , c ) )
``````

## How To Write Power Function

Up to now, we have used ready to use operators or methods in order to calculate the power of given numbers. Calculating the power is an easy operation where we can create or write a function in order to calculate given integer or base power. We will create a function named mypow( a , b ) and use for power calculation. We can also add new features to this function.

``````def mypow( a , b ):
result = 1
for i in range (1,b):
result = a * result
return result

mypow(3,4)
``````