Python pow() Method
The pow()
method returns the specified exponent power of a number.
pow() Syntax:
pow(base, exponent, modulus)
Parameters:
- base: A base number whose exponent power needs to be returned.
- exponent: An integer as an exponent.
- modulus: (Optional) A integer for modulus operations (pow(base, exp) % mod).
Return Value:
Return an integer value.
The following example calculates the power on numbers.
print('2 x 2 = ', pow(2,2))
print('3 x 3 = ', pow(3,2))
print('3 x 3 x 3 = ', pow(3,3))
print('1/(2 x 2) = ', pow(2,-2))
2 x 2 = 4
3 x 3 = 9
3 x 3 x 3 = 27
3 x 3 x 3 x 3 = 81
The modulus parameter returns pow(base, exp) % mod result, as shown below.
print('2 x 2 % 2 = ', pow(2,2,2))
print('3 x 3 % 2 = ', pow(3,2,2))
print('3 x 3 x 3 % 2 = ', pow(3,3,2))
print('3 x 3 x 3 % 4 = ', pow(3,3,4))
2 x 2 % 2 = 0
3 x 3 % 2 = 1
3 x 3 x 3 % 2 = 1
3 x 3 x 3 % 4 = 3
The **
operator is a short form of the pow()
method, as shown below.
print('2 x 2 = ', 2**2)
print('3 x 3 = ', 3**2)
print('3 x 3 x 3 = ', 3**3)
print('3 x 3 x 3 x 3 = ', 3**4)
print('10 x 10 = ', 10**2)
print('1/(10 x 10) = ', 10**-2)
2 x 2 = 4
3 x 3 = 9
3 x 3 x 3 = 27
3 x 3 x 3 x 3 = 81
10 x 10 = 100
1/(10 x 10) = 0.01