In this tutorial, we’ll learn what Python math functions are and how to use them. We’ll understand this concept using practical Python code examples.
Outline
- Introduction: Python Math Functions
- Implementing Python Math Functions (10 Examples)
- Example 1: Ceil Function
- Example 2: Floor Function
- Example 3: Factorial Function
- Example 4: Is Nan Function
- Example 5: Fabs Function
- Example 6: Square Root Function
- Example 7: Power Function
- Example 8: Sin Function
- Example 9: Cos Function
- Example 10: Tan Function
- Conclusion
Introduction: Python Math Functions
These are mathematical functions that can be used in Python programs. We’ve already some built-in functions or we can make our our custom functions in Python but in order to perform complex mathematical calculations, we’ve to use math module.
Module can be taken as a file in which some code is specified that can be reused. It’s useful to avoid bulkiness of code and it saves time as well. In order to use that, we’ve to import math module. Let’s now understand it using practical Python code examples.
Implementing Python Math Functions (10 Examples)
First, we’ve to import the math module in our Python file. See below:
import math
Example 1: Ceil Function
import math print(math.ceil(2.1)) print(math.ceil(2)) print(math.ceil(2.8)) print(math.ceil(3.5)) print(math.ceil(8))
Output
3 2 3 4 8
This method is used to return the smallest integer but greater than the floating number specified. If an integer is specified, then it’ll return the same integer value.
Example 2: Floor Function
import math print(math.floor(3.7)) print(math.floor(5)) print(math.floor(2.9)) print(math.floor(4.1)) print(math.floor(9))
Output
3 5 2 4 9
This method returns the greatest integer value but less than the value specified in floor function. If its an integer value then the same value will be returned.
Example 3: Factorial Function
import math print(math.factorial(7)) print(math.factorial(3)) print(math.factorial(6)) print(math.factorial(12))
Output
5040 6 720 479001600
This function is used to return the factorial of value specified. Just make sure that the specified value is integer only. Float or negative value is not acceptable and will raised an error, if used.
Example 4: Is Nan Function
import math print(math.isnan(7)) print(math.isnan(2.5)) print(math.isnan(5)) print(math.isnan(0)) print(math.isnan(0.0))
Output
False False False False False
The isnan function returns a Boolean(True/False) value. It returns True if the specified value is NaN(means not a number) and returns False, if it’s a number.
Example 5: Fabs Function
import math print(math.fabs(4)) print(math.fabs(3.84848)) print(math.fabs(0.74774)) print(math.fab(65)) print(math.fabs(0))
Output
4.0 3.84848 0.74774 65.0 0.0
This function is used to return the absolute value of the specified item, as shown in the above output.
Example 6: Square Root Function
import math print(math.sqrt(4)) print(math.sqrt(2.2)) print(math.sqrt(10)) print(math.sqrt(20.43)) print(math.sqrt(0))
Output
2.0 1.4832396974191326 3.1622776601683795 4.519955751995809 0.0
The sqrt function is used to return the square root of value passed to this function as an argument. Negative values are not acceptable and will raise value error, if used.
Example 7: Power Function
import math print(math.pow(4, 2)) print(math.pow(2, 8)) print(math.pow(10, 4)) print(math.pow(4.2, 2)) print(math.pow(8, 2.2)) print(math.pow(6.8, 4.2))
Output
16.0 256.0 10000.0 17.64 97.00586025666551 3137.165521816043
This function is used to specify the second argument as a power to the first one and return its result, as shown in the above code.
Example 8: Sin Function
import math print(math.sin(4)) print(math.sin(15.5)) print(math.sin(10)) print(math.sin(-20)) print(math.sin(40)) print(math.sin(0))
Output
-0.7568024953079282 0.2064674819377966 -0.5440211108893698 -0.9129452507276277 0.7451131604793488 0.0
This method applies the sin theta formula on the value passed to it as an argument and returns the result.
Example 9: Cos Function
import math print(math.cos(2)) print(math.cos(23.54)) print(math.cos(9)) print(math.cos(-29)) print(math.cos(80)) print(math.cos(0))
Output
-0.4161468365471424 -0.02194314059956344 -0.9111302618846769 -0.7480575296890004 -0.11038724383904756 1.0
This function applies cos theta formula on its value and returns the result.
Example 10: Tan Function
import math print(math.tan(3)) print(math.tan(14.345)) print(math.tan(7)) print(math.tan(-65)) print(math.tan(21)) print(math.tan(0))
Output
-0.1425465430742778 -4.742076063955321 0.8714479827243188 1.4700382576631723 -1.5274985276366035 0.0
This function applies tan theta on its value and the result is returned.
So this is how we can use Python math functions. Click here to explore more math functions in Python.
Don’t hesitate to ask if you have any questions concerning Python math functions. We’ll be more than happy to answer all.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical understanding of how to use Python math functions. We’ll be looking forward to receive your amazing feedback on this article.
Do visit our other posts on Python programming. Thank you for reading this article.