Python Math Module: Key Functions and Constants for Mathematical Operations

Discover the Python math module, which provides a range of functions and constants for mathematical operations. Learn about essential mathematical constants and how to utilize the module for various calculations and mathematical tasks in your Python programs.



Python - Math Module

The math module in Python provides functions for various mathematical operations and constants. Here are some of its key features:

Mathematical Constants

The math module defines two important mathematical constants:

Example: Getting Pi Value

import math

print(math.pi)  # Output: 3.141592653589793
Output

3.141592653589793
Example: e Value

import math

print(math.e)  # Output: 2.718281828459045
Output

2.718281828459045

Trigonometric and Angle Conversion Functions

The math module provides functions for trigonometric calculations and angle conversions:

Example: Math Radians and Degrees

import math

print(math.radians(30))  # Output: 0.5235987755982988
print(math.degrees(math.pi/6))  # Output: 29.999999999999996
Output

0.5235987755982988
30.000000000000004
Example: sin, cos, tan Calculation

import math

print(math.sin(0.5235987755982988))  # Output: 0.49999999999999994
print(math.cos(0.5235987755982988))  # Output: 0.8660254037844387
print(math.tan(0.5235987755982988))  # Output: 0.5773502691896257
Output

0.49999999999999994
0.8660254037844387
0.5773502691896257

Other Mathematical Functions

The math module also includes functions for logarithms, exponents, powers, square roots, and rounding:

Example: log

import math

print(math.log(10))  # Output: 2.302585092994046
Output

2.302585092994046
Example: log10

import math

print(math.log10(10))  # Output: 1.0
Output

1.0
Example: Exponent

import math

print(math.exp(10))  # Output: 22026.465794806718
Output

22026.465794806718
Example: Power

import math

print(math.pow(2, 4))  # Output: 16.0
Output

16.0
Example: Square Root

import math

print(math.sqrt(100))  # Output: 10.0
print(math.sqrt(3))    # Output: 1.7320508075688772
Output

10.0
1.7320508075688772
Example: Ceil and Floor

import math

print(math.ceil(4.5867))   # Output: 5
print(math.floor(4.5687))  # Output: 4
Output

5
4