Built-in Mathematical Functions in python
Python supports a lot of built-in functions, used to perform various tasks easily. Functions that are inbuilt within Python are called built-in functions.
A function is a group of statements that perform specific tasks. Here are a few mathematical built-in functions used by most people to reduce their time for solving these kinds of problems.
List of Some Built-in functions
- abs()
- bin()
- min()
- max()
- sum()
- id()
- type()
- format()
- round()
- pow()
abs()
abs() returns an absolute value of a number. The argument may be an integer or a floating-point number.
Example:
#Absolute value x = 5 y = 5.1 print("x = ", x) print("y = ", y) #Output x = 5 y = 5.1
bin()
bin() returns a binary string prefixed with “0b” for the given integer number.
Example:
#binary values x = 15 y = 5 print ('15 in binary : ',bin (x)) print ('5 in binary : ',bin (y)) #Output 15 in binary : 0b1111 5 in binary : 0b101
min()
min() returns the lowest or smallest value in a list.
Example:
#Minimum value list1 = [ 17, 24 ,10, 64, 3, 67 ] print(min(list1) #Output 3
max()
max() returns the highest or largest value in a list.
Example:
#Maximum value list1 = [ 17, 24 ,10, 64, 3, 67 ] print(max(list1) #Output 67
sum()
sum() returns the sum of all elements in the list.
Example:
#sum of all values list1 = [ 17, 24 ,10, 64, 3, 67 ] print(sum(list1) #Output 185
id()
id( ) returns the address of an object in the stored memory. The address of the object differs from one system to another.
Example:
#address of an object Blog = "Iterathon" print(id(Blog) #Output 3018363260528
type()
type() returns the type of object for the given variable.
Example:
#type of an object Blog = "Iterathon" add = 5+7 print(type(Blog)) print(type(add)) #Output <class 'str'> <class 'int'>
format()
format() returns the output based on the user input. For example, the user asks for a binary format then the format() function returns a value in binary.
Example:
# convert to given format x= 10 print ('x value in binary :',format(x,'b')) print ('y value in octal :',format(x,'o')) #output x value in binary : 1010 y value in octal : 12
round()
round() returns the nearest integer to the given value. It also allows specifying the number of decimal digits desired after rounding.
Example:
#Rounding the given value x = 10.5 print ('x value is rounded to', round (x)) y = 20.25 print ('y value is rounded in specific values',round (y,1)) #Output x value is rounded to 10 y value is rounded in specific values 20.2
pow()
pow() returns a value in square format. For example, in mathematical 52 = 25
Example:
#Square values a= 10 b= 2 print (pow (a,b)) #Output 100
My Previous Blog
10 awesome python tips and tricks you must learn today: https://iterathon.tech//10-awesome-python-tips-and-tricks-you-should-learn/
LEARN MORE ❣