# pow() in Python

April 5, 2021 4809

Normally, we calculate power of a number as:

​25 = 2 x 2 x 2 x 2 x 2 = 32
53 = 5 x 5 x 5 = 125
92 = 9 x 9 = 81

As we see, Python has a built-in function to calculate the power of a given number. Let's see:

## Definition of pow()

pow() is a built-in function in Python that is used to compute the power of a given number. If the number is x and the power is y then pow() returns x to the power y that is xy  or x**y. The function pow() takes three parameters x which is the base number, y which is the exponent and the third parameter z which is optional is used to calculate the modulus that is (x**y)% z
pow(x,y) is equivalent to x**y that is xy
pow(x,y,z) is equivalent to (x**y)%z

## Syntax of pow()

pow(x,y,z)

Parameter
Description
Required/Optional
x
The base number
Required
y
The exponent, that is the value of the power
Required
z
The value to calculate the modulus
Optional

Let's get to the examples of pow()

## Examples of pow()

p = pow(5, 3)    # 5<sup>3</sup>
print(p)

q = pow(2, 5)    # 2<sup>5</sup>
print(q)

r = pow(3, 4)    # 3<sup>4</sup>
print(r)


​125

32

81

Let's see whether it works with negative numbers as well:
p = pow(-2, 3)
print(p)

q = pow(2, -2)
print(q)

r = pow(-3, -2)
print(r)


-8

0.25

0.1111111111111111

Let's see some examples with the third parameter of pow():
p = pow(2, 3, 3)    # (2**3)%3
print(p)

q = pow(5, 2, 4)    # (5**2)%4
print(q)

r = pow(4, 4, 7)    # (4**4)%7
print(r)


​2

1

4

pow() also works with floating numbers and other numeric formats too. Let's see:
# binary
print(pow(0b1010, 2))  # 0b1010 = 100

# float
print(pow(2.2, 3))

print(pow(0x15, 2))  # 0x15 = 21


​100

10.648000000000003

441

Liked the post?
Rarely seen, always noticed.
Editor's Picks
0 COMMENT