Introduction to Python Power Function
Python’s Power function makes exponentiation operations very straightforward. Similarly known as the technique of exponentiation in mathematics. Let us first examine the mathematical understanding behind the Exponentiation.
method.
Methods in Exponentiation
Exponentiation is a mathematical process involving the two integers x and y, where x represents the base and y represents the exponent or power. With various values of y, the meaning of exponentiation varies.
-
y>0:
When y is positive, then the result of exponentiation would be repeated multiplication of the base. Example: – 2
4
= 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: – 2
0
= 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’ve examined the mathematical idea behind the Power Function, let’s move on to its programming side.
it.
Power Function in Python
Programming as it exists now has evolved since its conception. When the complexity of programming logic increases over time, it becomes vital to understand all calculating techniques. In our programmes, we often need to determine the power of a number, and it is in these instances that knowledge of this function comes in helpful.
Consider a Python example of a user-defined power function. Code:
base = int(input("Enter base:"))
Output:
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)
Description:
- 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
|
Y |
Z |
|
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 |
With Python’s Exponent Arithmetic Operator, the above line of code may be simplified. The Exponent Arithmetic Operator (**) facilitates the Exponentiation procedure. Example: – 2**3 = 8
However the power function was invented to provide the exponentiation process greater flexibility. The syntax for the power function is shown below. Syntax:
pow(x, y[, z])
Parameters
Let us discuss the power’s specifications.
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 parameter distinguishes and strengthens the pow function. The return values for the various combinations of x,y, and z values that are permitted by the pow function are listed below.
types:
Example #1
Nonnegative x and y integers, except z Code.
base = 3
Output:
power = 4
result = pow(base,power)
print(base,"raised to the power of",power,"gives",result)
Example #2
x is positive and y is negative, but there is no z. Code:
base = 3
Output:
power = -4
result = pow(base,power)
print(base,"raised to the power of",power,"gives",result)
Example #3
Negative x and positive y, but no z Code:
base = -3
Output:
power = 4
result = pow(base,power)
print(base,"raised to the power of",power,"gives",result)
Example #4
Without z, Negative x and Negative y. Code:
base = -3
Output:
power = -4
result = pow(base,power)
print(base,"raised to the power of",power,"gives",result)
Example #5
x and y are nonnegative when combined with z. Code:
base = 5
Output:
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
There are three other techniques for calculating the power, in addition to the one stated above.
function.
1. Using the pow function of Math Package
Code:
import math
Output:
base = 2
power = 5
print(math.pow(base,power))
2. Using the power function of the Numpy Package
Code:
import numpy as np
Output:
base = 2
power = 5
print(np.power(base,power))
3. Using the power function of the Scipy Package
Code:
import scipy
Output:
base = 2
power = 5
print(scipy.power(base,power))
0