Exponentiation is a process in Mathematics in which a number is multiplied a specified number of times with itself. We called the operation as ‘ a raised to the nth power.’ The number that is multiplied certain times is called the base ,i.e., a, and the number that specifies how many times it is to be multiplied is known as exponent .i.e. n
Python offers several distinct ways to calculate the exponent or power of a value. Python has the following methods to carry out.
- Double asterisk **
- Pow( ) Function
- Math.pow( ) Function
- Exp( ) Function
- Numpy.ny( )
In this article, we will discuss the five mentioned ways to calculate the exponent in Python. There is an example code snippet for each technique along with the output for better understanding.
Note: Python also offers the methods to round off a number.
Table of Contents
1. Double Asterisk **
You can use the Python asterisk ** operator. This operator takes two values just like we do simple multiplication. It is a shortcut for calculating exponent. Let’s look at the following examples.
exp1 = 6**5 print("The exponent of 6**5 = ",exp1) float_exp2 = 2.6**3 print("The exponent of 2.6**3 = ",float_exp2) float_exp3 = 5**2.5 print("The exponent of 5**2.5 = ",float_exp3) negative_exp4 = 25**-4 print("The exponent of 25**-4 = ",negative_exp4)
2. Pow( ) Function
Pow( ) is a built-in power function for calculating the exponent value in Python. It takes two values as arguments. If there is one more parameter, then it returns modulus. The syntax is given as:
pow (base, exponent)
Here is the code as an example:
pow1 = pow(6, 5) print("For pow(6, 5) = " ,pow1) pow2 = pow(2.6, 3) print("For pow(2.6, 3) = " ,pow2) pow3 = pow(5, 2.5) print("For pow(5, 2.5) = " ,pow3) pow3 = pow(-6, 3) print("For pow(-6, 3) = " ,pow3) pow4 = pow(25, -4) print("For pow(25, -4) = " ,pow4)
Modulus with 3rd Parameter:
pow1 = pow(6, 5, 8) print("For pow(6, 5, 8) = " ,pow1) pow2 = pow(10, 3, 2) print("For pow(10, 3, 2) = " ,pow2)
3. Math. pow( ) Function
The math library in Python offers pow( ) implementation on its own to calculate exponent in Python. Two arguments are passed in the function, one is for the base, and the other is for the exponent. Let’s look at the example:
import math math_pow1 = math.pow(5, 4) print("5 raised to the power 4 = " ,math_pow1) math_pow2 = math.pow(2.5, 3) print("2.5 raised to the power 3 = " ,math_pow2) math_pow3 = math.pow(6, 5.5) print("6 raised to the power 5.5 = " ,math_pow3)
4. Exp( ) Function
This function is used for calculating the exponent with the base ‘e’.
e is a mathematical constant. First, we will import math module. The syntax is:
import math math.exp (exponent)
And the example code:
import math math_exponent1 = math.exp(4) print("The result of e raised to power 4 = " ,math_exponent1) math_exponent2 = math.exp(16) print("The result of e raised to power 16 = " ,math_exponent2)
5. Numpy.ny( )
import NumPy as np numpy_exponent1 = np.power(6,7) print("The result of 6 raised to power 7 = " ,numpy_exponent1 ) numpy_exponent2 = np.power(9.5,2) print("The result of 9.5 raised to power 2 = " ,numpy_exponent2 ) numpy_exponent3 = np.power(2,4.5) print("The result of 2 raised to power 4.5 = " ,numpy_exponent3 )
Python offers different techniques to calculate the exponential value. This article has explained those techniques with easy example code snippets. I hope you will find it helpful in your development practice.