Standard Library

Python math Module

Python Mathematical Functions

Python math module provides functions like sin and pi constants.

Overview of the Python math Module

The Python math module is part of the standard library and provides mathematical functions defined by the C standard. It is widely used for performing complex mathematical operations with ease. The module includes various functions, constants, and types that facilitate mathematical computations.

Importing the math Module

To use the functions and constants provided by the math module, you must first import it into your Python script. This can be done using the following line of code:

Commonly Used math Functions

The math module provides a variety of functions for mathematical operations. Below are some of the most commonly used functions:

  • math.sqrt(x): Returns the square root of x.
  • math.sin(x): Returns the sine of x radians.
  • math.cos(x): Returns the cosine of x radians.
  • math.tan(x): Returns the tangent of x radians.
  • math.log(x, base): Returns the logarithm of x to the specified base. If no base is provided, returns the natural logarithm.

Common Constants in math Module

In addition to functions, the math module also provides several useful mathematical constants:

  • math.pi: The mathematical constant π (pi), approximately equal to 3.14159.
  • math.e: The mathematical constant e, approximately equal to 2.71828.

Using Trigonometric Functions

The math module provides trigonometric functions that operate on radians. To convert degrees to radians, you can use math.radians(). These functions are useful for various applications, including engineering and physics.