In this tutorial, we will learn how to compute the power of a number in Python. We will cover the basic concept of exponentiation and implement a function to perform the calculation.
Exponentiation is a mathematical operation that involves raising a base number to the power of an exponent. For example, 2 raised to the power of 3 is 23 = 8.
The syntax to compute the power of a number in Python is:
def compute_power(base, exponent):
return base ** exponent
We can create a function to compute the power of a given base and exponent using the exponentiation operator (**).
For example,
compute_power
that takes two parameters base
and exponent
.**
) to raise the base to the power of the exponent.def compute_power(base, exponent):
return base ** exponent
# Sample values
base = 2
exponent = 3
# Compute the power
result = compute_power(base, exponent)
# Print the result
print(f'{base} raised to the power of {exponent} is:', result)
2 raised to the power of 3 is: 8