Python 3 – Number exp() Method
In Python 3, the exp()
method is used to calculate the exponential value of a given number. The exp()
method belongs to the math module in Python 3.
Syntax
The syntax of the exp()
method is:
import math
math.exp(x)
Here, x
is the number whose exponential value needs to be calculated.
Example
Consider the following example:
import math
print(math.exp(2)) # Output: 7.38905609893065
Here, the exp()
method calculates the exponential value of 2
which is equal to 7.38905609893065
.
Code for Getting Exponential Values of Multiple Numbers
We can also obtain exponential values of multiple numbers at once. Here is an example:
import math
values = [1, 2, 3, 4, 5]
res = [math.exp(x) for x in values]
print(res) # Output: [2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236, 148.4131591025766]
In this code, we have initialized a list of numbers values
with values [1, 2, 3, 4, 5]
. Then, we have used a list comprehension to calculate the exponential value of each number. Finally, we have printed the resulting list res
which is [2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236, 148.4131591025766]
.
Conclusion
The exp()
method in Python 3 is used to calculate the exponential value of a given number. It is a simple and easy-to-use method and can be used to obtain exponential values for multiple numbers at once.