What is the code for power in python?

In this example, you will learn to compute the power of a number.

To understand this example, you should have the knowledge of the following Python programming topics:

  • Python pow()
  • Python for Loop
  • Python while Loop

Example 1: Calculate power of a number using a while loop

base = 3
exponent = 4

result = 1

while exponent != 0:
    result *= base
    exponent-=1

print("Answer = " + str(result))

Output

Answer = 81

In this program, base and exponent are assigned values 3 and 4 respectively.

Using the while loop, we keep on multiplying the result by base until the exponent becomes zero.

In this case, we multiply result by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81.


Example 2: Calculate power of a number using a for loop

base = 3
exponent = 4

result = 1

for exponent in range(exponent, 0, -1):
    result *= base

print("Answer = " + str(result))

Output

Answer = 81

Here, instead of using a while loop, we've used a for loop.

After each iteration, the exponent is decremented by 1, and the result is multiplied by the base exponent number of times.

Both programs above do not work if you have a negative exponent. For that, you need to use the pow() function in the Python library.


Example 3: Calculate the power of a number using pow() function

base = 3
exponent = -4

result = pow(base, exponent)

print("Answer = " + str(result))

Output

Answer = 0.012345679012345678

pow() accepts two arguments: base and exponent. In the above example, 3 raised to the power -4 is calculated using pow().

What is the code for power in python?
Manish Ram

How to use the pow() function from the math module

  1. Import the math module as shown below in your Python environment.

  2. Use the pre-defined math.pow(number,exponent) function to find the power of the number.

import math
print(math.pow(4,2))

Importing math module in Python

How to find the power through iterationfor…loop

  1. Define the function, which takes two arguments:

    • number
    • exponent/power
  2. Find the power of a number: multiply the number with itself the same number of times as the exponent’s value. We use the for…loop to achieve this task and store the result in the res variable.

  3. Return the result stored in res, which is the required power of a number.

  4. Finally, display the output.

def power(n,e):
    res=0
    for i in range(e):
        res *= n
    return res
print(pow(4,2))

Finding Power of a Number using Iteration.

How to find the power through recursive function

  1. Define the function, which takes two arguments:

    • number
    • exponent/power
  2. If the exponent/power is 0, then return 1.

  3. If the exponent/power is 1 then return n, i.e., the number itself.

  4. Otherwise, multiply the current number and call the power(n,e) function recursively, with e (exponent/power) decremented by 1n*power(n, e-1) each time the function is called.

  5. Finally, display the output, which is the desired power of the number.

def power(n, e):
    if e == 0:
        return 1
    elif e == 1:
        return n
    else:
        return (n*power(n, e-1))
n = 4
p = 2
print(power(n, p))

Recursive function to find power of a number.

RELATED TAGS

python

program

math

communitycreator

CONTRIBUTOR

What is the code for power in python?
Manish Ram

❮ Built-in Functions


Example

Return the value of 4 to the power of 3 (same as 4 * 4 * 4):

x = pow(4, 3)

Try it Yourself »


Definition and Usage

The pow() function returns the value of x to the power of y (xy).

If a third parameter is present, it returns x to the power of y, modulus z.


Syntax

Parameter Values

ParameterDescription
x A number, the base
y A number, the exponent
z Optional. A number, the modulus

More Examples

Example

Return the value of 4 to the power of 3, modulus 5 (same as (4 * 4 * 4) % 5):

x = pow(4, 3, 5)

Try it Yourself »

❮ Built-in Functions


Is there a power function in Python?

The power function in Python can be used when one needs to derive the power of variable x to the variable y. If in a particular situation, the user includes a third variable that is z into the equation, then the pow() function returns x to the power of y, modulus of z.

How do you write to the power of 2 in Python?

To raise a number to the power of another number, you need to use the "**" operator. Where multiplying two numbers only uses one * symbol, the operator for raising one number to the power of another uses two: **.

How do you write a power code?

pow() is function to get the power of a number, but we have to use #include h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.