Python Language Math Module Constants

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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
3.141592653589793
>>> e
2.718281828459045
>>>

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


Got any Python Language Question?