AppDividend
Latest Code Tutorials

Python pow() Example | pow() Function In Python Tutorial

0

Python pow() is an inbuilt function that returns x to the power of y. If the third argument (z) is given, it returns x to the power of y modulus z, i.e., pow(x, y) % z. The pow() function returns a value of x to the power of y (xy). If the third parameter is present, it returns x to the power of y, modulus z. 

Python pow() Example

Python pow() function computes a**b. The pow() function first converts its arguments into float and then computes the power. So, if you want to compute power operation then pow() function is your solution.

The syntax of the pow() method is the following.

pow(a, b, c)
Parameter Description
a A number, the base
b A number, the exponent
c Optional. A number, the modulus

 

  1. If only two arguments are provided, then a to the power of b is returned. In this case, the arguments can be integers, floats, and complex numbers. The two-argument form of pow(a, b) is equivalent to using the power operator: a**b.
  2. If three arguments are provided, then a to the power b, modulo c is returned. It’s computed more efficiently than using pow(a, b) % c.
  3. If c is present, a and b must be of integer types, and b must be non-negative.

See the following example.

# app.py

data = pow(4, 3)
print(data)

See the output.

➜  pyt python3 app.py
64
➜  pyt

Let’s pass the third parameter and see the output.

# app.py

data = pow(4, 3, 10)
print(data)

So, what is does is first 4*4*4, which is 64 and then 64%10, which is 4. That is why it will give us output 4.

➜  pyt python3 app.py
4
➜  pyt

#Python pow() with floats

See the following code example of floats.

# app.py

print(pow(11, 2.0))

print(pow(21.0, 2))

See the output.

➜  pyt python3 app.py
121.0
441.0
➜  pyt

#Python pow() with different format integers

See the following code example.

# app.py

print(pow(0b11, 2)) 
print(pow(0b11, 2, 2))

print(pow(0o11, 2))
print(pow(0o11, 3, 2))

print(pow(0xF, 2))
print(pow(0xF, 3, 2))

See the following output.

➜  pyt python3 app.py
9
1
81
1
225
1
➜  pyt

#pow() with complex numbers

See the following code.

# app.py

print(pow(11 + 21j, 2))

See the output.

➜  pyt python3 app.py
(-320+462j)
➜  pyt

#pow() vs math.pow()

Python math module also has the pow() function, but the built-in function is more powerful because we can perform the modulo operation too after power.

Also, we don’t need to import the math module for a single functionality.

So, it is preferable to use Python built-in pow() function and not the math.pow() function.

The math.pow() always returns float values. So if you, for some reason, want to make sure you get float as a result back, then math.pow() will provide that benefit to the user.

Finally, Python pow() Example Tutorial is over.

See also

Python sqrt()

Python absolute value

Python complex()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.