Python Math Constants

Python Math Constants

Now, we are quite familiar with some of the methods from the math module, and now, we are going to have a look at some of the constants from the math module. Basically, at times, we are required to make use of some constants related to math, like if we are calculating the area of a circle for example, then we need the constant pi. So, when we need the constant pi, we do not need to set it ourselves, but we have a constant pi available with the math module, which we can use in our program.

So, we are going to look at some of the constants available in the math module.

Python Math Constants

Here is a table, in which, we have listed some constants from the math module. Let’s have a look at the table first.

Constantvalue
piΠ = 3.141592…
ee = 2.718281…
tauτ = 6.283185…
InfPositive infinity (we use -math.inf for negative infinity)
nanThis simply means not a number.

Let’s have a look at a simple program now, which tries to demonstrate using the constant values from the math module.

As you can see in the above program, we have imported the math module, and then we are trying to use the constants, pi, e, tau, and more. Now, Let’s have a look at the output of the above program.

3.141592653589793

2.718281828459045

6.283185307179586

inf

nan

As you can see, we could access the constants in our programs. So, as and when required, we can make use of these constant values in our programs. Let’s have a look at a sample program, in which, we would calculate the area of a circle, and as the value of pi, we are going to use the constant value from the math module.

As you can see in the above program, we have imported the math module, and we are calculating the area of a circle, and the value of the pi constant is taken from the math module. So, as and when required, we can make use of the constants from the math module, as and when required.

So, this was much about the math module, which helps us a lot, in doing different mathematical operations. We can make use of the math module in our program, as and when required. The thing is that we need to import the module using the import statement. Then we can use some of the useful methods, and constants from the math module.