Python Math ModuleMathematical calculations may occasionally be required when dealing with certain fiscal or rigorous scientific tasks. Python has a math module that can handle these complex calculations. Both simple mathematical calculations like addition (+), and subtraction (-), and advanced mathematical calculations like trigonometric operations, and logarithmic operations can be performed by the functions in the math module. 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 built-in functions defined in this module for better understanding. What is Math Module in Python?Python has a built-in math module. It is a standard module, so we don't need to install it separately. We only have to 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. For instance, Code Output: 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 built-in 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. 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 precisely and repeatedly write down the value of each constant. 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: Code Output: The value of Euler's Number is: 2.718281828459045 TauThe ratio of a circle's circumference to its radius is known as tau. The value tau returned by the tau constant is 6.283185307179586. Syntax of this is: Code Output: The value of Tau is: 6.283185307179586 InfinityInfinity refers to anything limitless or never-ending in both directions of the actual number line. Numbers cannot adequately represent it. The math.inf returns positive infinity constant. We can use -math.inf to print negative infinity. Syntax of this is: Code Output: inf -inf Further, we are comparing a very large floating-point number with positive and negative infinity values. Code Output: True True PiPi is known to everyone. It is mathematically represented as either the fraction 22/7 or the decimal number 3.14. math.pi gives the most accurate value of pi. Syntax of this is: Code Output: The value of pi is 3.141592653589793 Let us calculate the circumference of a circle. Code Code 25.132741228718345 NaNThe math.nan gives us a floating-point nan (Not a Number) value. This amount is not a valid numeric value. Float("nan") and the nan constant are comparable. Code Output: 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. Code Output: 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 one-liner code by using the math.factorial() function. The Python interpreter will send a message if the given number is not integral. Code Output: The factorial of 6 is : 720 Cannot calculate factorial of a non-integral number Calculating the Absolute ValueThe method math.fabs() returns the absolute number of the number given to the function. Code Output: 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: 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: The value of 4 to the power of 5 is: 1024.0 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: 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 built-in method dir() delivers. The list includes the names of modules, each specified constants, functions, and methods. Here is a straightforward illustration: Code Output: ['__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.
Next TopicPython Tutorial
|