Python math.exp() Method

Python math.exp() is a built-in function that calculates the value of any number with a power of e. This means e^n, where n is the given number. The value of e is approximately equal to 2.71828.

Syntax

math.exp(num)

Arguments

The function takes only one argument num, 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.

Example 1

# 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 initialized the value of different data types in x, y, and z. Then we have printed values of e**x, e**y, and e**z. We can see that all the printed values are in float data type.

Example 2

# 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

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 a TypeError.

Example 3

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

That’s it.

Leave a Comment

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