Python Math Functions | Built-in Math Function

Python math/Mathematical functions; Through this tutorial, i am going to show you built-in math functions of Python.

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 in-built function.

Python math built-in functions

You can see in the list below to built-in math or Mathematical functions/methods of Python are there:

MethodDescription
round(number[, ndigits]) rounds the number, you can also specify precision in the second argument
pow(a, b)Returns a raise to the power of b
abs(x)Return absolute value of x
max(x1, x2, ..., xn)Returns largest value among supplied arguments
min(x1, x2, ..., xn)Returns smallest value among supplied arguments
cmp x , y ) Compare x and y , returning a number.
hex ( number ) Create a hexadecimal string representation of number .
oct number ) Create a octal string representation of number .
int ( string , [ base ])  Generates an integer from the string x . 
len()  The len() function returns the number of items in an object.

In the list given above, you have seen how many built-in math functions/methods are in Python.

Now you will learn 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.

Syntax

round(number, digits)

Example: python round method

x = round(5.5)
print(x)
x = round(6.76543, 2)
print(x)

Output

6 
6.77 

Pow method in python

In python, pow () methods are used to extract the power of a number.Such as x power y (xy).

Syntax

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.

Syntax

abs(n)

Example: ABS method in python

x = abs(-5.50)
print(x)

Output

5.50

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.

Syntax

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

Output

5

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.

Syntax

max(n1, n2, n3, …)

Example: python get max of list

x = max(5, 10, 15, 25, 35, 45, 55) # list of items
print(x)

Output

55

CMP method in python

The CMP() method in Python compares the two given objects and returns a result of 1,- 1, 0.

Syntax

cmp(a, b)

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)) 

Output

-1
 0 
 1

Python hex method

Use of hex () method in python changed the given number to hexadecimal value.

Syntax

hex(number)

Example: python hex function

x = hex(555)
print(x)

Output

 0x22b

Python oct method

Use of oct() method in python changed the given number to octal string.

Syntax

oct(number)

Example: python oct function

x = oct(555)
print(x)

Output

 0o1053

int() method in python

The int() method returns an integer object from any number or string.

Syntax

 int(x=0, base=10)

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'))

Output

 int(123) is: 123
 int(123.23) is: 123
 int('123') is: 123

Python len method

The len() the function returns the number of items in an object.

Syntax

 len(object)

Example: get length of list python

mylist = [1, 2, 3, 4, 5]
x = len(mylist)
print(x)

Output

5

Recommended Python Tutorials

Recommended:-Functions in Python
Recommended:-Python Modules
Recommended:-Python Lists
Recommended:-Python Strings

Leave a Comment