Explain the Python Math with example.
168
08-Oct-2025
Updated on 08-Oct-2025
Gulab
08-Oct-20251. Basic Arithmetic in Python
Python can perform basic mathematical operations directly using arithmetic operators:
+5 + 38-9 - 45*7 * 642/8 / 24.0//7 // 23%10 % 31**2 ** 382. Math with the
mathModulePython has a built-in
mathmodule for more advanced mathematical functions.You first need to import it:
Here are the main features:
Common Math Functions
math.sqrt(x)math.sqrt(16)4.0math.pow(x, y)math.pow(2, 3)8.0math.ceil(x)math.ceil(3.2)4math.floor(x)math.floor(3.8)3math.fabs(x)math.fabs(-5.6)5.6math.factorial(x)math.factorial(5)120math.fmod(x, y)math.fmod(10, 3)1.03. Trigonometric Functions
The
mathmodule supports trigonometry (angles in radians, not degrees).math.sin(x)math.sin(math.pi/2)→1.0math.cos(x)math.cos(0)→1.0math.tan(x)math.tan(math.pi/4)→1.0math.asin(x)math.asin(1)→1.5708math.degrees(x)math.degrees(math.pi)→180.0math.radians(x)math.radians(180)→3.141594. Logarithmic and Exponential Functions
math.exp(x)math.exp(2)7.389math.log(x)math.log(10)2.302math.log10(x)math.log10(1000)3.0math.log2(x)math.log2(8)3.05. Constants
The
mathmodule provides useful constants:math.pi3.141592653589793math.e2.718281828459045math.infmath.infmath.nanmath.nan6. Rounding and Comparison
round(x, n)round(3.14159, 2)→3.14math.isfinite(x)math.isfinite(100)→Truemath.isnan(x)math.isnan(float('nan'))→TrueExample Program
Output: