Wednesday, November 20, 2024
Google search engine
HomeLanguagesPython Built In Math Functions

Python Built In Math Functions

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

Method Description
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 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.

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
RELATED ARTICLES

Most Popular

Recent Comments