Use of pow() function:
The syntax of the pow() function has given below.
Syntax:
Here, the parameter x contains the base value of the number, and the parameter y contains the power value. The pow() function returns the value of xy. The values of x and y can be positive or negative. The optional parameter, z, is used to calculate the value of xy % z. The return value of this function will be float only if the value of y is negative and the return value is an integer for all other cases.
Example-1: Use of pow() function with mandatory arguments
Create a python file with the following script to calculate the power using pow() function based on the base and power values taken from the user. The try-except block has been used in the script to handle errors.
# Take the base value from the user
x = int(input("Enter the base value: "))
# Take the power value from the user
n = int(input("Enter the power value: "))
# Print the result
print("%d to the power %d = %0.2f" % (x, n, pow(x, n)))
except:
# Print the error message
print("The value of base or power is not a number")
Output:
The following output will appear after executing the above script with the input values 3 and 5.
The following output will appear after executing the above script with the input values 2 and -3.
The following output will appear after executing the above script with the input values -2 and 4.
The following output will appear after executing the above script with the input values -3 and -3.
Example-2: Use of pow() function with mandatory and optional arguments
Create a python file with the following script to calculate the power and the modulus value of the power using pow() function based on the base and power values taken from the user. The try-except block has been used here also to handle errors like the previous example.
# Take the base value from the user
x = int(input("Enter the base value: "))
# Take the power value from the user
n = int(input("Enter the power value: "))
# Print the result
print("%d to the power %d = %0.2f" % (x, n, pow(x, n)))
# Print the remainder value
print("The remainder value = ", pow(x, n, 5))
except:
# Print the error message
print("Error exists in the script.")
Output:
The following output will appear after executing the above script with the input values 2 and 3. Here, the 23 is 8, and 8%5 is 3.
Use of math.pow() function:
The math module will require to import for using the math.pow() function to calculate the power. This function can take two arguments only. The first argument takes the base value, and the second argument takes the power value.
Example-3: Use of math.pow() function to calculate power
Create a python file with the following script to calculate the power using math.pow() function based on the base and power values taken from the user. The try-except block has been used in the script like the previous examples to handle errors.
import math
try:
# Take the base value from the user
x = int(input("Enter the base value: "))
# Take the power value from the user
n = int(input("Enter the power value: "))
# Print the result using math.pow()
print("%d to the power %d = %0.2f" % (x, n, math.pow(x, n)))
except:
# Print the error message
print("Error exists in the script.")
Output:
The following output will appear after executing the above script with the input values 4 and 5.
Conclusion:
The uses of python power functions have been explained in this tutorial to know the purpose of using pow() and math.pow() functions in python.