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

🐍Code Cell
1import math
2 
3print(math.sqrt(25))
Output
5.0

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

🐍Code Cell
1import math
2 
3print(math.pi)
Output
3.141592653589793

Value of Euler Number

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

Example - math.e

🐍Code Cell
1import math
2 
3print(math.e)
Output
2.718281828459045

Square Root Function

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

Example - Square Root

🐍Code Cell
1import math
2 
3print(math.sqrt(64))
Output
8.0

Power Function

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

Example - Power

🐍Code Cell
1import math
2 
3print(math.pow(2, 3))
Output
8.0

Absolute Value

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

Example - Absolute Value

🐍Code Cell
1import math
2 
3print(math.fabs(-25))
Output
25.0

Ceiling Function

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

Example - Ceiling Function

🐍Code Cell
1import math
2 
3print(math.ceil(10.2))
Output
11

Floor Function

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

Example - Floor Function

🐍Code Cell
1import math
2 
3print(math.floor(10.9))
Output
10

Factorial Function

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

Example - Factorial

🐍Code Cell
1import math
2 
3print(math.factorial(5))
Output
120

GCD Function

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

Example - GCD

🐍Code Cell
1import math
2 
3print(math.gcd(12, 18))
Output
6

Trigonometric Functions

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

Example - sin()

🐍Code Cell
1import math
2 
3print(math.sin(0))
Output
0.0

Example - cos()

🐍Code Cell
1import math
2 
3print(math.cos(0))
Output
1.0

Example - tan()

🐍Code Cell
1import math
2 
3print(math.tan(0))
Output
0.0

Logarithmic Functions

The math module provides logarithmic functions.

Natural Logarithm

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

Example - math.log()

🐍Code Cell
1import math
2 
3print(math.log(10))
Output
2.302585092994046

Base 10 Logarithm

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

Example - math.log10()

🐍Code Cell
1import math
2 
3print(math.log10(100))
Output
2.0

Angle Conversion

The math module can convert angles between degrees and radians.

Degrees to Radians

🐍Code Cell
1import math
2 
3print(math.radians(180))
Output
3.141592653589793

Radians to Degrees

🐍Code Cell
1import math
2 
3print(math.degrees(math.pi))
Output
180.0

Maximum and Minimum Values

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

Example - max() and min()

🐍Code Cell
1numbers = [10, 50, 20]
2 
3print(max(numbers))
4print(min(numbers))
Output
50
10

Sum Function

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

Example - sum()

🐍Code Cell
1numbers = [10, 20, 30]
2 
3print(sum(numbers))
Output
60

Random Mathematical Operation

🐍Code Cell
1import math
2 
3a = 10
4b = 20
5 
6print(math.sqrt(a + b))
Output
5.477225575051661

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