Learn Programming and technology with ease @ developerpublish.com

HomePythonPython Program to Calculate the Power of a Number

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.

```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.

```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.

You May Also Like

Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...