Python

Python pow() Function

The pow() function is used to calculate the power of a number. It is similar to using the ** operator, but it also provides an optional third argument for modular exponentiation.

Basic Syntax

pow(base, exponent)

or

pow(base, exponent, modulus)
  • base → The number being raised to a power
  • exponent → The power
  • modulus → Optional value used to calculate the remainder

Basic Example

result = pow(2, 3)

print(result)

Output:

8

This means:

2³ = 2 × 2 × 2 = 8

Using pow() with Variables

base = 5 exponent = 2

result = pow(base, exponent)

print(result)

Output:

25

pow() vs **

Both can calculate powers.

print(pow(2, 4))
print(2 ** 4)

Output:

16
16

For basic power calculations, both produce the same result.

Using Decimal Numbers

result = pow(2.5, 2)

print(result)

Output:

6.25

Using a Negative Exponent

A negative exponent produces a fractional result.

result = pow(2, -2)

print(result)

Output:

0.25

This is equivalent to:

1 / 2² = 1 / 4 = 0.25

Using Zero as the Exponent

Any non-zero number raised to the power of zero is 1.

print(pow(10, 0))
print(pow(25, 0))
print(pow(100, 0))

Output:

1
1
1

Using One as the Exponent

number = 50

print(pow(number, 1))

Output:

50

Negative Base

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

Output:

-8
4

Because:

(-2)³ = -8
(-2)² = 4

pow() with map()

You can combine pow() with map().

numbers = [1, 2, 3, 4, 5]

result = map(
    lambda x: pow(x, 2),
    numbers
)

print(list(result))

Output:

[1, 4, 9, 16, 25]

Calculate Squares

numbers = [2, 4, 6, 8]

squares = list(
    map(
        lambda x: pow(x, 2),
        numbers
    )
)

print(squares)

Output:

[4, 16, 36, 64]

Calculate Cubes

numbers = [1, 2, 3, 4]

cubes = list(
    map(
        lambda x: pow(x, 3),
        numbers
    )
)

print(cubes)

Output:

[1, 8, 27, 64]

pow() with sum()

You can calculate the sum of powers.

numbers = [1, 2, 3, 4, 5]

result = sum(
    pow(x, 2)
    for x in numbers
)

print(result)

Output:

55

This calculates:

1² + 2² + 3² + 4² + 5²
= 55

pow() with filter()

You can filter numbers based on their power.

numbers = [1, 2, 3, 4, 5]

result = filter(
    lambda x: pow(x, 2) > 10,
    numbers
)

print(list(result))

Output:

[4, 5]

Because:

1² = 1
2² = 4
3² = 9
4² = 16
5² = 25

Only 4 and 5 have squares greater than 10.

Third Argument : Modulus

One of the important features of pow() is its optional third argument.

result = pow(2, 5, 3)

print(result)

Output:

2

Python calculates:

2⁵ = 32

32 % 3 = 2

So:

pow(2, 5, 3)

is equivalent to:

(2 ** 5) % 3

Another Modulus Example

result = pow(10, 3, 7)

print(result)

Output:

6

Because:

10³ = 1000

1000 % 7 = 6

Why the Third Argument Is Useful

The three-argument form is particularly useful when working with large numbers, because Python can calculate modular exponentiation efficiently without first creating the entire huge power.

result = pow(2, 100, 7)

print(result)

Output:

2

Practical Example : Compound Growth

Suppose an investment grows by a fixed factor.

principal = 10000 rate = 1.05 years = 5

amount = principal * pow(rate, years)

print(round(amount, 2))

Output:

12762.82

The formula is:

Amount = Principal × Rateⁿ

Practical Example : Area of a Square

side = 10

area = pow(side, 2)

print("Area:", area)

Output:

Area: 100

Practical Example : Volume of a Cube

side = 5

volume = pow(side, 3)

print("Volume:", volume)

Output:

Volume: 125

Practical Example : Calculate Powers

number = 3

print("Square:", pow(number, 2))
print("Cube:", pow(number, 3))
print("Fourth Power:", pow(number, 4))

Output:

Square: 9
Cube: 27
Fourth Power: 81

pow() with range()

for number in range(1, 6):
    print(number, pow(number, 2))

Output:

1 1
2 4
3 9
4 16
5 25

pow() with List Comprehension

numbers = [1, 2, 3, 4, 5]

squares = [
    pow(x, 2)
    for x in numbers
]

print(squares)

Output:

[1, 4, 9, 16, 25]

pow() with Dictionary Comprehension

numbers = [1, 2, 3, 4, 5]

squares = {
    x: pow(x, 2)
    for x in numbers
}

print(squares)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Important Difference Between pow() and math.pow()

Python also provides math.pow().

import math

print(pow(2, 3))
print(math.pow(2, 3))

Output:

8
8.0

The built-in pow() can return an integer when appropriate, while math.pow() returns a floating-point number.

For example:

print(type(pow(2, 3)))
print(type(math.pow(2, 3)))

Output:

<class 'int'>
<class 'float'>

The built-in pow() is generally preferable when you need the optional third modulus argument.

Important Points to Remember

Basic power

pow(2, 3)
8

Square

pow(number, 2)

Cube

pow(number, 3)

Negative exponent

pow(2, -2)
0.25

Modular exponentiation

pow(2, 5, 3)
2

Equivalent to **

pow(2, 5)

and

2 ** 5

both produce:

32

Easy Way to Remember

pow(base, exponent)
        ↓
Calculate power

pow(base, exponent, modulus)
        ↓
Calculate power and remainder
Interactive Sandbox
Python