Python math ModuleLast Updated : 6 Jan 2026 In Python, the math module is a built-in module that allows us to use mathematical tools and helps us to perform complex mathematical calculations, logarithms, exponentials, and other arithmetic operations. To read more Python Modules ![]() Let's see a simple example to understand the use of the Math module: Example: Finding Square RootExampleExecute NowOutput: Enter a number:25 The square root of the number is: 5.0 Explanation In the above example, we have imported the math module because it has built-in function sqrt() to find the square root. Constants in math ModuleThe math module provides the values of several constants that we regularly use in mathematical contexts. For example, to find the area of a circle, we have to use pi because the formula is pi*r2, where r is the radius of the circle. So, the math module makes it easier for us by providing built-in constant values.
Example: Printing Constant ValuesExampleExecute NowOutput: Value of Pi Constant 3.141592653589793 Value of Euler's Constant 2.718281828459045 Value of Tau's Constant 6.283185307179586 Value of Infinity = inf Value of Not a Number = nan Explanation In the above example, we have imported the math module and printed the values of various mathematical constants using math.constant_name. Python math Module FunctionsAs we know, the Python math module allows us to perform advanced mathematical calculations, such as trigonometric functions, logarithms, and exponentials. For performing the complex calculation math module provides the following built-in functions.
Example 1: Finding the Factorial of a NumberIn this example, we will see how to calculate the factorial of a number using the math module. ExampleExecute NowOutput: Enter a number: 5 The factorial of the entered number is: 120 Explanation In this example, we calculated the factorial of a number using math.factorial(n) function where n is the number entered by the user. Performing Trigonometric OperationsThe following example demonstrates how to calculate trigonometric ratios using the math module. Example: Using Trigonometric OperationsExampleExecute NowOutput: Enter a number: 90 The Sine value is: 0.8939966636005579 The Cosine value is: -0.4480736161291701 The Tan value is: -1.995200412208242 Explanation In the above example, we have calculated the values of some trigonometric functions using the sin, cos and tan functions in the math module. Permutation and CombinationIn the following example, we will determine the number of ways to choose a given number of balls from a bag of balls. In order to find the combination, we will be using the math.comb() function. Example: Ways of Choosing Balls from a BagExampleExecute NowOutput: Given Data: Total number of Balls in the bag: 10 Number of Balls to be selected: 4 Total number of Ways to select 4 balls from the bag of 10 balls: 210 Explanation Here we calculated the number of ways to choose 4 balls from the bag of 10 balls using math.comb() function Example 4: Degree to Radians and Vice-Versa ConversionHere, we will convert radians to degrees and degrees to Radians by taking input from the user. ExampleExecute NowOutput: Enter the angle in radians: 6.28318530718 Radians to Degrees = 360.0000000000237 Enter the angle in degrees: 360 Degrees to Radians = 6.283185307179586 Explanation In this example, we have used math.degrees() and math.radians() functions to convert radians to degrees and vice versa, receiving input from the user. Calculating Exponentialx to the power of e, often known as the exponential of a number x, is calculated using the exp() function. ExampleExecute NowOutput: The exponenetial value of 4 is: 54.598150033144236 The exponenetial value of -3 is: 0.049787068367863944 The exponenetial value of 0.0 is: 1.0 Using dir() FunctionA sorted list of strings comprising the identifiers of the functions defined by a module is what the built-in method dir() delivers. The list includes the names of modules, each specified constants, functions, and methods. Here is a straightforward illustration: ExampleExecute NowOutput: ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp'] ConclusionThe Python Math Module is a built-in module that allows us to perform advanced mathematical calculations, such as trigonometric functions, logarithms, and exponentials. We got to know about various types of functions, such as exponential and logarithmic functions. We learned about Constant values and their applications. Next TopicPython OS Module |
We request you to subscribe our newsletter for upcoming updates.