Python Math Module Guide: Functions, Constants & Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Python Math Module Basics for Mathematical Computations

Mathematical computations are a fundamental part of any program. From simple calculators to complex analytical systems, nearly every application requires working with numbers.

Python provides a powerful and convenient standard module for handling numbers and mathematical operations. It lets you use both basic and advanced functions without needing to install third-party libraries.

The math module is especially useful when you need precise and reliable calculations. It supports mathematical constants, rounding operations, trigonometric calculations, and many other tasks.

How to Import the Math Module

The math module is part of Python's standard library, so there's no need to install it separately. Simply import it like this:

import math

Once imported, all the module's functions and constants become available. This approach avoids naming conflicts and keeps your code readable.

Key Mathematical Functions in Python

The math module offers a wide range of functions for arithmetic, algebraic, and trigonometric operations.

Exponentiation and Roots

print(math.pow(3, 4))  # 81.0 (exponentiation)
print(math.sqrt(25))   # 5.0 (square root)

The math.pow function always returns a floating-point number. If you only need integer results, use the ** operator instead:

print(3 ** 4)  # 81

To compute roots of other degrees, you can combine functions:

print(math.pow(27, 1/3))  # 3.0 (cube root of 27)

Logarithmic Functions

print(math.log(100))      # Natural logarithm (base e)
print(math.log(100, 10))  # Logarithm base 10
print(math.log2(8))       # Logarithm base 2
print(math.log10(1000))   # Logarithm base 10

Logarithmic functions are widely used in scientific computing, signal processing, and machine learning algorithms.

Rounding Functions

print(math.ceil(3.1))   # 4 (round up)
print(math.floor(3.9))  # 3 (round down)
print(math.trunc(3.7))  # 3 (truncate decimal part)

These methods are useful when you need strict control over rounding behavior. math.ceil always rounds up to the nearest integer, math.floor rounds down, and math.trunc simply removes the decimal part.

Absolute Value

To get the absolute value of a number, use:

print(math.fabs(-15))  # 15.0

math.fabs always returns a positive float value. This differs from the built-in abs() function, which preserves the original number's type.

Handling Special Values

print(math.isinf(float('inf')))  # True
print(math.isnan(float('nan')))  # True
print(math.isfinite(100))        # True

These methods help detect invalid or special values in your calculations. math.isfinite checks whether a number is finite (not infinity or NaN).

Trigonometric Functions

The math module provides a complete set of trigonometric functions that work with angles in radians.

Basic Trigonometric Functions

print(math.sin(math.pi / 2))  # 1.0
print(math.cos(0))            # 1.0
print(math.tan(math.pi / 4))  # 1.0

All trigonometric functions in Python use radians. This is an important

Blogs

Book Recommendations