Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

math

Python's math module provides common real-number mathematical functions and constants, including floating-point, trigonometric, logarithmic, summation, distance, and number-theoretic operations. Use cmath instead when calculations require complex numbers.

How it works

Import the module and call its functions with numeric arguments or iterables. It supplies floating-point arithmetic, powers and logarithms, trigonometry, distance and summation helpers, and constants; complex-number calculations belong in cmath instead.

Syntax

import math
math.function(arguments)

Parameters

NameTypeDescription
function-specific argumentsvariesNumeric arguments, iterables, coordinates, or other inputs required by the selected function; each function documents its own parameters.

Returns

Varies by function; unless explicitly noted otherwise, documented math functions return floats.

Key points

  • Import math when backend or scripting code needs a standard collection of real-number operations and constants.
  • The module groups APIs for floating-point arithmetic, powers, logarithms, angles, trigonometry, hyperbolic functions, special functions, and number theory.
  • math.dist compares coordinate iterables and computes the Euclidean distance between two points.
  • math.fsum accepts an iterable and provides a dedicated floating-point summation operation.
  • math.prod multiplies iterable elements, beginning with the supplied start value.
  • math.fmod expresses the floating-point remainder operation for two numeric arguments.

Common mistakes

  • The module is for real-number mathematics; use cmath for complex values.
  • Do not assume a function returns an integer even when its input is integral; check the individual function description.

Example: Common floating-point operations

import math

values = [0.1, 0.2, 0.3]
print(math.fsum(values))
print(math.isclose(0.3, 0.1 + 0.2))
print(math.sqrt(16))

Uses accurate summation, approximate comparison, and square-root calculation.

When to use it

Numeric calculations

Compute roots, powers, logarithms, or floating-point remainders in application logic.

Geometry and spatial checks

Measure distances between points represented by coordinate iterables.

Stable floating-point totals

Aggregate decimal-like floating-point measurements with reduced summation error.

Scientific and engineering formulas

Convert angles and perform trigonometric or hyperbolic calculations.

Interview questions

QWhat is Python's math module used for?

The math module provides common mathematical functions and constants, including functions defined by the C standard. Its documented areas include floating-point arithmetic, trigonometry, logarithms, summation, products, distances, and number theory.

Follow-up: Which module should handle complex-number operations? Use cmath, whose corresponding functions are intended for complex numbers.

QWhen would you choose math.fsum?

math.fsum(iterable) sums values from an iterable and is intended for floating-point summation. It is preferable to a naive repeated addition when accumulated floating-point error matters.

Follow-up: How would you sum integers with exact integer semantics? Use Python's built-in sum for ordinary integer accumulation rather than choosing a floating-point-specific helper.

QWhat does math.sqrt do, and what type should callers generally expect?

math.sqrt(x) returns the square root of x for real-number calculations. Functions in the module generally return floats unless their documentation explicitly says otherwise.

Follow-up: What should be considered for a negative real input? A real square root is not available through math.sqrt; complex-valued work requires cmath or another suitable approach.

  • cmath
  • decimal
  • fractions

Source: math — Mathematical functions. Checked 2026-10-16.