Sometimes when working with some kind of financial or scientific projects it becomes necessary to implement mathematical calculations in the project. Python provides the math module to deal with such calculations. Math module provides functions to deal with both basic operations such as addition(+), subtraction(-), multiplication(*), division(/) and advance operations like trigonometric, logarithmic, exponential functions. Show
In this article, we learn about the math module from basics to advance using the help of a huge dataset containing functions explained with the help of good examples. Constants provided by the math moduleMath module provides various the value of various constants like pi, tau. Having such constants saves the time of writing the value of each constant every time we want to use it and that too with great precision. Constants provided by the math module are –
Let’s see each constant in detail. Euler’s NumberThe math.e constant returns the Euler’s number: 2.71828182846. Syntax:
Example: Python350.265482457436690 50.265482457436691 50.265482457436692
50.265482457436693 50.265482457436694 50.265482457436695 Output: 2.718281828459045 PiYou all must be familiar with pi. The pi is depicted as either 22/7 or 3.14. math.pi provides a more precise value for the pi. Syntax:
Example 1: Python350.265482457436690 50.265482457436691 50.265482457436692
50.265482457436699 50.265482457436694 6.2831853071795861 Output: 3.141592653589793 Example 2: Let’s find the area of the circle Python350.265482457436690 50.265482457436691 50.265482457436692
6.2831853071795865 6.2831853071795866 6.2831853071795867 6.2831853071795868
6.2831853071795869 inf -inf0 6.2831853071795867 inf -inf2
inf -inf3 50.265482457436694 inf -inf5 inf -inf6 6.2831853071795866 inf -inf6 inf -inf9 Output: 50.26548245743669 TauTau is defined as the ratio of the circumference to the radius of a circle. The math.tau constant returns the value tau: 6.283185307179586. Syntax:
Example: Python350.265482457436690 50.265482457436691 50.265482457436692
True True3 50.265482457436694 True True5 Output: 6.283185307179586 InfinityInfinity basically means something which is never-ending or boundless from both directions i.e. negative and positive. It cannot be depicted by a number. The math.inf constant returns of positive infinity. For negative infinity, use -math.inf. Syntax:
Example 1: Python350.265482457436690 50.265482457436691 50.265482457436692
True True9 50.265482457436694 nan1
nan2 50.265482457436694 nan4 nan5 nan6 Output: inf -inf Example 2: Comparing the values of infinity with the maximum floating point value Python350.265482457436690 50.265482457436691 50.265482457436692
50.265482457436694 The ceil of 2.3 is : 3 The floor of 2.3 is : 21 The ceil of 2.3 is : 3 The floor of 2.3 is : 22 The ceil of 2.3 is : 3 The floor of 2.3 is : 23 50.265482457436694 nan4 nan5 The ceil of 2.3 is : 3 The floor of 2.3 is : 27 nan5 The ceil of 2.3 is : 3 The floor of 2.3 is : 22 The ceil of 2.3 is : 3 The floor of 2.3 is : 23 Output: True True NaNThe math.nan constant returns a floating-point nan (Not a Number) value. This value is not a legal number. The nan constant is equivalent to float(“nan”). Example: Python350.265482457436690 50.265482457436691 50.265482457436692
The factorial of 5 is : 1204 50.265482457436694 The factorial of 5 is : 1206 Output: nan Numeric FunctionsIn this section, we will deal with the functions that are used with number theory as well as representation theory such as finding the factorial of a number. Finding the ceiling and the floor valueCeil value means the smallest integral value greater than the number and the floor value means the greatest integral value smaller than the number. This can be easily calculated using the ceil() and floor() method respectively. Example: Python3The factorial of 5 is : 1207 The factorial of 5 is : 1208
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 The gcd of 5 and 15 is : 54
The gcd of 5 and 15 is : 55 50.265482457436694 nan4 The gcd of 5 and 15 is : 58 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 3.14159265358979303
3.14159265358979304 50.265482457436694 nan4 3.14159265358979307 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 3.14159265358979312 Output: The ceil of 2.3 is : 3 The floor of 2.3 is : 2 Finding the factorial of the numberUsing the factorial() function we can find the factorial of a number in a single line of the code. An error message is displayed if number is not integral. Example: Python3The factorial of 5 is : 1207 3.14159265358979314
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 3.14159265358979320
3.14159265358979321 50.265482457436694 nan4 3.14159265358979324 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 3.14159265358979329 Output: The factorial of 5 is : 120 Finding the GCDgcd() function is used to find the greatest common divisor of two numbers passed as the arguments. Example: Python3The factorial of 5 is : 1207 3.14159265358979331
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 3.14159265358979337 3.14159265358979338 6.2831853071795867 3.14159265358979320
3.14159265358979341 50.265482457436694 nan4 3.14159265358979344 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 3.14159265358979349 Output: The gcd of 5 and 15 is : 5 Finding the absolute valuefabs() function returns the absolute value of the number. Example: Python3The factorial of 5 is : 1207 3.14159265358979351
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 nan5 3.14159265358979358
3.14159265358979359 50.265482457436694 nan4 3.14159265358979362 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 3.14159265358979367 Output: 3.1415926535897930 Refer to the below article to get detailed information about the numeric functions.
Logarithmic and Power FunctionsPower functions can be expressed as x^n where n is the power of x whereas logarithmic functions are considered as the inverse of exponential functions. Finding the power of expexp() method is used to calculate the power of e i.e. or we can say exponential of y.Example: Python33.14159265358979368 3.14159265358979369 50.265482457436691 50.265482457436692
3.14159265358979372 3.14159265358979373 6.2831853071795867 6.2831853071795868 3.14159265358979376 6.2831853071795867 nan5 3.14159265358979379 3.14159265358979380 6.2831853071795867 3.14159265358979382
3.14159265358979383 3.14159265358979384 50.265482457436694 3.14159265358979386 50.265482457436694 3.14159265358979388 50.265482457436694 3.14159265358979390 Output: 3.1415926535897931 Finding the power of a numberpow() function computes x**y. This function first converts its arguments into float and then computes the power. Example: Python33.14159265358979391 3.14159265358979392
50.265482457436694 nan4 3.14159265358979395 3.14159265358979396 6.2831853071795867 3.14159265358979301
3.14159265358979399 50.265482457436694 nan4 50.2654824574366902 nan4 3.14159265358979379 50.2654824574366905 6.2831853071795868 50.2654824574366907 Output: 3.1415926535897932 Finding the Logarithm
Python3The factorial of 5 is : 1207 50.2654824574366909
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
50.2654824574366913 50.265482457436694 nan4 50.2654824574366916 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 50.2654824574366921 50.2654824574366922 50.2654824574366905 3.14159265358979379 50.2654824574366907
50.2654824574366926 50.265482457436694 nan4 50.2654824574366929 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 50.2654824574366934 50.2654824574366935 50.2654824574366907 50.2654824574366937 50.2654824574366938 50.265482457436694 nan4 50.2654824574366941 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 50.2654824574366946 50.2654824574366947 50.2654824574366907 Output: 3.1415926535897933 Finding the Square rootsqrt() function returns the square root of the number. Example: Python350.2654824574366949 50.2654824574366950
50.2654824574366951 50.265482457436691 50.265482457436692
50.2654824574366954 50.265482457436694 50.2654824574366956 50.2654824574366957 50.2654824574366907
50.2654824574366959 50.265482457436694 50.2654824574366956 6.2831853071795868 50.2654824574366907
50.2654824574366964 50.265482457436694 50.2654824574366956 50.2654824574366967 50.2654824574366907 Output: 3.1415926535897934 Refer to the below article to get detailed information about the Logarithmic and Power Functions
Trigonometric and Angular FunctionsYou all must know about Trigonometric and how it may become difficult to find the values of sine and cosine values of any angle. Math module provides built-in functions to find such values and even to change the values between degrees and radians. Finding sine, cosine, and tangentsin(), cos(), and tan() functions returns the sine, cosine, and tangent of value passed as the argument. The value passed in this function should be in radians. Example: Python3The factorial of 5 is : 1207 50.2654824574366970
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 inf -inf2 50.2654824574366977 50.2654824574366978
50.2654824574366979 50.265482457436694 nan4 50.2654824574366982 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 50.2654824574366987
50.2654824574366988 50.265482457436694 nan4 50.2654824574366991 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 50.2654824574366996
50.2654824574366997 50.265482457436694 nan4 6.28318530717958600 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 6.28318530717958605 Output: 3.1415926535897935 Converting values from degrees to radians and vice versa
Example: Python3The factorial of 5 is : 1207 6.28318530717958607
The factorial of 5 is : 1209 50.265482457436691 50.265482457436692
The gcd of 5 and 15 is : 52 6.2831853071795867 inf -inf2 50.2654824574366977 50.2654824574366978 3.14159265358979338 6.2831853071795867 6.28318530717958618
6.28318530717958619 50.265482457436694 nan4 6.28318530717958622 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 6.28318530717958627
6.28318530717958628 50.265482457436694 nan4 6.28318530717958631 The gcd of 5 and 15 is : 59 6.2831853071795867 3.14159265358979301 50.265482457436694 6.28318530717958636 Output: 3.1415926535897936 Refer to the below articles to get detailed information about the trigonometric and angular functions.
Special FunctionsBesides all the numeric, logarithmic functions we have discussed yet, the math module provides some more useful functions that does not fall under any category discussed above but may become handy at some point while coding. |