Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

#Built-in Math Functions

#The min() and max() functions can be used to find the lowest or highest value in an iterable:

# round(): This function returns a floating point number that is rounded version of the specified number.

#abs() :The abs() function returns the absolute (positive) value of the specified number:

#pow():The pow(x, y) function returns the value of x to the power of y (xy).This function requires two
parameter first is base and second is power.

#Eg

x = min(5, 10, 25)

y = max(5, 10, 25)

print(x)

print(y)

print(round(10.2345))

print(round(5.76543))

print(abs(-5))

print(abs(5))

print("2 raised to 5:", pow(2,5))

print("7 raised to 0:", pow(7,0))

output:

25

10

2 raised to 5: 32
7 raised to 0: 1

#Built-in Math Functions(math Module)

Python has also a built-in module called math, which extends the list of mathematical functions.

To use it, you must import the math module:

import math

#ceil(): The math.ceil() method rounds a number upwards to its nearest integer,

#floor(): method rounds a number downwards to its nearest integer, and returns the result:

import math

x = math.ceil(1.4)

y = math.floor(1.4)

print(x) # returns 2

print(y) # returns 1

#The math.sqrt() method for example, returns the square root of a number:

#The math.pi constant, returns the value of PI (3.14...):

import math

a = math.sqrt(64)

print(a) #returns 8.0

x = math.pi

print(x) #returns 3.1415

y = math.cos(3)

print(y) # returns-0.9899924966004454
z = math.sin(6)

print(z) # returns -0.27941549819892586

# Data type and object RElated Functions

#type(): Returns the type(datatype) of parameter value/object.

a=10

b=True

c=16.55

d=[1,2,3]

print("Type of a is :",type(a))

print("Type of b is :",type(b))

print("Type of c is :",type(c))

print("Type of d is :",type(d))

Output:Type of a is : <class 'int'>

Type of b is : <class 'bool'>

Type of c is : <class 'float'>

Type of d is : <class 'list'>

ID(): This Function returns the id of an object.

list1=[10,20,30]

list2=[40,50,60]

tuple=(70,80,90)

print("ID of List1 is :",id(list1))

print("ID of List2 is :",id(list2))

print("ID of tuple is :",id(tuple))

Output: ID of List1 is : 1873523454784


ID of List2 is : 1873523389056

ID of tuple is : 1873523446272

Isinstance():This Function returns True,if specified object is an instance of specified class.otherwise


returns False.

a=10

print("a is instance of int:",isinstance(a,int))

list=[10,20,30]

print("list is instance of tuple:",isinstance (list,tuple))

output: a is instance of int: True

list is isinstance of tuple: False

Data Conversion Functions

Bin() :This Functions returns the binary conversion of specified integer parameter.

Hex(): This Function returns the hexa-decimal conversion of specified integer .

Oct():This Function returns the octal conversion of specified integer parameter.

Bool(): This Function returns the Boolean equivalent of specified parameter.

Float(): This function converts and return provided parameter value into ‘float’.

Int():This Function converts and returns provided parameter value into ‘int’.

Tuple(): This Function converts specified data –structure inot tuple and returns.

List():This Function converts specified data structure into list and returns.

print("Binary equivalent of 15 is :",bin(15))

print("Hexa-decimal equivalent of 15 is :",hex(15))

print("octal equivalent of 15 is :",oct(15))

print("Boolean equivalent of 15 is :",bool(15))

print("Float equivalent of 3.5 is :",float(3.5))


print("Integer equivalent of 15 is :",int(15))

tuple=(10,20,30)

a=list(tuple)

print("Tuple conversion of List is ", a ,"and its type is" ,type(a))

tuple=(1,2,3,4)

b=set(tuple)

print("Tuple conversion of Set is ", b ,"and its type is" ,type(b))

Output:

Binary equivalent of 15 is : 0b1111

Hexa-decimal equivalent of 15 is : 0xf

octal equivalent of 15 is : 0o17

Boolean equivalent of 15 is : True

Float equivalent of 3.5 is : 3.5

Integer equivalent of 15 is : 15

Tuple conversion of List is [10, 20, 30] and its type is <class 'list'>

Tuple conversion of Set is {1, 2, 3, 4} and its type is <class 'set'>

You might also like