math.exp() method is a library method of math module, it is used to get the number in exponential form, it accepts a number and returns the number in the exponential format (if the number is x, it returns e**x.
math.exp(n)
n
– an integer or a float number.The math.exp() function returns a floating type number by calculating e**n (e^n). This function returns a TypeError if the given input is not a number.
# math.exp() method
import math
# numbers
a = 0
b = 245
c = -102.34
d = 111.111
e = -123.456
# printing values in exp format
print("exp(a): ", math.exp(a))
print("exp(b): ", math.exp(b))
print("exp(c): ", math.exp(c))
print("exp(d): ", math.exp(d))
print("exp(e): ", math.exp(e))
Output
exp(a): 1.0
exp(b): 2.5243412626998188e+106
exp(c): 3.583461328080821e-45
exp(d): 1.7984326512709283e+48
exp(e): 2.4195825412645934e-54
Pass string as an argument in Python exp()
import math
# When the given number is not a number
n = "546"
print("Value of e^n: ", math.exp(n))
Output
Traceback (most recent call last):
File "exp2.py", line 6, in <module>
print("Value of e^n: ",math.exp(n))
TypeError: must be real number, not str
In this program, we have initialized the value of n a string. As the value of n is not a number, we got one a TypeError.
Python exp() with list and tuple.
Let’s take Python list and tuple and pass any items of list and tuple to the exp() function.
import math
Tup = (1.21, 19.26, 13.05, -40.95, 0.45) # Tuple Declaration
Lis = [-11.21, 3.64, -9.59, -4.15, 5.97] # List Declaration
print('Python EXP() Function on Positive Number = %.2f' % math.exp(1))
print('Python EXP() Function on Negative Number = %.2f' % math.exp(-1))
print('Python EXP() Function on Tuple Item = %.2f' % math.exp(Tup[2]))
print('Python EXP() Function on List Item = %.4f' % math.exp(Lis[2]))
print('Python EXP() Function on Multiple Number = %.4f' %
math.exp(11 + 19 - 15.64))
print('Python EXP() Function on String Number = ', math.exp('Python'))
Output
Python EXP() Function on Positive Number = 2.72
Python EXP() Function on Negative Number = 0.37
Python EXP() Function on Tuple Item = 465096.41
Python EXP() Function on List Item = 0.0001
Python EXP() Function on Multiple Number = 1723728.0946
Traceback (most recent call last):
File "app.py", line 14, in <module>
print('Python EXP() Function on String Number = ', math.exp('Python'))
TypeError: must be real number, not str
This method is used to calculate the power of e i.e., e^y, or we can say exponential of y. The value of e is approximately equal to 2.71828…
Thanks for reading !
#python #programming