Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be studying about the Python Mathematical Functions.
I will be explaining each of these functions that was listed in our previous tutorial in details with the help of an example. Now lets start with the abs() function.
I will be explaining each of these functions that was listed in our previous tutorial in details with the help of an example. Now lets start with the abs() function.
Python Number abs() Function
The Python number function abs() returns an absolute value of x - the positive distance between x and zero.
Syntax
The following below is the syntax for abs() function -
abs( x )
Parameter Values
- x - This is a numeric value.
Return Value
This function returns the absolute value of x.
Example
The following example shows the usage of abs() function -
#!/usr/bin/python print "abs(-45) : ", abs(-45) print "abs(100.12) : ", abs(100.12) print "abs(119L) : ", abs(119L)
Output
Below is the output of the above example -
abs(-45) : 45 abs(100.12) : 100.12 abs(119L) : 119
RECOMMENDED: Python Numbers
Python Number ceil() Function
The Python number function ceil() returns the ceiling value of x - the smallest integer not less than x.
Syntax
The following below is the syntax for ceil() function -
import math math.ceil( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the smallest integer not less than x.
Example
The following example shows the usage of ceil() function -
#!/usr/bin/python import math # This will import math module print "math.ceil(-45.17) : ", math.ceil(-45.17) print "math.ceil(100.12) : ", math.ceil(100.12) print "math.ceil(100.72) : ", math.ceil(100.72) print "math.ceil(119L) : ", math.ceil(119L) print "math.ceil(math.pi) : ", math.ceil(math.pi)
Output
Below is the output of the above example -
math.ceil(-45.17) : -45.0 math.ceil(100.12) : 101.0 math.ceil(100.72) : 101.0 math.ceil(119L) : 119.0 math.ceil(math.pi) : 4.0
Python Number cmp() Function
The number cmp() function return the sign of the difference of two numbers : -1 if x < y, 0 if x == y, or 1 if x > y.
Syntax
The following below is the syntax for cmp() function -
cmp( x, y )
Parameter Values
- x - This is a numeric value.
- y - This is also a numeric value.
Return Value
This function returns -1 if x < y, 0 if x == y, 1 if x > y.
Example
The following example shows the usage of cmp() function -
#!/usr/bin/python print "cmp(80, 100) : ", cmp(80, 100) print "cmp(180, 100) : ", cmp(180, 100) print "cmp(-80, 100) : ", cmp(-80, 100) print "cmp(80, -100) : ", cmp(80, -100)
Output
Below is the output of the above example -
cmp(80, 100) : -1 cmp(180, 100) : 1 cmp(-80, 100) : -1 cmp(80, -100) : 1
RECOMMENDED POST: Python Basic Syntax Tutorial
Python Number exp() Function
Number function exp() returns the exponential of x ie. ex.
Syntax
The following below is the syntax for exp() function -
import math math.exp( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the exponential of x.
Example
The following example shows the usage of exp() function -
#!/usr/bin/python import math # This will import math module print "math.exp(-45.17) : ", math.exp(-45.17) print "math.exp(100.12) : ", math.exp(100.12) print "math.exp(100.72) : ", math.exp(100.72) print "math.exp(119L) : ", math.exp(119L) print "math.exp(math.pi) : ", math.exp(math.pi)
Output
Below is the output of the above example -
math.exp(-45.17) : 2.41500621326e-20 math.exp(100.12) : 3.03084361407e+43 math.exp(100.72) : 5.52255713025e+43 math.exp(119L) : 4.7978133273e+51 math.exp(math.pi) : 23.1406926328
Python Number fabs() Function
Python number function fabs() returns the absolute value of x.
Syntax
The following below is the syntax for fabs() function -
import math math.fabs( x )
Note - This function is not directly accessible, so we need to import the math module and then we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the absolute value of x.
Example
The following example shows the usage of fabs() function -
#!/usr/bin/python import math # This will import math module print "math.fabs(-45.17) : ", math.fabs(-45.17) print "math.fabs(100.12) : ", math.fabs(100.12) print "math.fabs(100.72) : ", math.fabs(100.72) print "math.fabs(119L) : ", math.fabs(119L) print "math.fabs(math.pi) : ", math.fabs(math.pi)
Output
Below is the output of the above example -
math.fabs(-45.17) : 45.17 math.fabs(100.12) : 100.12 math.fabs(100.72) : 100.72 math.fabs(119L) : 119.0 math.fabs(math.pi) : 3.14159265359
RECOMMENDED POST: An Overview on Python Programming
Python Number floor() Function
Python number function floor() returns the floor of x. The largest integer not greater than x.
Syntax
The following below is the syntax for floor() function -
import math math.floor( x )
Note - This function is not directly accessible, so we need to import the math module and then we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the largest integer not greater than x.
Example
The following example shows the usage of floor() function -
#!/usr/bin/python import math # This will import math module print "math.floor(-45.17) : ", math.floor(-45.17) print "math.floor(100.12) : ", math.floor(100.12) print "math.floor(100.72) : ", math.floor(100.72) print "math.floor(119L) : ", math.floor(119L) print "math.floor(math.pi) : ", math.floor(math.pi)
Output
Below is the output of the above example -
math.floor(-45.17) : -46.0 math.floor(100.12) : 100.0 math.floor(100.72) : 100.0 math.floor(119L) : 119.0 math.floor(math.pi) : 3.0
Python Number log() Function
The Python number function log() returns the natural logarithm of x, for x > 0.
Syntax
The following below is the syntax for log() function -
import math math.log( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the natural logarithm of x, for x > 0.
Example
The following example shows the usage of log() function -
#!/usr/bin/python import math # This will import math module print "math.log(100.12) : ", math.log(100.12) print "math.log(100.72) : ", math.log(100.72) print "math.log(119L) : ", math.log(119L) print "math.log(math.pi) : ", math.log(math.pi)
Output
Below is the output of the above example -
math.log(100.12) : 4.60636946656 math.log(100.72) : 4.61234438974 math.log(119L) : 4.77912349311 math.log(math.pi) : 1.14472988585
RECOMMENDED POST: Introduction to Python Programming
Python Number log10() Function
Python number function log10() returns the base-10 logarithm of x, for x > 0.
Syntax
The following below is the syntax for log10() function -
import math math.log10( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This returns the base-10 logarithm of x, for x > 0.
Example
The following example shows the usage of log10() function -
#!/usr/bin/python import math # This will import math module print "math.log10(100.12) : ", math.log10(100.12) print "math.log10(100.72) : ", math.log10(100.72) print "math.log10(119L) : ", math.log10(119L) print "math.log10(math.pi) : ", math.log10(math.pi)
Output
Below is the output of the above example -
math.log10(100.12) : 2.00052084094 math.log10(100.72) : 2.0031157171 math.log10(119L) : 2.07554696139 math.log10(math.pi) : 0.497149872694
Python Number max() Function
Python number function max() returns back the largest of its arguments. Value that is closest to positive infinity.
Syntax
The following below is the syntax for max() function -
max( x, y, z, ........ )
Parameter Values
- x - This is a numeric value.
- y - This is also a numeric value.
- z - This is also a numeric value.
Return Value
This function returns the largest of its arguments.
Example
The following example shows the usage of max() function -
#!/usr/bin/python print "max(80, 100, 1000) : ", max(80, 100, 1000) print "max(-20, 100, 400) : ", max(-20, 100, 400) print "max(-80, -20, -10) : ", max(-80, -20, -10) print "max(0, 100, -400) : ", max(0, 100, -400)
Output
Below is the output of the above example -
max(80, 100, 1000) : 1000 max(-20, 100, 400) : 400 max(-80, -20, -10) : -10 max(0, 100, -400) : 100
RECOMMENDED POST: How to Setup a Python Development Environment
Python Number min() Function
The Python number function min() returns back the smallest of its arguments. Value that is closest to negative infinity.
Syntax
The following below is the syntax for min() function -
min( x, y, z, ........ )
Parameter Values
- x - This is a numeric value.
- y - This is also a numeric value.
- z - This is also a numeric value.
Return Value
This function returns the smallest of its arguments.
Example
The following example shows the usage of min() function -
#!/usr/bin/python print "min(80, 100, 1000) : ", min(80, 100, 1000) print "min(-20, 100, 400) : ", min(-20, 100, 400) print "min(-80, -20, -10) : ", min(-80, -20, -10) print "min(0, 100, -400) : ", min(0, 100, -400)
Output
Below is the output of the above example -
min(80, 100, 1000) : 80 min(-20, 100, 400) : -20 min(-80, -20, -10) : -80 min(0, 100, -400) : -400
Python Number modf() Function
The Python number function modf() returns the fractional and has well as integer parts of x in a two-item tuple. Both parts has the same sign as x. The integer part is returned as a float.
Syntax
The following below is the syntax for modf() function -
import math math.modf( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numeric value.
Return Value
This function returns the fractional and integer parts of x in a two-item tuple.
Example
The following example shows the usage of modf() function -
#!/usr/bin/python import math # This will import math module print "math.modf(100.12) : ", math.modf(100.12) print "math.modf(100.72) : ", math.modf(100.72) print "math.modf(119L) : ", math.modf(119L) print "math.modf(math.pi) : ", math.modf(math.pi)
Output
Below is the output of the above example -
math.modf(100.12) : (0.12000000000000455, 100.0) math.modf(100.72) : (0.71999999999999886, 100.0) math.modf(119L) : (0.0, 119.0) math.modf(math.pi) : (0.14159265358979312, 3.0)
RECOMMENDED POST: Python Operators
Python Number pow() Function
The Python number function pow() returns x to the power of y. If the third argument z is given, it returns x to the power of y modulus z.
Syntax
The following below is the syntax for pow() function -
import math math.pow( x, y[, z] )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - The number which is to be powered.
- y - The number which is to be powered with x.
- z - (Optional) The number which is to be used for the modulus operation.
Return Value
This returns the value of xy.
Example
The following example shows the usage of pow() function -
#!/usr/bin/python import math # This will import math module print "math.pow(100, 2) : ", math.pow(100, 2) print "math.pow(100, -2) : ", math.pow(100, -2) print "math.pow(2, 4) : ", math.pow(2, 4) print "math.pow(3, 0) : ", math.pow(3, 0)
Output
Below is the output of the above example -
math.pow(100, 2) : 10000.0 math.pow(100, -2) : 0.0001 math.pow(2, 4) : 16.0 math.pow(3, 0) : 1.0
Python Number round() Function
Python number function round() returns x rounded to n digits from the decimal point.
Syntax
The following below is the syntax for round() function -
round( x, [, n] )
Parameter Values
- x - The is a numerical value.
- n - The is also a numerical value.
Return Value
It returns x rounded to n digits from the decimal point.
Example
The following example shows the usage of round() function -
#!/usr/bin/python print "round(80.23456, 2) : ", round(80.23456, 2) print "round(100.000056, 3) : ", round(100.000056, 3) print "round(-100.000056, 3) : ", round(-100.000056, 3)
Output
Below is the output of the above example -
round(80.23456, 2) : 80.23 round(100.000056, 3) : 100.0 round(-100.000056, 3) : -100.0
RECOMMENDED: JavaScript Events tutorial with examples
Python Number sqrt() Function
The Python number function sqrt() returns the square root of x for x > 0.
Syntax
The following below is the syntax for pow() function -
import math math.sqrt( x )
Note - This function is not directly accessible, so we need to import the math module and we need to call this function using math static object.
Parameter Values
- x - This is a numerical value.
Return Value
This returns the square root of x for x > 0.
Example
The following example shows the usage of sqrt() function -
#!/usr/bin/python import math # This will import math module print "math.sqrt(100) : ", math.sqrt(100) print "math.sqrt(7) : ", math.sqrt(7) print "math.sqrt(math.pi) : ", math.sqrt(math.pi)
Output
Below is the output of the above example -
math.sqrt(100) : 10.0 math.sqrt(7) : 2.64575131106 math.sqrt(math.pi) : 1.77245385091
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Random Number Functions.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.