Python math.pow() | Power of a Number

Python has a standard “math” module that provides different kinds of functions to perform various kinds of mathematical operations. The “math.pow()” function also belongs to the math module’s family and is used to calculate the power of a number.

This article will provide you with a detailed demonstration of Python math.pow() function with the below listed content:

Let’s get started!

What is math.pow() in Python?

The “math.pow()” function in Python returns the value of x “base number” raised to the power of y “exponent number”. This function is accessible after importing the math module at the start of the Python script.

How to install Kali Linux in Window...

The syntax of the “math.pow()” function is below:

math.pow(x, y)

In the above syntax:

  • The parameter “x” represents the base value number.
  • The parameter “y” represents the exponent value number.

The value retrieved by this “math.pow()” is the “float” type.

Note: if the value of parameter “x” is negative and the value of parameter “y” is not an integer, the “math.pow()” retrieves a “ValueError”.

Example 1: Power of Integer Base Number and Integer Exponent

In the example given below, the “math.pow()” returns the value of the positive base number raised to the power of the positive exponent number:

Code:

import math

Number_1 = 2
Number_2 = 10
output = math.pow(Number_1, Number_2)
print('pow(x, y) :', output)

In the above code:

  • The “math” module is imported into the program.
  • Two positive integer type values are initialized.
  • The “math.pow()” calculates the value of the base number “2” raised to the power of the exponent number “10”. The base number is placed in the first argument, and the exponent number is placed in the second.

Output:

In the above output, the power of a number is calculated successfully, and the value is in float.

Example 2: Power of Negative Base Number and Integer Exponent

In the example given below, the “math.pow()” returns the value of the negative base number raised to the power of the positive exponent number.

Code:

import math

Number_1 = -2
Number_2 = 3
output = math.pow(Number_1, Number_2)
print('pow(x, y) :', output)

In the above code:

  • The “math” module is initialized and imported into the program.
  • Negative base integer type value and positive exponent value are initialized.
  • The “math.pow()” calculates the value of base number “-2” raised to the power of exponent number “3”.

Output:

The value is returned in float and is a negative number.

Note: If the base is negative and the exponent is an odd-positive, the output will be negative. Similarly, if the base is negative but the exponent is an even positive, the output will be a positive number.

Example 3: Power of Negative Base Number and Float Exponent

In the example given below, the “math.pow()” is used with the negative base number raised to the power of the float exponent number.

Let’s see how “math.pow()” behaves in such a situation. 

Code:

import math

Number_1 = -2
Number_2 = 3.3
output = math.pow(Number_1, Number_2)
print('pow(x, y) :', output)

In the above code:

  • The “math” module is initialized and imported into the program.
  • Negative base number as an integer type and exponent value as float type are initialized.
  • The “math.pow()” does not calculate the value of number “-2” raised to the power of number “3.3”. The function gives “ValueError” while executing the program.

Output:

In the above output, the power of a number is not calculated due to “ValueError”.

Example 4: Power of Infinity Number

In the example given below, two “math.pow()” functions are used with the value of the infinite base number raised to the power of a finite number and vice versa.

Code:

import math

print ('pow(x, y) :',  math.pow(math.inf, 3))
print ('pow(x, y) :',  math.pow(2, math.inf))

In the above code:

  • The “math.inf” constantly used in the above code returns a positive infinity value.
  • The “math.pow()” calculates the value of the number “infinity” raised to the power of number “3”.
  • Similarly, the “math.pow()” also calculates the final value of the base number “2” raised to the power of exponent “infinity”.

Output:

The output shows that if either base or the exponent is infinity, then the “math.pow()” will return infinity. 

That’s it, Guys!

Conclusion

To calculate the power of a number, the “math.pow()” function is utilized in the Python program. It returns a value of base raised to a power of exponent. The “math.pow()” function takes two parameter values, base and exponent. The function returns “ValueErrors” when the base number value is less than “zero” and the exponent value is “float”. The infinite base or exponent number has returned the infinite number in the output.

This guide has demonstrated the Python”math.pow()” function of the math module with multiple examples.