Out of the many modules available in python, one of them is the math module. Using this math module, we can access several mathematical functions. This module consists of several functions such as logarithmic functions, trigonometric functions, representation functions, etc., as well as mathematical constants. In this article, we shall be looking into one such mathematical constant – python e constant.
What are mathematical constants
By using the math module in python, we can access mathematical constant from the module. We can use these constants for carrying out mathematical operations and defining formulas in python. The values returned by these constants are equal to their values as defined in mathematics. The mathematical constants in python’s math module are :
Note: Here, math.inf and math.nan is available for python versions 3.5 onwards, and math.tau is available for python versions 3.6 and onwards.
About Python Math e
The math e is a mathematical constant in python. It returns the value of Euler’s constant which is an irrational number approximately equal to 2.71828. Euler’s constant is used in Natural Logarithms, calculus, trigonometry, differential equations, etc.
The Syntax of Accessing the Euler’s Constant from the math module is:
The return value of math.e constant is a floating-point value of Euler’s constant.
Examples of Python Math e
Let us take a python code to understand the math e constant. First, we shall import the math module. And then, we will print the return value of math.e constant.
import math print(math.e)
The output of the constant is:
Else, we can also use math e constant like this:
from math import e print(e)
It will generate same output.
We can also use the math constant to create formulas.
Suppose, if we want to define the particular function, f(x) = (e^x – 1)/x, then we can use math e constant to achieve that. We shall be defining a user-defined function func() which takes a single argument which is the value of ‘x’ from the formula. Here, math.e shall represent ‘e.’
import math def f(x): return ((math.e ** x) - 1)/x print(f(2))
Python e Constant Using math.exp()
The exp() is a function present in the math module which is used to output the exponential power of the euler’s constant.
The syntax is:
It takes one argument ‘n’, which can be any positive or negative number. The argument is taken as the power value to which the Euler’s constant has to be raised.
As the output, it returns the python e constant raised to the given power (e^n).
import math print(math.exp(1))
The output will be e ^ 1. It is similar to math.e
Python e Constant Using numpy.exp()
Similar to the exp() function in the math module, we also have an exp() function in the numpy library. The syntax of the exp() function in numpy is:
numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
It accepts an array as the argument. The output of the function is an n-dimensional array that consists of the calculated values. The array values are taken as the power to the python e constant.
First, we shall import the numpy library in python. Then, we shall take an array containing a single element – 1.
Then, we shall pass that array as an argument to the numpy.exp() function and print the result.
import numpy as np array = np.array(1) print(np.exp(array))
The output is:
We can also print multiple powers of the python e constant at the same time. For that, we shall add multiple values in the array as the powers to the constant.
import numpy as np array = np.array([1,2,3,4,5,6,7,8,9,10]) print(np.exp(array))
[2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03 2.98095799e+03 8.10308393e+03 2.20264658e+04]
Plotting math e
The graph of math e plotted will be an exponentially increasing graph. We shall try to plot the first 10 powers of the Euler’s constant using math e constant.
For that, we shall have to import the matplotlib library also along with the math module. Then with a for loop, we shall generate the powers of Euler’s constant by raising numbers to the math e constant and store it into a list. Then we shall plot that list using the plot() function.
import math import matplotlib.pyplot as plt list1 =  for i in range(0,10): list1.append(math.e ** i) plt.plot(list1) plt.show()
As seen, it is an exponentially increasing graph.
Using Constant e as Base for log
The main application of Euler’s constant is in the natural logarithm. We can use the constant e as the base for the log. For that, we shall make use of the log() function present in the math module. We pass the value whose log has to be calculated as the first argument and the base as the second argument.
import math print(math.log(2,math.e))
- Repr(): Discovering a Special Method in Python
- Diving into Python’s Numpy Random Poisson
- An Insight into the Numpy Searchsorted Function
- Match Case Python: New Addition to the Language
- Everything You Should Know About Numpy recarray
That was all about python e constant. If you have any questions in mind, let us know in the comments below.
Until next time, Keep Learning!