Python pow

The Python pow function is used to calculate the power of the specified expression and the syntax it is

math.pow(base, Exponent);
  • Base: Please specify the base value here.
  • Exponent: Please specify the Exponent value or power here.

For example, if x is the base value and 2 is the exponent, then the Math.pow(x, 2) = x². If the Base value or Exponent value argument is not a number, the pow function returns TypeError.

Python math pow Function Example

The pow function returns the Power of the given number. In this example, we are going to find the power of different data types and display the output.

  1. Within the first three statements, we passed both the positive and negative integers as arguments. From the above screenshot, you can observe that the Python math.pow function is returning the power of a number output.
  2. Next, We used the Tuple and List items as base arguments and integer value as an exponent. If you see the above Python image, the Math method is working correctly on them.
  3. We assigned multiple values as base arguments.
  4. Last, We tried on the String value, and it returned TypeError.
import math

Tup = (10.98, 20.26, 12.05, -40.95 , 50.45) # Tuple Declaration
Lis = [-10.98, 32.65, -39.59, -42.15 , 15.97] # List Declaration

print('Calculating of Positive Number = %.2f' %math.pow(2, 3))
print('Calculating of Negative Number = %.2f' %math.pow(-2, 3))
print('Calculating of String Number = ', math.pow(2, -3))

print('Calculating of of Tuple Item = %.2f' %math.pow(Tup[2], 2))
print('Calculating of List Item = %.2f' %math.pow(Lis[4], 4))

print('Calculating of Multiple Number = %.2f' %math.pow(1 + 2 - 12.65, 2))

print('Calculating of String Value = ', math.pow('2', 3))
Python pow math function to find power of a number

How to write 2 to the power of 3 in Python?

We can use either the built-in math.pow() function with 2 and 3 as the arguments, or use the exponent operator (**).

import math

print(math.pow(2, 3))
print(2 ** 3)
8.0
8

Python power of 2

We can use the exponent operator (**) or the built-in math.pow() function to calculate the power of 2.

import math
print(math.pow(2, 4))
print(2 ** 4)
16.0
16

If your goal is to check whether the number is a power of two, then we must use the code below. Here, the function checks whether the user-given number is a power of 2.

def poweroftwo(n):
return n > 0 and (n & (n - 1)) == 0

print(poweroftwo(8))
print(poweroftwo(18))
print(poweroftwo(32))
True
False
True

Handling Negative values in the Python pow function

The math.pow() function handles negative values depending on whether the negative value is a base or an exponent.

Integer base and Negative exponent

The pow() function supports the negative exponent value. If we pass a negative exponent value, the pow() function calculates the power. In the following example pow(2, -2) means ½2 = ¼ = 0.25.

import math

print(math.pow(2, -2))
print(math.pow(2, -3))
0.25
0.125

Negative base and integer exponent

In the following example, we use the Python pow() function with a negative base value and a positive integer exponent. In mathematics, an even number of negatives becomes positive, and the odds become negative.

  • If the exponent is an even number, the pow() function returns a positive number.
  • If the exponent is an odd number, the pow() function returns a negative number.
import math
print(math.pow(-2, 2))
print(math.pow(-4, 3))
4.0
-64.0

Python pow function with 3 arguments

Apart from the above mentioned basic syntax, the pow() function accepts a third argument (modulus), which is helpful for modular exponentiation. The syntax is

pow(base, exponent, modulus)

The pow() function with 3 arguments calculates base power exponent mod modulus. Otherwise, (base)exponent mod modulus, which is equivalent to (base ** exponent) % modulus. It is very helpful and faster when working with large numbers.

In the following example, we use simple small numbers to explain the working functionality of the pow() function with 3 arguments. Here, 25 = 32. Next, 32 mod 3 = 2 (3 * 10 = 30 and 2 is the remainder).

print(pow(2, 5, 3))
2