Python Power | pow() | Python Power Operator

The power (or exponent) of a number says how many times to use the number in a multiplication. Calculating the power of any number is a vital job. Because the power of a number can be needed in many mathematics as well as in big projects. It has many-fold applications in day to day programming. Calculating Python Power is easy and can be done in a few lines. Python comes with a host of different functions each built specifically to add more versatility to the interface than before.

To calculate the power of a number in Python we have basically three techniques or methods. The first one in the Python ecosystem is the power function, also represented as the pow(). The second way is to use the Power Operator. Represented or used as ** in Python. And the third one is using loops or iteration. In this article, we will discuss the Power function, Calculating the power of a number using loops and Power Operator in Python.

What is Power pow() Function in Python?

One important basic arithmetic operator, in Python, is the exponent operator. It takes in two real numbers as input arguments and returns a single number. Pow can be used with 3 arguments to do modulo division.

Syntax of pow() Function

pow(x, y, z)

Parameters

The pow() function in Python takes three parameters:

  • x – a number, the base
  • y – a number, the exponent
  • z (optional) – a number, used for modulus

Meaning / Usage of x,y,z in pow()

The x,y,z parameters in pow() function can be used like the following.

  • pow(x, y) is equal to xy
  • pow(x, y, z) is equal to xy % z
  1. X: X can either be a non-negative integer or a negative integer whenever it is being used. 
  2. Y: Y can also be a non-negative integer or a negative integer when used in the equation.
  3. Z: In most cases, z is an optional variable and may or may not be present.

Note: Here the third parameter in pow() is optional. So you can use the pow() function with only two parameters. And in place of x,y,z you can use any variable.

Return Value and Implementation Cases in pow() Function

pow() function

Examples to Calculate Python Power using pow() Function

In the following examples, we will calculate the Python Power of a number using pow() function.

Let’s Start.

Example 1: Basic Example to calculate the power of a number using pow() function

# taking x and y oth as positive
print(pow(5, 2))

# taking x as negative and y as positive
print(pow(-5, 2))

# taking x as positive and y as negative
print(pow(5, -2))

# taking x as negative and y as negative
print(pow(-5, -2))

Output:

25
25
0.04
0.04

Example 2: Using a floating number to calculate power of a number:

pow1 = pow(5,2.5)
 
pow2 = pow(8,2.5)
 
pow3 = pow(9,2.5)
 
print("The exponent of 5**2.5 = ",pow1)
 
print("The exponent of 8**2.5 = ",pow2)
 
print("The exponent of 9**2.5 = ",pow3)

Output:

The exponent of 5**2.5 =  55.90169943749474
The exponent of 8**2.5 =  181.01933598375618
The exponent of 9**2.5 =  243.0
Using a floating number to calculate power of a number.

Example 3: Using the third argument (modulo) in pow() function

It’s also possible to calculate  a^b mod m.

>>> pow(a,b,m)  

This is very helpful in computations where you have to print the resultant % mod.

Note: Here, a and b can be floats or negatives, but, if a third argument is present, b cannot be negative.

Python program that uses pow with 3 arguments

x = 12
y = 2
z = 5
 
print(pow(x, y, z))

Output:

4

Explanation:

So in the above example, we have three parameters x,y and z. Here x is the base number. The y variable is the exponential or power of a number and z is the modulus.

The above example can be written in simple mathematics as

12^2%5 which is equal to 4

Example 4: Basic Example to calculate the power of a number using math.pow() function

Here in this example, we have to import the math module to calculate the power of a number.

import math

print(math.pow(5,2))

Output:

25.0

Note: Python has a math module that has its own pow(). It takes two arguments and returns a floating-point number. Frankly speaking, we will never use math.pow().

What is Python Power Operator

In Python, power operator (**) and pow() function are equivalent. So we can use whichever one seems clearest. I feel pow is clearer, but I rarely need exponents in programs.

** operator used as a power(exponent) operator in python.

Raising a number to a power N multiplies the number by itself N times. For instance, 2 raised to the power of 3 is equal to 2 × 2 × 2 = 8.

How to Use Power Operator in Python

To use the power operator (**) in Python, We have to put (**) between two numbers to raise the former number to the latter.

Syntax

a**b

Here a is Any expression evaluating to a numeric type. And b any expression evaluating to a numeric type.

Examples to Use the Power Operator in Python

#Python power operator example
#Change the below value and try it yourself.

a = 25
b = 3

result = a ** b

print(result)

Output:

15625

Examples to Use the Power Operator in Python

Note:

The python power operator accepts any numeric data type, such as an int or a floating-point number.

Calculating Python Power of a Number Using Loops

In this method to calculate the power of a number, we are using the for loop.
This method or way is also known as the naive method to compute power.

This Python program allows the user to enter any numerical value, exponent. Next, this Python program finds the power of a number using For Loop.

Program to calculate power of a number using for loop.

# Python Program to find Power of a Number using for loop

number = int(input(" Please Enter any number : "))
exponent = int(input(" Enter Exponent Value : "))
power = 1

for i in range(1, exponent + 1):
    power = power * number
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))

Output:

Please Enter any number : 20
Enter Exponent Value : 2
The Result of 20 Power 2 = 400

This Python power of a number program using the while loop will be the same as above, but this time, we are using  While Loop instead of for loop.

Must Read

How to Convert String to Lowercase in Python
How to Calculate Square Root in Python
Python User Input | Python Input () Function | Keyboard Input
Best Book to Learn Python in 2020

Remarks

Python defines pow(0, 0) and 0 ** 0 to be 1, as is common for programming languages.

Conclusion

So we have covered all the possible ways or methods to calculate the Python Power of a number. Power function in Python, when used correctly can eliminate a lot of stress and confusion. We hope that from this article you have learned how to use the power function in Python correctly and thus will make use of the same in your day to day programming.

If you still have any doubts or suggestions regarding the python power do let us know in the comment section below.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments