Python Language Math Module Constants


math modules includes two commonly used mathematical constants.

  • math.pi - The mathematical constant pi
  • math.e - The mathematical constant e (base of natural logarithm)
>>> from math import pi, e
>>> pi
>>> e

Python 3.5 and higher have constants for infinity and NaN ("not a number"). The older syntax of passing a string to float() still works.

Python 3.x3.5
math.inf == float('inf')
# Out: True

-math.inf == float('-inf')
# Out: True

# NaN never compares equal to anything, even itself
math.nan == float('nan')
# Out: False