In this post, we will learn about the built-in math functions of Python.
Do you know that in Python you can use some built-in functions of Python without using the Math module?
If you do not know, after reading this Python Math built-in function tutorial, you will know and will also learn how to use Python’s in-built function.
First of all, let us know how many built-in math functions are functions in Python that can be used to perform a mathematical operation.
You can see in the list below how many built-in math functions/methods of Python are there:
Python math built-in functions
|rounds the number, you can also specify precision in the second argument|
|Return absolute value of |
|Returns largest value among supplied arguments|
|Returns smallest value among supplied arguments|
| ||Compare x and y , returning a number.|
| ||Create a hexadecimal string representation of number .|
| ||Create a octal string representation of number .|
| ||Generates an integer from the string x .|
| || The |
In the list given above, you have seen how many in-built math functions/methods are in Python.
Now we will tell you about every python built-in method/function of the above-given list in detail and also take an example to understand you better.
Round method in Python
In Python, the round method rounded off the given number. If you have not specified how many decimals points to rounding off given number then it rounds the given number in the integer.
Example: python round method
x = round(5.5) print(x) x = round(6.76543, 2) print(x)
Pow method in python
In python, pow () methods are used to extract the power of a number.Such as x power y (xy).
pow(x, y, z)
Example: pow example python
Let us see Python’s built-in pow () method with an example:
x = pow(2, 3) print(x)
Abs method in python
One more great in-built method is available in Python. In Python, the ABS () method is used when the absolute value of a given number is to be found.
Example: ABS method in python
x = abs(-5.50) print(x)
Min method in python
In Python, the min() method is used when the smallest value is to be obtained from a list or a given input list.
min(n1, n2, n3, …)
Example: python get min of list
x = min(5, 10, 15, 25, 35, 45, 55) # list of items print(x) // output
Max method in python
In Python, the max() method is used when the largest value is to be obtained from a list or a given input list.
max(n1, n2, n3, …)
Example: python get max of list
x = max(5, 10, 15, 25, 35, 45, 55) # list of items print(x)
CMP method in python
The CMP() method in Python compares the two given objects and returns a result of 1,- 1, 0.
Example: python cmp function
# when x<y x = 1 y = 2 print(cmp(x, y)) # when x = y x = 2 y = 2 print(cmp(x, y)) # when x>y x = 3 y = 2 print(cmp(x, y))
-1 0 1
Python hex method
Use of hex () method in python changed the given number to hexadecimal value.
Example: python hex function
x = hex(555) print(x)
Python oct method
Use of oct() method in python changed the given number to octal string.
Example: python oct function
x = oct(555) print(x)
int() method in python
The int() method returns an integer object from any number or string.
Example: string to int in python
# integer print("int(123) is:", int(123)) # float print("int(123.23) is:", int(123.23)) # string print("int('123') is:", int('123'))
int(123) is: 123 int(123.23) is: 123 int('123') is: 123
Python len method
len() the function returns the number of items in an object.
Example: get length of list python
mylist = [1, 2, 3, 4, 5] x = len(mylist) print(x)