. Advertisement .
..3..
. Advertisement .
..4..
It is undeniable that e (or Euler’s number) is among the most fundamental concepts in math. Though it plays a vital role in many programming applications, not many people know how to manipulate it properly.
This is also the reason why we are here! Read on to learn how to use Euler’s number in Python now!
How to use Euler’s number in Python
Use e In Python With math.e
The first method that most of us will immediately think of is the usual math module. This module is an original integration in Python, so there is no need for additional installation. It provides you with numerous mathematical constants, including e.
To test it out, just throw in this base code.
from math import e
print('The value of the constant e is:',e)
Output:
The value of the constant e is: 2.718281828459045
Now you can manipulate this value however you want.
Let’s take a new example, which accepts a value and returns e to the power of that value.
Code:
from math import e
def calculateExponential(x):
return e**x
print(calculateExponential(5))
Output:
148.41315910257657
The exact result of this example is e^5. If you want to specify exactly how many decimals places the result shows, try to format it to string first and then print it out.
Use e In Python With math.exp()
math.e can only call the base value of e out, and you need to write sub-functions to take control of it. To make things easier, math also offers an exp() feature. This function does the same thing as our example, taking in a number and returning e to the power of that number.
Here is how it works:
Code:
import math
print(math.exp(5))
Output:
148.4131591025766
The output is the same, which is 148.4131591025766. In comparison with math.e
, math.exp()
has considerably better performance in the speed department.
It also has some codes that can validate the input parameter. We must build these codes ourselves if we use math.e. However, you cannot modify this function like with math.e, as it’s locked behind the math library.
Use e In Python With numpy.exp()
In fact, the numpy.exp()
function has the exact same operation process as math.exp()
. That is why it accepts the same input and returns the same output.
Nevertheless, it has a wider range of acceptable inputs, such as vectors, arrays, and collections. It also has a much faster operation speed than both math.e and math.exp().
Its only weakness is that it requires you to install the numpy library to work. There is no way to bypass this requirement. We do recommend installing numpy, though, as it can help you tremendously later down the line.
If you put in a number array, its returned value will be an array of e to the power of each value in that array.
Here is a quick example:
import numpy as np
number_array = [1.7, 2.2, 4, 2.9, 1.9]
number_single = 5
print(np.exp(number_array))
print(np.exp(number_single))
Output:
[ 5.47394739 9.0250135 54.59815003 18.17414537 6.68589444]
148.4131591025766
Remember that this technique will become very useful later down the line, just like converting JSON to CSV.
Conclusion
All in all, you now learn about the three approaches to Using e in Python. Each of them has some particular cases where that suit most.
For instance, math.e
is the quickest if you are only interested in calling the e value. math.exp()
and numpy.exp()
has the same operation, but numpy.exp()
is quicker while requiring additional installation.
Leave a comment