Python pow() With Examples

Python pow() function usually takes two number arguments and returns their power.

Python pow()

Python pow() function syntax is:

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

Let’s look into some pow() function examples.

Python pow() with integers


Python pow() with floats


pow() with different format integers


pow() with complex numbers

Output: (-5+12j)

pow() exception scenarios

  1. Error: ValueError: complex modulo

  2. Error: TypeError: pow() 3rd argument not allowed unless all arguments are integers

  3. Error: ValueError: pow() 2nd argument cannot be negative when 3rd argument specified

pow() vs math.pow()

Python math module also has a pow() function but the built-in function is more powerful because we can perform modulo operation too after power. Also, we don’t need to import math module for a single functionality.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: