In this python tutorial, you will learn how to Calculate the Power of a Number using the while loop and the pow() function along with the different operators of the python programming language.
How to Calculate the Power of a Number?
Let’s take a look at the first source code , here the values are given as input by the user in the code, the operators and while loop along with the assignment operators carry out the function.
RUN CODE SNIPPETbase = int(input("Enter the base number: "))
exponent = int(input("\nEnter the exponent number: "))
result = 1
while exponent != 0:
result *= base
exponent-=1
print("\nThe Power of the number: " + str(result))INPUT:
3 4
OUTPUT:
Enter the base number: Enter the exponent number: The Power of the number: 81
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variablebaseandexponentwith the statements/strings("Enter the base number: ")and("\nEnter the exponent number: "), we use theintfunction and declare the input value as an integer value. - We assign the variable
resultwith the integer1. - Next, we declare a
whileloop with the conditionexponent != 0followed by a:, after which we declare the conditionresult *= baseandexponent-=1. - In this code we use the while loop, where we multiply the
resultwith thebaseuntil theexponentcondition is satisfied that is it becomes0. - In the STDIN section of the code editor the input values are entered.
- The final output values are displayed with the statement
("\nThe Power of the number: " + str(result))using theprintfunction, where the+ str(result))returns and displays the string version of theresultvalue.
Let’s take a look at the second source code , here the values are given as input by the user in the code, the pow() function along with the print statement carry out the function.
RUN CODE SNIPPETbase = int(input("Enter the base number: "))
exponent = int(input("\nEnter the exponent number: "))
result = pow(base, exponent)
print("\nThe Power of the number: " + str(result))
INPUT:
4 3
OUTPUT:
Enter the base number: Enter the exponent number: The Power of the number: 64
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variablebaseandexponentwith the statements/strings("Enter the base number: ")and("\nEnter the exponent number: "), we use theintfunction and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- We declare the
resultvariable to the functionpow(base, exponent). - The final output values are displayed with the statement
("\nThe Power of the number: " + str(result))using theprintfunction, where the+ str(result))returns and displays the string version of theresultvalue.
NOTE:
- The While true loop in python runs without any conditions until the break statement executes inside the loop.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The str() function in python returns the string version of the object.
- The != operator is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal.
- The *= multiplication assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable.
- The -= subtraction assignment operator subtracts right operand from the left operand and assigns the result to left operand.
- The pow() function of the python programming language returns the value of x to the power of y (xy).
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The print statement/string to be displayed in enclosed in double quotes.
- The input() function allows a user to insert a value into a program, it returns a integer value.
- The statement for the input function are enclosed in single quotes and parenthesis.