Power Function In Python Using A Loop

In this tutorial, I will explain the Power Function in Python using a loop through a step-by-step process. Nowadays, Python is the most eye-catching and popular programming language all over the world.

Moving on, Python has different host functions. It can be designed specifically to improve interface skills. One of the functions in Python is the power function, also defined as the pow() function.

Also read: Python Next Function (With Examples)

Parameters of the Power Function in Python

At this time, you are familiar with the basic python power function. And now we will explore the several parameters that are associated with the statement.

These are the three important parameters when you are using the power function in Python.

  • X represents the number value which is to be powered.
  • Y is represented by the number which is to be powered with the value of x.
  • Z is represented as an optional variable and is used to get the modulus of power of x and y.

Power of a number and a Logic

The power of a number represents the number of times it can be multiplied. However, power is expressed as a base number and an exponent.

While the logic lets us represent a limited variable power with a value of 1. We will use the data type long, which is to hold a big long value.

We will multiply the base by its power inside the loop, for example, base * power . To complete the loop, we will need to repeatedly store the loop in the variable power.

Example program:

import math


variable_base = int(input("Enter base : "))
variable_exponent = int(input("Enter exponent : "))

variable_power = 1

for i in range(1, variable_exponent + 1):
    variable_power = variable_power * variable_base

print("\nResult", variable_base, "^", variable_exponent, ":", variable_power)
print("Using math class :", math.pow(variable_base, variable_exponent))

The output of the example program above:

Enter base : Enter exponent :
Result 2 ^ 4 : 16
Using math class : 16.0

Try to run for free online using the example above.

</iframe
Note: You must fill in the "Stdin Inputs" to run the example code successfully. For example, if you enter 2, then enter 4, and click execute, it will not give an error, yet it will show the successful result.

How to use a loop in Python to find the power of a number?

We will first use the for loop. A complete use of the for loop to find the power is shown in the program example below. 

n = int(input("Enter the number: "))
pow = int(input("Enter the power: "))

res = 1
for i in range(pow):
    res *= n

print("Result : {}".format(res))

The output of the above example program

Enter the number:3
Enter the power:5
Result : 243

Code explanation:

  • n” is represented as a number. We are looking at its usefulness as user input.
  • “Pow” means power. We are also looking into its use as input from a user.
  • “Res” is presented as a result. To store the last result and have it initialized as 1.
  • A “for loop”  is used to compute the last result. It multiplied the value of the result with “n” and allocated that value to the result.
  • If the for loop ends, it will print the value of the result.

Also, you can try to run the example program for free online.

</iframe
Note: To run the example program online, make sure that you put a number in "Stdin Inputs". For example, enter 3, then enter 5, and click execute. Because if you're not putting a number in "Stdin Inputs", it will show the error.

Frequently ask Question

How do you write a power function in Python?

The python pow() function can return the value of A to the power of a value of B. Once the third arguments which is C in the given. The A will return the power B modulus C. For example pow(A, B)% Z.

How do you do powers in Python?

The Python ** operator is used to increase the number on the left to the power of the exponent on the right. For example, in the expression 8 ** 2, then 8 should be raised to the 2nd power.

Conclusion

The Power Function in Python uses a loop. When you use it correctly, it can remove a lot of problems and doubt in your mind. I hope that through this tutorial you have learned Power Function in Python using a Loop correctly.

Leave a Comment