Python math.e Constant



The Python math.e represents the mathematical constant e, which is approximately equal to 2.71828. It is a predefined value available in Python's math module and is commonly used in mathematical calculations involving exponential growth and decay, such as compound interest, population growth, and various scientific and engineering problems.

In general mathematics, e is a special number known as Euler's number, named after the Swiss mathematician Leonhard Euler. It is an irrational number, which means its decimal representation goes on infinitely without repeating.

The constant e is the base of the natural logarithm and is frequently used in mathematical computations. It represents the limit of (1 + 1/n)n as n approaches infinity.

Syntax

Following is the basic syntax of the Python math.e constant −

math.e

Return Value

The constant returns the value of mathematical constant e.

Example 1

In the following example, we are using the math.e constant to calculate the compound interest issued on a principal amount over a specified period of time, given an interest rate. This involves applying the formula for compound interest with continuous compounding, where the base of Euler's number (e) is raised to the power of the interest rate multiplied by time −

import math
principal = 1000
rate = 0.05
time = 5
compound_interest = principal * math.e ** (rate * time)
print("The compound interest after", time, "years is:", compound_interest)

Output

Following is the output of the above code −

The compound interest after 5 years is: 1284.0254166877414

Example 2

Here, we calculate exponential growth of a quantity over time using Euler's number (e). This is done by computing the final amount after a specified period of time, providing the initial amount, growth rate, and time, using the formula for exponential growth −

import math
initial_amount = 10
growth_rate = 0.1
time = 3
final_amount = initial_amount * math.e ** (growth_rate * time)
print("The final amount after", time, "years of exponential growth is:", final_amount)

Output

The output obtained is as follows −

The final amount after 3 years of exponential growth is: 13.498588075760033

Example 3

In this example, we approximate the factorial of a number using Stirling's approximation. This involves raising Euler's number (e) to the power of the natural logarithm of the factorial of the number plus one −

import math
n = 5
factorial_approximation = math.e ** (math.lgamma(n + 1))
print("The approximation of", n, "! using Stirling's approximation is:", factorial_approximation)

Output

The result produced is as follows −

The approximation of 5 ! using Stirling's approximation is: 120.00000000000006

Example 4

Now, we are calculating the probability density at a specified point "x" for a standard normal distribution using Euler's number (e) −

import math

x = 2
mu = 0
sigma = 1
probability_density = (1 / (math.sqrt(2 * math.pi) * sigma)) * math.e ** (-0.5 * ((x - mu) / sigma) ** 2)
print("The probability density at x =", x, "for a standard normal distribution is:", probability_density)

Output

We get the output as shown below −

The probability density at x = 2 for a standard normal distribution is: 0.05399096651318806
python_maths.htm
Advertisements