Updated April 1, 2023
Introduction to Python Power Function
Power function in Python helps us to perform exponentiation operation with relative ease. In mathematical terminology is also known as the method of exponentiation. Let us first look at the mathematical intuition of the Exponentiation method.
Methods in Exponentiation
Exponentiation is a mathematical operation that involves two numbers in the following format xy, where x corresponds to the base and y represents the exponent or power. For different values of y, we get the different meaning of exponentiation:
- y>0: When y is positive, then the result of exponentiation would be repeated multiplication of the base. Example: – 24 = 2*2*2*2 = 16 (the base i.e 2 multiplied repeatedly exponent i.e. 4 number of times)
- y=0: When y is 0, then the result of the exponentiation would be 1. Example: – 20 = 1
- y<0: When y is negative, then the result of the exponentiation would be the repeated division of the base. Example: – 2-2 = ¼
Now that we have discussed the Power Function’s mathematical intuition let us come to the programming aspect of it.
Power Function in Python
Programming as we know it has developed over time since its inception. Day by day, the programming logic is getting complex, so it becomes very crucial to know all the calculation methods. Very frequently, we require to find the power of a number in our program, and it is in this scenario that the knowledge of this power function comes in handy.
Let us see an example of a user-defined power function in Python.
base = int(input("Enter base:")) power = int(input("Enter power:")) n = 1 for i in range(1,power+1): n=base*n print ("The value of",base,"**",power," is",n)
- First Iteration i.e. n=1: n = base*n = 2*1 = 2
- Second Iteration i.e. n=2: n = base * n = 2*2 = 4
- Third Iteration i.e. n=3: N = base * n = 2*4 = 8
|Non-negative Integer||Non-negative Integer||N/A||Integer|
|Non-negative Integer||Negative Integer||N/A||Float|
|Negative Integer||Non-negative Integer||N/A||Integer|
|Negative Integer||Negative Integer||N/A||Integer|
|Negative/Non-negative Integer||Non-negative Integer||Negative/Non-negative Integer||Integer|
The above piece of code can be made simple by using the Exponent Arithmetic Operator in Python. The Exponent Arithmetic Operator (**) helps us to perform the Exponentiation operation.
Example: – 2**3 = 8
But to give more flexibility to the exponentiation operation, the power function was introduced. The following is the syntax of the power function.
pow(x, y[, z])
Let us discuss the parameters of the power function:
- x: x denotes the base number
- y: y denotes the exponent value
- z: z is an optional variable and is used to derive the modulus of the power of x and y.
Examples to Implement Power Function
The third argument makes the pow function unique and robust. The following are the different combinations of values of x,y, and z that are permissible in pow function with their respective return types:
Non-negative x and y integers without z
base = 3 power = 4 result = pow(base,power) print(base,"raised to the power of",power,"gives",result)
Non-negative x and negative y without z
base = 3 power = -4 result = pow(base,power) print(base,"raised to the power of",power,"gives",result)
Negative x and non-negative y without z
base = -3 power = 4 result = pow(base,power) print(base,"raised to the power of",power,"gives",result)
Negative x and Negative y without z
base = -3 power = -4 result = pow(base,power) print(base,"raised to the power of",power,"gives",result)
Non-negative x and y with z
base = 5 power = 3 modulus = 10 result = pow(base,power,modulus) print(base,"raised to the power of",power,"and thereby applying the modulus on",modulus,"gives",result)
Other Power Functions in Python
Other than the method described above, there are three other methods with the help of which we can calculate the power function.
1. Using the pow function of Math Package
import math base = 2 power = 5 print(math.pow(base,power))
2. Using the power function of the Numpy Package
import numpy as np base = 2 power = 5 print(np.power(base,power))
3. Using the power function of the Scipy Package
import scipy base = 2 power = 5 print(scipy.power(base,power))
We finally come to the closure of this article. This article discussed the mathematical intuition of the exponentiation function, a basic approach towards power function in Python, use of an Exponent Arithmetic Operator for finding power. Then, we discussed the pow function in Python in detail with its syntax. Finally, we signed off the article with other power functions that are available in Python. Phew!! Now it is time to practice the concepts learned from today’s session and start coding. Happy Coding!!!
This is a guide to Python Power Function. Here we discuss methods in exponentiation and the example of power functions for better understanding. You can also go through our other related articles to learn more –