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 can we 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. And in mathematic we represent exponent like this an where a will be multiplied by itself n times.
In Python, there are three methods that can be used to find the exponential value of a number.
- Using the ** operator
- Using the Python built-in pow() method.
- Using the math.pow() method.
Calculate the exponent value of a number using Python ** Operator
** symbol represents the exponential operator or Power operator. This operator needs two values to operate. The value on the left side represent the number which exponential value we need to find out, and the value on the right represent the power of the value.
a ** n
Python exponent operator example
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
Calculate the exponent value of a number using the Python pow() method
Python provides an inbuilt
pow() method, which can be used to find out the exponential value of a number. The
pow(a, n) method accepts two values as parameter
n, and return a numeric value by raising the value
a to the power
the above syntax represents an
Python pow() method Example
print(pow(2,3)) # 2*2*2 print(pow(3,4)) # 3*3*3*3 print(pow(4, 2)) #4*4
8 81 16
Calculate the exponent value of a number using Python math.pow() method
math is a Python inbuilt module which is inherited from the C programming language. The math module also contains
pow() method that work similar to the Python
pow() method. The only difference is the
math.pow() method always returns a float value whereas the
pow() method can return both integer as well as a floating-point value, it depends on the passed parameters.
the above syntax represents an
Python math.pow() method Example
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, using the
math.pow() method we can do that with a single line of code.
Example exponential value of a list using 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]
Example 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
Both the methods are the same the only difference is the
pow() method will return an integer value if the output is supposed to be an integer. But the
math.pow() 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
Here in this article, you learned how can we use different python methods to find out the exponent of a number in Python. The exponent operator
pow() method are the same and returns the same value whereas the
math.pow() function always returns a floating-point number. It’s on you what method you prefer using when finding the power of a number.
People are also reading: