math Module Fundamentals
2 min read ·
Importing math Module
You must use the module name with dot operator to access its functions.
Common Functions
sqrt()
Returns square root of a number.
pow()
Returns power of a number.
factorial()
Returns factorial of a number.
Note
factorial works only with non negative integers.
ceil(), floor(), trunc()
These functions control how decimal numbers are handled.
ceil()
Rounds number up to nearest integer.
floor()
Rounds number down to nearest integer.
trunc()
Removes decimal part without rounding.
Pro Tip
Use ceil when you want next integer and floor when you want previous integer.
Constants pi and e
math module provides important mathematical constants.
pi
e
Combining Functions
Caution
Always import math module before using its functions, otherwise it will give error.
Exercise
Find square root of a number
Calculate factorial of a number
Use ceil, floor, and trunc on a decimal value
Calculate area of a circle using pi