Python Program to Calculate the Power of a Number

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 SNIPPET
base = 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
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable base and exponent with the statements/strings ("Enter the base number: ") and ("\nEnter the exponent number: ")  , we use the int  function and declare the  input value as an integer value.
  2. We assign the variable result with the integer 1.
  3. Next, we declare a while  loop with the condition exponent != 0 followed by a :, after which we declare the condition result *= base and exponent-=1.
  4. In this code we use the while loop, where we multiply the result with the base until the exponent condition is satisfied that is it becomes 0.
  5. In the STDIN section of the code editor the input values are entered.
  6. The final output values are displayed with the statement ("\nThe Power of the number: " + str(result)) using the print function, where the + str(result)) returns and displays the string version of the result value.

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 SNIPPET
base = 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
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable base and exponent with the statements/strings ("Enter the base number: ") and ("\nEnter the exponent number: ")  , we use the int  function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the result variable to the function pow(base, exponent).
  4. The final output values are displayed with the statement ("\nThe Power of the number: " + str(result)) using the print function, where the + str(result)) returns and displays the string version of the result value.

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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...