Notes

`pow()` Function in Python [ English ]

< Prev Next >

1. Introduction

The pow() function in Python is a built-in function used to calculate the power of a number. It raises a number (called the base) to the power of another number (called the exponent).

In simple terms, pow(a, b) calculates:

[ a^b ]

The function can also take a third argument for modular exponentiation, which is commonly used in cryptography and number theory.

2. Syntax

Syntax 1: Two arguments

pow(base, exponent)

Syntax 2: Three arguments

pow(base, exponent, modulus)

3. Parameters

  1. base The number to be raised to a power.

  2. exponent The power to which the base is raised.

  3. modulus (optional) A number used to compute the remainder after exponentiation.


4. Return Value

[ (base^{exponent}) \mod modulus ]

5. Basic Example

print(pow(2, 3))

Output

8

Explanation

[ 2^3 = 8 ]

6. Example with Larger Numbers

print(pow(5, 4))

Output

625

Explanation

[ 5^4 = 625 ]

7. Example with Floating-Point Numbers

print(pow(4, 0.5))

Output

2.0

Explanation

[ 4^{0.5} = \sqrt{4} = 2 ]

8. Example with Modulus

print(pow(2, 5, 3))

Output

2

Explanation

First compute the power:

[ 2^5 = 32 ]

Then apply modulus:

[ 32 \mod 3 = 2 ]

9. Difference Between pow() and ** Operator

Feature pow() ** Operator
Purpose Power calculation Power calculation
Syntax pow(a, b) a ** b
Modulus support Yes (pow(a, b, m)) No

Example:

print(pow(3, 2))
print(3 ** 2)

Output:

9
9

10. Practical Example

side = 5

area = pow(side, 2)

print("Area of square:", area)

Output

Area of square: 25

Explanation

[ side^2 ]

11. Summary

< Prev Next >