AppDividend
Latest Code Tutorials

Python exp(): How to Use Python exp() Method

0

In Mathematics, the exponential value of a number is equivalent to the number being multiplied by itself a particular set of times. The number to be multiplied by itself is called the base, and the number of times it is to be multiplied is the exponent.

Python exp()

Python exp() is an inbuilt function that is used to calculate the value of any number with a power of e. Means e^n where n is the given number. The value of e is approximately equal to 2.71828… The exp() function is under the math library, so we need to import the math library before using this function.

Python exp() returns exponential of x: ex. The exp() function does not accessible directly, so we need to import the math module, and then we need to call the exp() function using math static object.

Syntax

math.exp(num)

The function takes only one argument num of which we want to find exponential.

Return Value

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.

Programming Example

See the following code example.

# Program to show the working of exp
import math

# Initializing the values

# int type
x = 16
# float type
y = 10.6
# negative num
z = -6

print("Value of e^x: ", math.exp(x))
print("Value of e^y: ", math.exp(y))
print("Value of e^z: ", math.exp(z))

Output

Value of e^x:  8886110.520507872
Value of e^y:  40134.83743087578
Value of e^z:  0.0024787521766663585

In this program, we have imported math libraries, and then we have initialized the value of different data types in x, y, and z. Then we have printed value of e**x, e**y, and e**z. We can see that all the values which are printed are in float data type.

Pass string as an argument in Python exp()

Let’s pass the string as an argument to the Python exp() method.

# Program2 to show working of 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.

See the following code.

# app.py

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

Conclusion

Python has a math library and has many functions regarding it. One such function is exp(). 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…

See also

Python trunc()

Python absolute value

Python fabs()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.