Python

Python power operator

The power of a number can be calculated in python by using the loop, pow() function, and the power operator(**). The use of a power operator(**) is the simplest way to calculate the power of a number in python. The different uses of this operator have shown in this tutorial.

Use Power Operator in Python:

The power operator is defined by the ‘**’ symbol. It is used between the base and the power values. If the base value is defined by x and the power value is defined by n, then the power operator is used in the following way to calculate the xn.

x**n

Power Operator Examples:

Different uses of power operators in python have been shown in this part of the tutorial.

Example-1: Simple use of power operator

Create a python file with the following script to check the use of the power operator to calculate 34 in python. Here, the base value is set to 3, and the power value is set to 4. Next, the output of the 34 has been printed.

# Set the base value

baseVal = 3

# Set the power value

powerVal = 4

# Calculate the result using power operator

output = baseVal ** powerVal

# Print the result

print("{} to the power {} is {}.".format(baseVal, powerVal, output))

Output:

The following output will appear after executing the above script.

Example-2: Calculate the power with error handling

Create a python file with the following script to take base and power values from the user and calculate the power value after validating the input values. The isdigit() function has been used in the script to check the validity. The isdigit() function will return True for the valid base and power values. The error message will be printed if the base or power value is invalid.

# Take the base value

baseVal = input("Enter the base value: ")

# Take the power value

powerVal = input("Enter the power value: ")

# Check the input values are number or not

if baseVal.isdigit() == True and powerVal.isdigit() == True:

# Calculate the result using power operator

output = int(baseVal) ** int(powerVal)

# Print the result

print("{} to the power {} is {}.".format(baseVal, powerVal, output))

else:

# Print the error message

print("The base or power value is incorrect.")

Output:

After executing the above script for the valid data, the following output will appear. Here, 4 is the base value, and 3 is the power value.

After executing the above script for the invalid data, the following output will appear. Here, 6 has been given as the base value, and ‘f‘ is the invalid power value.

Example-3: Testing user answer using power operator

Create a python file with the following script to check the answer taken from the user is correct or incorrect by using the power operator. The answer of the 25 will be asked to the user after executing the script. The answer to this question has been calculated by using the power operator in the script. Next, the calculated value and the answer given by the user will be checked, and the appropriate message will be printed based on the returned value of the ‘if’ statement.

# Set the base value

baseVal = 2

# Set the power value

powerVal = 5

# Calculate the result using power operator

value = baseVal ** powerVal

# Take input from the user

ans = int(input("What is the value of 2 to the power 5?\n"))

# Check the answer is correct or incorrect

if ans != value:

print("Sorry, your answer is incorrect.")

else:

print("Congrats, your answer is correct.")

Output:

The following output will appear after executing the above script if the answer given by the user is correct.

The following output will appear after executing the above script if the answer given by the user is incorrect.

Conclusion:

The uses of power operators based on pre-defined or input values have shown in this tutorial by using simple python scripts.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.