The math.pow() method, provided within the math module, is used to calculate the power of a number. It takes two arguments: the base number (which is the number you want to raise to a power) and the exponent. It returns the result of raising the base to the power of the exponent (which is the power to which the base is raised).
This method specifically deals with floating-point numbers and always returns a floating-point result, even if the base and exponent are integers.
In addition to the math.pow() method, Python provides the built-in pow() function. While both functions calculate powers, the difference lies in the data types they handle and the types of results they return.Syntax
Following is the syntax of Python math.pow() method −
math.pow(x, y)Parameters
Following are the parameters accepted by the Python math.pow() method −
x − number which is to be powered (base).
y − number which is to be powered with x (exponent).
This method returns the value of xy.
Example 1In the following example, we are using math.pow() with integers (2 as the base and 3 as the exponent). Despite both arguments being integers, this method consistently returns a floating-point result −
In the following example, we are using math.pow() with integers (2 as the base and 3 as the exponent). Despite both arguments being integers, this metho consistently returns a floating-point result −
import math # Using math.pow() with integers result = math.pow(2, 3) print("Result:", result)Output
When we execute the above program, it produces following result −
Result: 8.0Example 2
In this example, we are using the math.pow() with a floating-point number "2.0" as the base and an integer "3" as the exponent. Similar to the previous example, the method returns a floating-point result −
import math # Using math.pow() with integers result = math.pow(2, 3) print("Result:", result)Output
When we execute the above program, it produces following result −
Result: 8.0Example 3
In here, we are creating an exponent consisting of a negative number. We then use the math.pow() method to retrieve the result −
import math # negative exponent result = math.pow(5, -2) print("Result:", result)Output
Following is the output of the above code −
Result: 0.04
python_maths.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4