Nearby lessons

76 of 159

Python - Math Module

📌 What You Will Learn
  • Import the math module and use its constants math.pi and math.e
  • Compute square roots, powers, absolute values, ceiling, floor, factorial, and GCD
  • Use trigonometric functions such as sin(), cos(), and tan()
  • Work with logarithmic functions like log() and log10()
  • Convert angles between degrees and radians with radians() and degrees()
  • Apply the built-in max(), min(), and sum() functions to sequences

Introduction

Python provides a built-in module called math for performing mathematical calculations.

The math module contains many ready-to-use mathematical functions and constants.

Why Math Module is Required?

Sometimes programs need complex mathematical calculations.

Writing these calculations manually can be difficult.

The math module provides ready-made functions to make calculations easier.

Importing the Math Module

Before using the math module, you must import it.

Syntax:

import math

Example - Import Math Module

Loading editor…

Features of Math Module

The math module provides:

  • Mathematical constants
  • Mathematical functions
  • Trigonometric functions
  • Logarithmic functions

Mathematical Constants

The math module provides useful mathematical constants.

Value of PI

Use math.pi to get the value of PI.

Example - math.pi

Loading editor…

Value of Euler Number

Use math.e to get the value of Euler's number.

Example - math.e

Loading editor…

Square Root Function

The math.sqrt() function returns the square root of a number.

Example - Square Root

Loading editor…

Power Function

The math.pow() function calculates the power of a number.

Example - Power

Loading editor…

Absolute Value

The math.fabs() function returns the absolute (positive) value of a number.

Example - Absolute Value

Loading editor…

Ceiling Function

The math.ceil() function returns the nearest greater integer.

Example - Ceiling Function

Loading editor…

Floor Function

The math.floor() function returns the nearest smaller integer.

Example - Floor Function

Loading editor…

Factorial Function

The math.factorial() function returns the factorial of a positive integer.

Example - Factorial

Loading editor…

GCD Function

The math.gcd() function returns the Greatest Common Divisor (GCD) of two numbers.

Example - GCD

Loading editor…

Trigonometric Functions

The math module provides trigonometric functions such as sin(), cos(), and tan().

Example - sin()

Loading editor…

Example - cos()

Loading editor…

Example - tan()

Loading editor…

Logarithmic Functions

The math module provides logarithmic functions.

Natural Logarithm

Use math.log() to calculate the natural logarithm.

Example - math.log()

Loading editor…

Base 10 Logarithm

Use math.log10() to calculate the base-10 logarithm.

Example - math.log10()

Loading editor…

Angle Conversion

The math module can convert angles between degrees and radians.

Degrees to Radians

Loading editor…

Radians to Degrees

Loading editor…

Maximum and Minimum Values

Python provides built-in functions max() and min() to find the largest and smallest values.

Example - max() and min()

Loading editor…

Sum Function

The built-in sum() function returns the total of all values in a sequence.

Example - sum()

Loading editor…

Random Mathematical Operation

Loading editor…

Difference Between Built-in Functions and math Module

Built-in Functions math Module
Available directly. Must be imported before use.
Example: abs() Example: math.sqrt()

Real World Usage

The math module is commonly used in:

  • Scientific calculations
  • Engineering software
  • Data analysis
  • Artificial Intelligence (AI)
  • Machine Learning (ML)
  • Financial applications

Example Areas

Usage Example
Geometry Area Calculation
Banking Interest Calculation
AI Mathematical Formulas
📝 Key Takeaways
  • The math module must be imported before its functions can be used
  • math.pi and math.e provide the values of PI and the Euler number
  • math.sqrt() and math.pow() return floating-point results
  • math.ceil() rounds up and math.floor() rounds down
  • math.gcd() finds the Greatest Common Divisor of two numbers
  • The math module provides ready-made functions for scientific and engineering calculations

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.