Python built-in Method - pow()

The pow() function is a built-in Python method that returns the result of raising a number to a given power, optionally modulo another number.

The syntax for the pow() function is as follows:

pow(x, y[, z])
So‮ru‬ce:www.theitroad.com
  • x: The base number.
  • y: The exponent.
  • z (optional): The modulus.

Here are some example usages of the pow() function:

>>> pow(2, 3)
8

In this example, we use the pow() function to calculate 2 raised to the power of 3, which is equal to 8.

>>> pow(2, 3, 5)
3

In this example, we use the pow() function to calculate (2**3) % 5, which is equal to 3. The third argument to the pow() function is the modulus z.

The pow() function can also be used with negative numbers and fractional exponents:

>>> pow(-2, 3)
-8

>>> pow(2, -3)
0.125

>>> pow(2, 0.5)
1.4142135623730951

In the first example, we use the pow() function to calculate -2 raised to the power of 3, which is equal to -8. In the second example, we use the pow() function to calculate 2 raised to the power of -3, which is equal to 0.125. In the third example, we use the pow() function to calculate 2 raised to the power of 0.5, which is equal to the square root of 2, approximately 1.4142135623730951.

The pow() function is a useful built-in method in Python for performing power calculations and modular arithmetic operations. It is often used in mathematical and scientific programming applications.