Amongst the umpteen mathematical constants in existence, one that is prominent of the lot would be Euler’s number. Distilled after a series of derivations by Leonard Euler in the 18th century, Euler’s number ventured into the world as we know being irrational with over a trillion digits of accuracy. Denoted by the letter ‘e’, given below is the value of this constant.
e = 2.718281828459045……
There are a handful of ways that can be deployed in Python to put Euler’s number into use. This article steps forth to cover each of these methods in great detail. More specifically, it details how to combine the power operation alongside Euler’s number through each of the following sections.
Using e from the math library
Python has an in-built library known as math, which is a repository of all the mathematical constants one can think of. Euler’s number ‘e’ is also one among them. So, before putting it into use, let us first import e from the math library using the following code.
from math import e
Now one shall very well raise the Euler’s number to the required power using the following code.
from math import e Power = 3 R = e**Power print(R)
Following is the result when the above code is run.
Using the exp( ) function:
For those who are not interested to use the power operator (**) to raise the Euler’s number, Python offers another way around. Enter the exp( ) function which raises the Euler’s number to the power of the value given as the input.
The subtle difference can be found towards the difference in the values of the ending decimals due to the approximation involved in executing this function. Python also offers a choice for us to pull this function from either of the following libraries.
- The math library
- The numpy library
Using exp( ) from the math library:
The first thing one ought to do is to import the math library using the following code.
Once done let us now put the same value used in the previous section & see how the result turns out.
import math math.exp(3)
It could be seen from the above result that the final digit in the decimal differs from the result obtained in the previous section. This is due to the rounding off approximation that occurs while using this function.
Using exp( ) from the numpy library:
Besides the math library, there is the numpy library too which can be used to deploy the exp( ) function for raising the Euler’s number to the power of the desired input value. One shall get started by importing the numpy library using the following code.
import numpy as np
Once, done the input value is given within the exp( ) function through the following code.
The difference in the value of the last decimal prevails here too as observed in the exp( ) function from the math library. If one ought to use the value of the Euler’s number, then one can simply print e from the math library or print the value of exp(1) from either math or numpy library as shown below.
Now that we have reached the end of this article, hope it has elaborated on how to use Euler’s number combined with the power operation in Python. Here’s another article that details the workings of the convolve( ) function from the numpy library within Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Carpe diem!