Content · Programming · Python

Python math Module: Mathematical Functions Overview

Use the math module for numerical calculations that exceed basic operators.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Use the math module for numerical calculations that exceed basic operators.

What You Will Learn


math contains real-number functions for rounding, trigonometry, powers, logarithms, floating-point tests, and special functions. In Python, integer-only functions are also available from math.integer; math retains backward-compatible aliases that are soft-deprecated.

Floating-point values are approximations; use Decimal for financial decimals and math.isclose() for tolerant comparisons.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

import math
print(math.sqrt(81))
print(math.sin(math.pi / 2))
print(math.isclose(0.1 + 0.2, 0.3))

How the Example Works


  • The imports provide the standard-library tools used in the demonstration: math.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

Expected output from the example:

9.0
1.0
True

Another Example: Compare the Results


Try a second input and compare it with the first. Notice whether the result changes in type, value, order, or number of items.

import math
print(math.isfinite(math.inf), math.isfinite(math.pi))

Expected output:

False True

When You Would Use This


These tools are useful when composing small reusable functions, adapting callbacks, or eliminating repeated work. Pay attention to whether the operation returns a new object, updates state, or produces lazy results. Test a small success case and a boundary case before integrating it into an application.

Practice: Modify the Code


Calculate the diagonal of a rectangle with hypot() and compare rounded results.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Using math functions with complex-number arguments, which generally require cmath instead.
  • Assuming the operation has the same behavior for missing values, empty inputs and regular inputs.

Key Takeaways


  • Use the math module for numerical calculations that exceed basic operators.
  • math contains real-number functions for rounding, trigonometry, powers, logarithms, floating-point tests, and special functions.
  • Floating-point values are approximations; use Decimal for financial decimals and math.isclose() for tolerant comparisons.
Share this resourceSend this page to someone who may find it useful.