Python Program to Compute the Power of a Number


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

Computing the power of a number involves raising a given number to a specified exponent, which is a positive integer. The result of this operation is a new number that represents the original number multiplied by itself a certain number of times, based on the exponent.


Python Code :

You can use the ** operator in Python to compute the power of a number. Here is an example program that demonstrates this:

base = 2
exponent = 3

# Compute the power of the number
result = base ** exponent

# Print the result
print(f'{base} raised to the power of {exponent} is {result}')

In this program, we first define the base and exponent (in this example, 2 and 3, respectively). We then use the ** operator to compute the power of the number. This operator raises the base to the exponent.

We store the result in a variable called result. Finally, we print the result using the print() function.

Note that the ** operator can be used with any numerical type in Python, including integers, floating-point numbers, and complex numbers. If the exponent is negative, the result will be a reciprocal (i.e., base ** -exponent = 1/(base ** exponent)). If the exponent is a floating-point number, the result will be a floating-point number. If the base is a negative number and the exponent is a fractional number, the result will be a complex number.


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co