Let’s look at e in Python with Examples! 🔥🔥🔥
Euler’s number, denoted as e, is a fundamental mathematical constant approximately equal to 2.71828. It’s the base of the natural logarithm and is particularly significant in calculus, complex analysis, and number theory due to its unique properties.
In Python, Euler’s number can be accessed using the math module, specifically math.e. The below code will simply print the value of e.
import math
print(math.e)
#Prints
#2.718281828459045
You can then use e as part of different mathematical operations, like multiplication etc. :
import math
result = math.e * 3
print(result)
#Prints
#8.154845485377136
e can be used as a base and raised to some power x with the math.exp() function, which is really the same as ex .The function accepts a real number to which power e is raised, as a parameter:
import math
principal = 1000
rate = 0.05
time = 3
compound_interest = principal * math.exp(rate * time)
print(compound_interest)
#Prints:
#1161.834242728283
In this example we calculate compound interest. This is a known formula that uses e. We used the math.exp(rate * time) function as part of the expression that does the calculation which we can also write as erate x time.
We can also use e in calculations that require the natural logarithm. This is a mathematical function that represents the logarithm to the base e and is written as ln(x) or as loge of x. This simply means that you are finding the power to which e must be raised to equal x. In python you use the code math.log() to accomplish this. See the code below:
import math
x = 5.0
# Calculate the natural logarithm of x
ln_x = math.log(x)
print(ln_x)
#Prints:
#1.6094379124341003
Basically anything that you would do with any other number or constant you can do with e in Python. Check out our other great tutorials HERE.
Thanks for reading e in Python Examples! Happy coding! 👌🏻👌🏻👌🏻