PYTHON

Python Math Module: Syntax, Usage, and Examples

The Python math module provides a set of mathematical functions and constants that simplify calculations in programming. It includes functions for arithmetic, trigonometry, logarithms, and more. This module is useful for scientific computing, data analysis, and engineering applications.

How to Import the Math Module in Python

The math module in Python is built-in, meaning you don’t need to install it separately. To access its functions, import it using:

import math

Once imported, you can use its methods and constants:

print(math.sqrt(25))  # Output: 5.0
print(math.pi)        # Output: 3.141592653589793

Is the Math Module Built Into Python?

Yes, the math module is part of Python's standard library, so it does not require installation. It is included with Python distributions and can be used immediately after import.

When to Use the Math Module in Python

Performing Complex Calculations

When working with advanced mathematical operations, the math module helps avoid manual calculations. Functions like square root, logarithms, and exponentiation make code more efficient.

import math

value = 9
print(math.sqrt(value))  # Output: 3.0
print(math.pow(2, 3))    # Output: 8.0
print(math.log(100, 10)) # Output: 2.0

Working with Trigonometry

The math module provides functions for trigonometric calculations, which are useful in graphics, physics simulations, and machine learning.

import math

angle = math.radians(45)  # Convert degrees to radians
print(math.sin(angle))  # Output: 0.7071067811865475
print(math.cos(angle))  # Output: 0.7071067811865476

Using Mathematical Constants

The module provides constants like pi and Euler’s number, which are essential in geometry and logarithmic calculations.

import math

radius = 10
circle_area = math.pi * math.pow(radius, 2)
print(circle_area)  # Output: 314.1592653589793

Examples of Using the Math Module in Python

Calculating Factorials

Factorials are commonly used in probability, statistics, and combinatorics. The math module makes it easy to compute them:

import math

print(math.factorial(5))  # Output: 120

Rounding and Flooring Numbers

To round numbers up or down, use the ceil and floor functions.

import math

print(math.ceil(4.3))  # Output: 5
print(math.floor(4.7))  # Output: 4

Computing Logarithms

Logarithmic calculations are common in statistics and machine learning.

import math

print(math.log(100, 10))  # Output: 2.0
print(math.log2(16))  # Output: 4.0
print(math.log10(1000))  # Output: 3.0

Finding the Greatest Common Divisor

The greatest common divisor (GCD) is useful in simplifying fractions and number theory.

import math

print(math.gcd(48, 18))  # Output: 6

Learn More About the Math Module in Python

How to Install the Math Module in Python

The math module is included in Python by default. If you get an import error, ensure that Python is installed correctly. Run the following command to check your Python version:

python --version

If Python is installed, the math module should be available.

Functions in the Math Module Python

The math module provides a wide range of functions:

  • math.sqrt(x): Computes the square root of x.
  • math.pow(x, y): Raises x to the power of y.
  • math.exp(x): Returns e^x.
  • math.log(x, base): Computes the logarithm of x to the specified base.
  • math.sin(x), math.cos(x), math.tan(x): Trigonometric functions.
  • math.degrees(x), math.radians(x): Converts between degrees and radians.
  • math.gcd(a, b): Computes the greatest common divisor.

How to Use the Math Module in Python

To use the math module, simply import it and call functions as needed.

import math

number = 16
square_root = math.sqrt(number)
print(f"Square root of {number} is {square_root}")

This prints the square root of 16.

Using Math Functions in Loops

The math module is useful in loops for batch calculations.

import math

numbers = [4, 9, 16, 25]
roots = [math.sqrt(num) for num in numbers]
print(roots)  # Output: [2.0, 3.0, 4.0, 5.0]

Using Math Functions for Financial Calculations

Logarithms and exponentiation are used in financial calculations, such as compound interest.

import math

principal = 1000
rate = 0.05
time = 10

final_amount = principal * math.exp(rate * time)
print(f"Final amount after {time} years: {final_amount:.2f}")

How to Import Math Module Python

To import the math module, use:

import math

Once imported, all math functions become available.

How to Use the Math Module in Python

Here’s an example using multiple math functions together:

import math

x = 45  # Degrees
radians = math.radians(x)
sin_value = math.sin(radians)
cos_value = math.cos(radians)
factorial_value = math.factorial(5)

print(f"Sin of {x} degrees: {sin_value}")
print(f"Cos of {x} degrees: {cos_value}")
print(f"Factorial of 5: {factorial_value}")

Using the Math Module with Conditionals

You can use the math module with conditional statements to validate input values.

import math

value = -5

if value >= 0:
    print(math.sqrt(value))
else:
    print("Cannot compute square root of a negative number")

How to Install Math Module in Python

The math module is built into Python, so no installation is required. If an error occurs, check that Python is installed correctly.

Math Functions for Scientific Computing

The math module is frequently used in scientific applications such as physics simulations, machine learning, and engineering.

import math

force = 100
angle = math.radians(30)
horizontal_force = force * math.cos(angle)
vertical_force = force * math.sin(angle)

print(f"Horizontal force: {horizontal_force}")
print(f"Vertical force: {vertical_force}")

Using Math Functions in a For Loop

To apply math functions in iterations, use loops.

import math

values = [1, 2, 3, 4, 5]
log_values = [math.log(v) for v in values]

print(log_values)

This prints the natural logarithm of each value in the list.

The Python math module is a powerful tool for mathematical operations. It provides functions for square roots, logarithms, trigonometry, factorials, rounding, and much more. Since it is built into Python, it does not require installation. Whether you need basic arithmetic or advanced scientific computations, the math module simplifies mathematical tasks and improves efficiency.

Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH