Python power: How to Calculate Power in Python

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

Python has a ** operator that can be used to raise a number into a power of an exponent. The ** operator requires two values to perform a calculation.

Python power

To calculate power in Python, use the pow() function. The pow() is a built-in Python 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 converts its arguments into float and computes the power. So, if you want to compute power operation, then the pow() function is your solution.

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. 

Syntax

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. The arguments can be integers, floats, and complex numbers in this case. 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 it 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

To calculate the power of floating-point values in Python, use the pow() function.

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

We can calculate the power of different format integers using the pow() method.

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

To calculate the power of complex numbers in Python, use the pow() function.

# 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 after power. 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.

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.

That’s it for this tutorial.

See also

Python sqrt()

Python absolute value

Python complex()

Leave A Reply

Please enter your comment!
Please enter your name here

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