Mathematical Constants in Python


In this article, we will go through Python's mathematical constants and how to use them.

Some of the defined constants that can be utilized in a variety of mathematical operations are included in the math module. These mathematical constants return values that are equivalent to their standard defined values.

The following math module constants are available in the Python programming language:

  • Python math.e constant

  • Python math.pi constant

  • Python math.tau constant

  • Python math.inf constant

  • Python math.nan constant

Python math.e constant

The Euler's number, 2.71828182846, is returned by the math.e constant.

Syntax

math.e

Return Value − It returns the float value 2.71828182846 which represents the mathematical constant e.

Example

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the math module.

  • Print the Euler value e using the e constant of the math module with the help of the (.) operator.

Example

The following program returns the value of math.e constant using the math module in Python −

# importing math module import math # printing the Euler value e print ("The Euler value e = ", math.e)

Output

On executing, the above program will generate the following output −

The Euler value e = 2.718281828459045

Python math.pi constant

The pi value, 3.14159265359, is returned by the math.pi constant. It is described as the ratio of the circumference of a circle to the diameter of a circle.

Syntax

math.pi

Return Value − It returns the float value 3.14159265359, which represents the mathematical constant PI.

Example

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the math module.

  • Print the pi value using the pi constant of the math module with the help of the (.) operator.

Example

The following program returns the value of math.pi constant using the math module in Python −

# importing math module import math # printing the pi value print ("The pi value = ", math.pi)

Output

On executing, the above program will generate the following output −

The pi value = 3.141592653589793

Python math.tau constant

The tau value 6.283185307179586, is returned by the math.tau constant. It is described as the ratio of the circumference of a circle to the radius of a circle.

Syntax

math.tau

Return Value − It returns the float value 6.283185307179586, which represents the mathematical constant tau.

Example

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the math module.

  • Print the tau value using the tau constant of the math module with the help of the (.) operator.

Example

The following program returns the value of math. tau constant using the math module in Python −

# importing math module import math # printing the tau value print ("The tau value = ", math.tau)

Output

On executing, the above program will generate the following output −

The tau value = 6.283185307179586

Python math.inf constant

The value of positive infinity is returned by the math.inf constant. Use -math.inf for negative infinity.

The inf constant is the same as float("inf").

Syntax

math.inf

Return Value: It returns the float value that represents the value of positive infinity.

Example

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the math module.

  • Print the value of positive infinity using the inf constant of the math module with the help of the (.) operator.

  • Print the value of negative infinity using the inf constant of the math module with the help of the (.) operator and apply the -(negative) sign to it

Example

The following program returns the value of math.inf constant using the math module in Python −

# importing math module import math # printing the value of positive infinity print ("The value of positive infinity = ", math.inf) # printing the value of negative infinity print ("The value of negative infinity = ", -math.inf)

Output

On executing, the above program will generate the following output −

The value of positive infinity = inf
The value of negative infinity = -inf

Python math.nan constant

The floating-point nan (Not a Number) value, is returned by the math.nan constant. This is not a valid number. float("nan") is similar to the nan constant.

Syntax

math.nan

Return Value: It returns the float value, nan (Not a Number)

Example

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import the math module.

  • Print the nan value using the nan constant of the math module with the help of the (.) operator.

Example

The following program returns the value of math.nan constant using the math module in Python −

# importing math module import math # printing the nan value print ("The nan value = ", math.nan)

Output

On executing, the above program will generate the following output −

The nan value = nan

Conclusion

We learned about the mathematical constants available in Python in this article. We also demonstrated how to use these constants in a Python program with some examples.

Updated on: 25-Oct-2022

760 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements