Python Math ModuleIntroductionIn this article, we are discussing Math Module in Python. We can easily calculate many mathematical calculations in Python using the Math module. Mathematical calculations may occasionally be required when dealing with specific fiscal or rigorous scientific tasks. Python has a math module that can handle these complex calculations. The functions in the math module can perform simple mathematical calculations like addition (+) and subtraction () and advanced mathematical calculations like trigonometric operations and logarithmic operations. This tutorial teaches us about applying the math module from fundamentals to more advanced concepts with the support of easy examples to understand the concepts fully. We have included the list of all builtin functions defined in this module for better understanding. What is Math Module in Python?Python has a builtin math module. It is a standard module, so we do not need to install it separately. We only must import it into the program we want to use. We can import the module, like any other module of Python, using import math to implement the functions to perform mathematical operations. Since the source code of this module is in the C language, it provides access to the functionalities of the underlying C library. Here we have given some basic examples of the Math module in Python. The examples are written below  Program Code 1:Here we give an example of a math module in Python for calculating the square root of a number. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  3.0 This Python module does not accept complex data types. The more complicated equivalent is the cmath module. We can, for example, calculate all trigonometric ratios for any given angle using the builtin functions in the math module. We must provide angles in radians to these trigonometric functions (sin, cos, tan, etc.). However, we are accustomed to measuring angles in terms of degrees. The math module provides two methods to convert angles from radians to degrees and vice versa. Program code 2: Here we give an example of a math module in Python for calculating the factorial of a number. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  5 120 Program code 3: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12 2 144.0 Program code 4: Here we give an example of a math module in Python for calculating the GCD of two numbers. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12 4 4 Math Module:Program code We use a program code to know all the functions of the Math module in Python. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  ['__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', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc'] Now we discuss the functionalities mentioned above in Python. 1. Ceil() :In the case of the ceiling, it will always return the nearest integer value toward positive. Here we give the program code of ceil() in Python. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12.7856 13 2. floor() :In the case of the floor(), it will always return the nearest integer value toward negative. Here we give the program code of floor() in Python. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12.7856 13 3. fabs() :In the case of fabs(), it will always return the nearest value towards negative. Here we give the program code of fabs() in Python. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12.7856 13 How to print the value of pi?We write code to print the pi value in the Python math module. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  3.141592653589793 What is the division concept in Python math module?We write code for the division concept in the Python math module. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  12 5 Float Division 2.4 Integer Division 2 Reminder 2 Constants in Math ModuleThe value of numerous constants, including pi and tau, is provided in the math module so that we do not have to remember them. Using these constants eliminates the need to write down the value of each constant precisely and repeatedly. The math module includes the following constants:
Let's go over each of them one by one. Euler's NumberThe value 2.71828182845 of Euler's number is returned by the math.e constant. Syntax of this is: The syntax of Euler's number is given below  Program Code: Now we write a code for print the value of Euler's number. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The value of Euler's Number is: 2.718281828459045 TauThe tau is the ratio of a circle's circumference to its radius. The value tau returned by the tau constant is 6.283185307179586. Syntax of this is: The syntax of tau is given below  Program Code Now we write a code to print the value of tau. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The value of Tau is: 6.283185307179586 InfinityInfinity means which have no boundaries. Infinity refers to anything limitless or neverending in both directions of the actual number line. Numbers cannot adequately represent it. The math.inf returns a positive infinity constant. We can use math.inf to print negative infinity. Here we can also compare a very large floatingpoint number with infinity values which are positive and negative. Syntax of this is: The syntax of infinity is given below  Program Code 1: Now we write a code to know whether the value is infinity or not. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  inf inf Program code 2: Now we write a code to know whether the value is infinity. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  True True PiPi is a constant value in mathematics and is used to find the radius of a circle. Pi is known to everyone. It is mathematically represented as the fraction 22/7 or the decimal number 3.14. math.pi gives the most accurate value of pi. Syntax of this is: The syntax of pi is given below  Program Code 1: Now we write a code to print the value of pi. The code is given below  Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  Output: The value of pi is 3.141592653589793 Let us calculate the circumference of a circle. Program Code 2: Now we write a code for the value of circle circumference using the pi function in Python. The code is given below  Output Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The value of the circle circumference: 25.132741228718345 NaNThe math.nan gives us a floatingpoint nan (Not a Number) value. This amount is not a valid numeric value. Float("nan") and the nan constant are comparable. Program Code: Now we write a code for now the floatingpoint nan (Not a Number) value. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  nan Mathematical Operations with Math ModuleThe functions that are required in representation theory and number theory, such as calculating the factorial of an integer, will be covered in this part. Calculating the Ceiling and the Floor ValueThe terms "ceiling value" and "floor value" refer to the smallest integral value larger than the number and the largest integral value less than the number, respectively. The ceil() and floor() methods simplify calculating this. Program Code: Here we write a code in Python using floor() and ceil() functions. The code is given below  Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The ceiling value of 4.346 is : 5 The floor value of 4.346 is : 4 Calculating the Factorial of the NumberWe may determine the factorial of a given integer in a oneliner code by using the math.factorial() function. The Python interpreter will send a message if the given number is not integral. Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The factorial of 6 is : 720 Cannot calculate factorial of a nonintegral number Calculating the Absolute ValueThe method math.fabs() returns the absolute number of the number given to the function. Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The absolute value of 45 is: 45.0 Calculating the Exponentialx to the power of e, often known as the exponential of a number x, is calculated using the exp() function. Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  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 Calculating the Power of a Numberx**y is computed via the pow() function. This function calculates the value of the power after converting its inputs into floats. Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The value of 4 to the power of 5 is: 1024.0 Without using of pow function, we can also calculate Power in Python. The code is given below  Program Code: Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The value of 4 to the power of 5 is: 1024 Calculating Sine, Cosine, and TangentThe values of sine, cosine, and tangent of an angle, which are supplied as an input to the function, are returned by the sin(), cos(), and tan() methods. This function expects a value that is provided in radians. Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  The sine of pi/4 is : 0.7071067811865475 The cosine of pi/4 is : 0.7071067811865476 The tangent of pi/4 is : 0.9999999999999999 The dir( ) FunctionA sorted list of strings comprising the identifiers of the functions defined by a module is what the builtin method dir() delivers. The list includes the names of modules, each specified constants, functions, and methods. Here is a straightforward illustration: Code Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below  ['__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'] Description of all the Functions in Python Math ModuleHere is a list of all the properties and functions specified in the math module, along with a brief description of what each one does.
Conclusion:So, in this article, we discuss the math module in Python. In the math module, there are various functions that we can use for mathematical calculation and many more. We discuss every function in the math module and also share some program codes of these functions.
Next TopicPython Tutorial
