Exponential is a mathematical operation in which a number is multiplied by itself up to a certain time. It is also known as the power of something. Let’s discuss how we can use the Python math operations and inbuilt function and method to find the exponent of a number.
How to Calculate Exponents of a Number in Python Programming?
An exponent of a number is defined by multiplying the number itself up to a certain time. In mathematics, we represent exponent like this - a n , where a will be multiplied by itself n times. In Python, we can use the following three methods to find the exponential value of a number:
- By using the ** operator.
- Python built-in pow() method.
- math.pow() method.
1. Calculate the exponent value of a number using the Python ** operator
symbol represents the exponential operator or Power operator. This operator needs two values to operate. The value on the left side represents the number whose exponential value we need to find out, and the value on the right side represents the power of the value.
a ** n
Python exponent operator examples
print(3**3) # this is equal to 3 *3 *3 print(2**3) # this is equal to 2 * 2 *2 print(5**2) # this is equal to 5*5
27 8 25
2. Calculate the exponent value of a number using the Python pow() method
Python provides an inbuilt
method to find out the exponential value of a number. The
method accepts two values as parameters
, and it returns a numeric value by raising the value
to the power
The above syntax represents a n
Python pow() method examples
print(pow(2,3)) # 2*2*2 print(pow(3,4)) # 3*3*3*3 print(pow(4, 2)) #4*4
8 81 16
3. Calculate the exponent value of a number using Python math.pow() method
math is a Python inbuilt module that is inherited from the C programming language. The math module also contains a
method that works similar to that of the Python
method. However, the only difference is that the
method always returns a
method can return integers as well as floating-point values, it depends on the passed parameters.
The above syntax represents a n Python math.pow() method examples
import math print(math.pow(2,3)) # 2.0*2.0*2.0 print(math.pow(3,4)) # 3.0*3.0*3.0*3.0 print(math.pow(4, 2)) #4.0*4.0
8.0 81.0 16.0
Calculate the Exponential Value of a List
If you have a list of numbers and you want to find the square or cube value for every number present in the list, you can do that by using the
Calculating the exponential value of a list using the Python pow() method
my_list = [2,4,23, 67,34,84,47] #square every number sq_list = list(map(lambda a:pow(a,2), my_list)) print(sq_list)
[4, 16, 529, 4489, 1156, 7056, 2209]
Calculating the exponential value of a list using Python math.pow() method
import math my_list = [2,4,23, 67,34,84,47] sq_list = list(map(lambda a:math.pow(a,2), my_list)) print(sq_list)
[4.0, 16.0, 529.0, 4489.0, 1156.0, 7056.0, 2209.0]
Difference Between Python pow() and math.pow() Methods
While both the methods are similar, the only difference is that the
method returns an integer value if the output is supposed to be an integer. But the
method will always return a Floating point number.
|Python pow()||Python math.pow()|
>>> pow(2,3) 8 pow() returned an integer value 8
>>> math.pow(2,3) 8.0 math.pow() returned a floating value 8.0
>>> pow(2, 0.5) 1.4142135623730951
>>> math.pow(2, 0.5) 1.4142135623730951
In this article, we have discussed the different Python methods to find out the exponent of a number. The exponent operator
methods are the same and return the same value. On the other hand, the
function always returns a floating-point number. Choosing a method to find the exponent of a number entirely depends on your preferences.
People are also reading: