Post

Python pow()

The pow() is the built-in function of python that returns the power of a given number.

Python pow()

The syntax of pow() is:

1
2
pow(x, y, z)

pow() Parameters

The pow() function takes three parameters as argument:

  • x - a base number.
  • y - an exponent number.
  • z (optional) - a number that can be used as modulus.

pow() function indicates the calculation of the power of a given number that is x**y which is equal to xy , and the calculation with modulus is xy % z.

Let see some examples of pow() functions in python.

Example 1: How to use the pow() function in python?

1
2
3
4
5
6
7
8
9
10
11
12
# positive x, positive y (x**y)
print(pow(2, 2))    # 4

# negative x, positive y
print(pow(-2, 2))    # 4  

# positive x, negative y
print(pow(2, -2))    # 0.25

# negative x, negative y
print(pow(-2, -2))    # 0.25

Output:

1
2
3
4
5
4
4
0.25
0.25

Example 2: How to use pow() with modules argument?

1
2
3
4
5
6
x = 7
y = 2
z = 5

print(pow(x, y, z))    # 4

Output:

1
2
4

Here we are calculating (x**y) % z, and that 7 powered by 2 equals 49. So then, 49 modulus 5 equals 4 in ablow program.

This post is licensed under CC BY 4.0 by the author.