Kodeclik Blog

# Euler’s number in Python

Euler's number, named after Leonhard Euler, is a mathematical constant with a value of approximately 2.71828. It is often denoted by the letter "e" and appears in many mathematical formulas. One of the most famous formulas that contains e is Euler's formula: e^ (i * pi) +1 = 0. This formula relates five interesting concepts in math: the numbers 0 and 1, the imaginary number “i”, and the irrational and transcendental numbers pi and “e”.

Euler's number appears in many different areas of mathematics and physics. In calculus, for example, it appears in the definition of the natural exponential function: f(x) = e^x. This function is used to model many real-world phenomena such as population growth, radioactive decay, and compound interest. In physics, e appears in the equation for the energy of an electron in an atom, and so on. Euler's number is sometimes also called Napier's constant.

Accessing Euler’s number in your Python is simplicity itself. Here is sample code:

```
import math
print (math.e)
```

The output will be:

`2.718281828459045`

Note that “e” is irrational so it does not have a terminating decimal representation. As you can see, this approach to accessing the value of “e” is quite simple and only takes a single line of code. However, it should be noted that the value returned by the math module is only an approximation of e; it is not exact.

## Computing Euler’s constant via Taylor expansion

A second way to calculate Euler’s constant is via a Taylor series expansion for e. A Taylor series expansion is an infinite series that is used to approximate functions. Here is how the code for that looks:

```
import math
sum = 0
for i in range(20):
sum += 1/math.factorial(i)
print(sum)
```

Even though Taylor series is supposed to be infinite, obviously in a computer we cannot simulate infinity. So we create a running series and sum upto 20 entries. At each step we add the reciprocal of the factorial of the loop index and add it to our accumulating sum. This series can be proven to converge to “e”. The output of the program is:

```
1.0
2.0
2.5
2.6666666666666665
2.708333333333333
2.7166666666666663
2.7180555555555554
2.7182539682539684
2.71827876984127
2.7182815255731922
2.7182818011463845
2.718281826198493
2.7182818282861687
2.7182818284467594
2.71828182845823
2.718281828458995
2.718281828459043
2.7182818284590455
2.7182818284590455
2.7182818284590455
```

As you can see, after the first few not very accurate terms the series quickly converges to the decimal value for “e”. The more terms we include in our Taylor series expansion, the more accurate our approximation will be (although there will always be some error since we're working with an infinite series).

So in conclusion, in this blog post, we have looked at two different ways to calculate Euler's number in Python. The first approach is to simply use the built-in math module. The second is via a Taylor series expansion.

If you liked this blogpost, learn about how to access the Boltzmann constant in Python!

For more Python content, checkout the math.ceil() and math.floor() functions! Also

learn about the math domain error in Python and how to fix it!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.