Open In App

math.pow() in Python

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, math.pow() is a function that helps you calculate the power of a number. It takes two numbers as input: the base and the exponent. It returns the base raised to the power of the exponent.

In simple terms, if you want to find "x raised to the power y", you can use math.pow(x, y).

Example:

Python
import math

# Calculate 2 raised to the power of 3
r = math.pow(2, 3)
print(r)  

Output
8.0

Explanation:

  • math.pow(2, 3) → Raises 2 to the power of 3.
  • Returns 8.0 (float), even though the result is a whole number.

Syntax of math.pow() method

math.pow(x, y)

Parameters

  • x: The base number.
  • y: The exponent.

Return Value

Returns a floating-point number (float), even if the result is a whole number.

Important Points About math.pow()

  • It always returns a floating-point number (float), even if the result is a whole number.
  • It only works with numbers (integers or floats).
  • If you give it a negative base with a fractional exponent, it may give an error.

Examples of math.pow() method

1. Power of a Fraction

Python
import math

# Calculate (1/2) raised to the power of 2
r = math.pow(1/2, 2)
print(r)

Output
0.25

Explanation: This code calculates (1/2)^2, which equals 0.25. The result is returned as a float (0.25).

2. Negative Exponent

Python
import math

# Calculate 2 raised to the power of -3
r = math.pow(2, -3)
print(r)

Output
0.125

Explanation: This code calculates 2^(-3), which is the reciprocal of 2^3 (1/8 = 0.125). The result is returned as a float (0.125).

3. Power with Floating-Point Numbers

Python
import math

# Calculate 3.5 raised to the power of 2
r = math.pow(3.5, 2)
print(r)

Output
12.25

Explanation: This code calculates 3.5^2, which equals 12.25. The result is returned as a float (12.25).

4. Using math.pow() with Negative Numbers

Python
import math

# Calculate (-2) raised to the power of 3
r = math.pow(-2, 3)
print(r)

Output
-8.0

Explanation: This code calculates (-2)^3, which equals -8. The result is returned as a float (-8.0).


Next Article
Article Tags :
Practice Tags :

Similar Reads