In Python, the pow()
function is used to calculate the power of a number. It takes two or three arguments and returns the result of the exponentiation operation.
The syntax of the pow()
function is as follows:
pow(x, y[, z])
Here, x
is the base number, y
is the exponent, and z
(optional) is the modulo value. The function calculates x**y
or x
raised to the power of y
. If the optional z
argument is provided, it calculates (x**y) % z
, which is the remainder when x**y
is divided by z
.
Let's see some examples:
pow()
result = pow(2, 3)
print(result) # Output: 8 (2 raised to the power of 3)
result = pow(5, 2)
print(result) # Output: 25 (5 raised to the power of 2)
z
result = pow(2, 10, 3)
print(result) # Output: 1 (2 raised to the power of 10, and then modulo 3)
result = pow(3, 4, 5)
print(result) # Output: 1 (3 raised to the power of 4, and then modulo 5)
Keep in mind that the pow()
function is available in both Python 2 and Python 3. In Python 3, you can also use the **
operator for exponentiation, which is more commonly used for simple power calculations.
result = 2 ** 3
print(result) # Output: 8 (2 raised to the power of 3)
However, the pow()
function can be more useful when you need to use the optional modulo z
.