Skip to Content
ModulesMath Tutorial

Math Module in Python

The math module in Python provides a wide range of mathematical functions to perform operations like trigonometry, logarithms, and constants.


Installing

The math module is built into Python, so no installation is required. It is ready to use right after importing.


Configuration

No additional configuration is needed for the math module.


Import Statements

You can import the entire math module or specific functions as needed.

# Import the entire math module import math # Import specific functions from math import sqrt, pi

Basic Usage

The math module provides access to mathematical constants and functions.

Example:

import math # Accessing constants print(math.pi) # Output: 3.141592653589793 print(math.e) # Output: 2.718281828459045 # Using basic functions print(math.sqrt(16)) # Output: 4.0 print(math.factorial(5)) # Output: 120

FunctionDescriptionExampleOutput
sqrt(x)Returns the square root of x.math.sqrt(25)5.0
factorial(x)Returns the factorial of x.math.factorial(5)120
pow(x, y)Returns x raised to the power y.math.pow(2, 3)8.0
ceil(x)Rounds up to the nearest integer.math.ceil(4.2)5
floor(x)Rounds down to the nearest integer.math.floor(4.7)4
log(x, base)Returns the logarithm of x to base.math.log(100, 10)2.0
sin(x)Returns the sine of x in radians.math.sin(math.pi/2)1.0
cos(x)Returns the cosine of x in radians.math.cos(0)1.0

Niche Usage

The math module provides advanced functions for specific use cases:

1. Convert Degrees to Radians and Vice Versa

import math # Degrees to Radians angle_in_radians = math.radians(90) print(angle_in_radians) # Output: 1.5707963267948966 # Radians to Degrees angle_in_degrees = math.degrees(math.pi) print(angle_in_degrees) # Output: 180.0

2. Using Constants

import math # Using Pi and Euler's number circumference = 2 * math.pi * 7 # Circle with radius 7 print(circumference) # Output: 43.982297150257104

Examples

Example 1: Calculate the Hypotenuse of a Right Triangle

import math # Calculate the hypotenuse base = 3 perpendicular = 4 hypotenuse = math.sqrt(base**2 + perpendicular**2) print("Hypotenuse:", hypotenuse) # Output: Hypotenuse: 5.0

Example 2: Find the Area of a Circle

import math # Area of a circle radius = 7 area = math.pi * math.pow(radius, 2) print("Area of the circle:", area) # Output: Area of the circle: 153.93804002589985

Example 3: Find Logarithms

import math # Logarithms print("Natural log of 10:", math.log(10)) # Output: 2.302585092994046 print("Log base 10 of 100:", math.log(100, 10)) # Output: 2.0

Example 4: Trigonometric Functions

import math # Trigonometric values angle = math.pi / 4 # 45 degrees in radians print("sin(45°):", math.sin(angle)) # Output: 0.7071067811865475 print("cos(45°):", math.cos(angle)) # Output: 0.7071067811865476

Example 5: Round Numbers

import math # Rounding numbers number = 4.567 print("Ceil:", math.ceil(number)) # Output: 5 print("Floor:", math.floor(number)) # Output: 4

Pyground

Play with Python!

Output:

Last updated on