Basic fundamentals of python part 4

By
There are many built-in functions available in Python Language. We can use this functions in any program and make your program easy.Here we will learn some important mathematical functions and use it in python programs.
Built-in Mathematical functions:-
  • pow(x,y)               #value of x raised to y
  • abs(x)                  #absolute value of x
  • min(x1,x2)           #smallest value
  • max(x1,x2)          #largest value
  • divmod(x,y)         #return a pair(x//y,x%y)
  • bin(x)                  # binary value
  • oct(x)                  # octal value
  • hex(x)                 #hexadecimal value
  • round(x[,n])         # x rounded to n digit after decimal point
  • sum(number,start) #sum function contains two arguments,start is optional argument.
  • any(x)                     # it returns True or false value,x can be list,string , dictionary etc.
  • all(x)                       # it returns True or false value .x can be list , string , dictionary etc.
  • ascii(object)           # it return a string,it takes arguments like integer string,list,etc
  • bool(value)             # bool() function takes a single parameter,it return True OR False
  • complex(a,b)           #It returns a complex number.it takes integer value only.
Python Program to implement above functions:-

pow(2,3)           # 8 will be(x^y) returned
print("power value is=",pow(2,3))
pow(-2,2)          # 4 will be(x^y) returned
print("power value is=",pow(-2,2))
pow(-1,-2)         # 1.0 will be(x^y) returned
print("power value is=",pow(-1,-2))
pow(2.5,2)         # 6.25 will be(x^y) returned
print("power value is=",pow(2.5,2))
pow(5,3,4)         # 1 will be [(x**y)%z] returned
print("power value is=",pow(5,3,4))

abs(-20)           #integer absolute value is returned
print("absolute value is=",abs(-20))
abs(-10.55)        #float absolute value is returned
print("absolute value is=",abs(-10.55))
abs(2-3j)          #magnitude of the number is returned
print("absolute value is=",abs(2-3j))

min(5,-8)          # minimum value -8 is returned
print("minimum value is=",min(5,-8))
list1=[2,3,-2,5,9] # minimum value -2 is returned
min(list1)
print("minimum value is=",min(list1))
list2=['ram','mohan','abc'] #least ordered alphabet is returned
min(list2)
print("minimum value is=",min(list2))
dic={1:3,2:4,3:-1}  #minimum key value will print of dictionary
min(dic)
print("minimum value is=",min(dic))

max(-8,7)          # maximum value 7 is returned
print("maximum value is=",max(-8,7))
list1=[2,3,-2,5,9] # maximum value 9 is returned
max(list1)
print("maximum value is=",max(list1))
list2=['ram','mohan','abc'] #maximum ordered alphabet is returned
max(list2)
print("maximum value is=",max(list2))
dic={1:3,2:4,3:-1}  #maximum key value will print of dictionary
max(dic)
print("maximum value is=",max(dic))

divmod(5,2)        #5//2 and 5%2 will be returned
print("divmod value is=",divmod(5,2))
divmod(5.7,2.5)        #5//2 and 5%2 will be returned
print("divmod value is=",divmod(5.7,2.5))

bin(5)             # bin()fuction can contains three arguments(integer,octal,hexadecimal)
bin(0o5)
bin(0x5)
print("binary value is=",bin(5),bin(0o5),bin(0x5))

oct(5)            # oct()fuction can contains three arguments(integer,binary,hexadecimal) 
oct(0b101)
oct(0x5)
print("octal value is=",oct(5),oct(0b101),oct(0x5))

hex(5)            # hex()fuction can contains three arguments(integer,octal,binary) 
hex(0b101) 
hex(0o5)
print("hexadecimal value is=",hex(5),hex(0b101),hex(0o5))

round(10)         # it rounds 0 decimal palace,default value of round is 0(zero)
print("round value is=",round(10))
round(10.5)
print("round value is=",round(10.5))
round(10.5678,2)
print("round value is=",round(10.5678,2))
round(10.78432,3)
print("round value is=",round(10.78432,3))

#sum(number,start)   #sum function contains two arguments,start is optional argument.
number=[1,2,3,4,-1]
sum(number)
print("sum value is=",sum(number))
sum(number,10)      #add the value from left to right
print("sum value is=",sum(number,10))

#any(x)              # it returns True or false value .x can be list,string,dictionary etc.
x=[1,2,3,4]
any(x)
print("any value is=",any(x))
x=[0,False]
any(x)
print("any value is=",any(x))
x=[0,False,3,4]
any(x)
print("any value is=",any(x))
x=''
any(x)
print("any value is=",any(x))

#all(x)              # it returns True or false value .x can be list,string,dictionary etc.
x=[1,2,3,4]
all(x)
print("all value is=",all(x))
x=[0,False]
all(x)
print("all value is=",all(x))
x=[0,False,3,4]
all(x)
print("all value is=",all(x))
x=''
all(x)
print("all value is=",all(x))

#ascii(object)           # it return a string,it takes arguments like integer string,list,etc.
str='i am a student'
ascii(str)
print("ascii value is=",ascii(str))
str1="i am 'a' student"
ascii(str)
print("ascii value is=",ascii(str))
ascii(12)
print("ascii value is=",ascii(str))

#bool(value)        # bool() function takes a single parameter,it return True OR False
str=[1,2,3]
bool(str)
print("bool value is=",bool(str))
str=[]
bool(str)
print("bool value is=",bool(str))
str='0'
bool(str)
print("bool value is=",bool(str))
str=''
bool(str)
print("bool value is=",bool(str))

#complex number

real=2
imag=3
print('complex number =',complex(real,imag))
print('complex number =',complex(1.5,3.5))
print('complex number =',complex('2'))
#print('complex number =',complex('2','3'))  #can't take second argument if first is string   
c=4+5j
print('complex number Real part =',c.real)
print('complex number Real part =',c.imag)
print('complex number conjugate =',c.conjugate())
#mathematical operations in complex number
c1=4+6j
c2=1+2j
print('addition of two complex number=',c1+c2)
print('subtractions of two complex number=',c1-c2)
print('multiplication of two complex number=',c1*c2)
print('division of two complex number=',c1/c2) 

Python Program Output:-

power value is= 8
power value is= 4
power value is= 1.0
power value is= 6.25
power value is= 1
absolute value is= 20
absolute value is= 10.55
absolute value is= 3.605551275463989
minimum value is= -8
minimum value is= -2
minimum value is= abc
minimum value is= 1
maximum value is= 7
maximum value is= 9
maximum value is= ram
maximum value is= 3
divmod value is= (2, 1)
divmod value is= (2.0, 0.7000000000000002)
binary value is= 0b101 0b101 0b101
octal value is= 0o5 0o5 0o5
hexadecimal value is= 0x5 0x5 0x5
round value is= 10
round value is= 10
round value is= 10.57
round value is= 10.784
sum value is= 9
sum value is= 19
any value is= True
any value is= False
any value is= True
any value is= False
all value is= True
all value is= False
all value is= False
all value is= True
ascii value is= 'i am a student'
ascii value is= 'i am a student'
ascii value is= 'i am a student'
bool value is= True
bool value is= False
bool value is= True
bool value is= False
complex number = (2+3j)
complex number = (1.5+3.5j)
complex number = (2+0j)
complex number Real part = 4.0
complex number Real part = 5.0
complex number conjugate = (4-5j)
addition of two complex number= (5+8j)
subtractions of two complex number= (3+4j)
multiplication of two complex number= (-8+14j)
division of two complex number= (3.2-0.4j)

Note:-Complex number doesn't support comparison operators(c1>c2,c1<c2 etc.)
Watch Lecture 7 Complete Video:-


Library Functions:-
For performing sophisticated mathematical operations,we can use the functions present in modules:-
  • Math:- It is used for useful mathematics functions.
  • cmath:- It is used for performing operations on complex numbers.
  • decimal:- It is used for performing arithmetic operations.
  • random :- It is used for performing number random number generations. 
There are some mathematical functions in math module as follows:- 
  • pi,e                          # It is constants
  • sqrt(x)                     #calculate square root of x
  • fabs(x)                    # calculate absolute vale of float x    
  • factorial(x)              # calculate factorial of x
  • log(x)                     # calculate log of x       
  • log10(x)                 # calculate base 10 log of x
  • exp(x)                   # calculate e raised to x
  • turn(x)                  # truncates to integer       
  • ceil(x)                   # calculate smallest integer >= x
  • floor(x)                 # calculate largest integer <= x
  • modf(x)                # calculate fractional and integer part of x
  • round(x)               # Round to a specific number of decimal places.
Note:- round() function is used to round to a specific number of decimal places,whereas trunc() , ceil()  and floor() functions always round to zero decimal places.

There are some Trigonometric functions in math module as follows:-
  • degrees(x)            # calculate radians to degree 
  • radians(x)           # calculate degree to radians 
  • sin(x)                 # calculate sine of x radians 
  • cos(x)                # calculate cosine of x radians
  • tan(x)                # calculate tan of x radians
  • sinh(x)               # calculate hyperbolic sine of x 
  • cosh(x)              # calculate hyperbolic cosine of x 
  • tanh(x)              # calculate hyperbolic tan of x 
  • asin(x)               # calculate sine inverse of x, in radians
  • acos(x)               # calculate cos inverse of x, in radians
  • atan(x)               # calculate tan inverse of x, in radians
  • hypot(x,y)           # calculate  sqrt(x*x+y*y)

import math

print("Value of pi =",math.pi)
print("Value of pi =",math.pi)

print("Value of square root =",math.sqrt(25))

print("Value of fabs =",math.fabs(12.3450))

print("Value of factorial =",math.factorial(4))

print("Value of log(x) =",math.log(5))
print("Value of log10(x) =",math.log10(5))

print("Value of exp(x) =",math.exp(2))

a=2.65
b=-3.15

print("Value of trunc =",math.trunc(a))
print("Value of trunc =",math.trunc(b))

print("Value of ceil =",math.ceil(a))
print("Value of ceil =",math.ceil(b))

print("Value of floor =",math.floor(a))
print("Value of trunc =",math.floor(b))

print("Value of round =",round(a))
print("Value of round =",round(b))

print("Value of pi radian in degrees =",math.degrees(math.pi))
print("Value of degrees in radians =",math.radians(180))

print("Value of sin =",math.sin(10))
print("Value of cos =",math.cos(10))
print("Value of tan =",math.tan(10))
print("Value of sinh =",math.sinh(5))
print("Value of cosh =",math.cosh(5))
print("Value of tanh =",math.tanh(10))
print("Value of asin =",math.asin(1))
print("Value of acos =",math.acos(1))
print("Value of atan =",math.atan(1))
print("Value of hypot =",math.hypot(0,5))
Above program output:-
Value of pi = 3.141592653589793
Value of pi = 3.141592653589793
Value of square root = 5.0
Value of fabs = 12.345
Value of factorial = 24
Value of log(x) = 1.6094379124341003
Value of log10(x) = 0.6989700043360189
Value of exp(x) = 7.38905609893065
Value of trunc = 2
Value of trunc = -3
Value of ceil = 3
Value of ceil = -3
Value of floor = 2
Value of trunc = -4
Value of round = 3
Value of round = -3
Value of pi radian in degrees = 180.0
Value of degrees in radians = 3.141592653589793
Value of sin = -0.5440211108893698
Value of cos = -0.8390715290764524
Value of tan = 0.6483608274590866
Value of sinh = 74.20321057778875
Value of cosh = 74.20994852478785
Value of tanh = 0.9999999958776927
Value of asin = 1.5707963267948966
Value of acos = 0.0
Value of atan = 0.7853981633974483
Value of hypot = 5.0

decimal module:- It is used for mathematical operations in python

import decimal
print('decimal value is=',0.2)
a=1.1
b=2.4
c=a+b
print('decimal value is=',c)
Above python code Output:-
decimal value is= 0.2
decimal value is= 3.5

cmath module:-Python cmath module provides access to mathematical functions for complex numbers.

import cmath
c=2+3j
modulus=abs(c)
phase=cmath.phase(c)
polar=cmath.polar(c)
print('complex number modulus is=',modulus)
print('complex number phase is=',phase) #angle between vector line and real axis
print('complex number polar coordinates is=',polar) #the straight line joining the two points at which tangents from a fixed point touch a conic section
print('rectangle co-ordinate value is=',cmath.rect(modulus,phase))
print('value of pi is =',cmath.pi)
print('value of e is =',cmath.e)
print('value of exonant is =',cmath.exp(c))
print('value of log2c is =',cmath.log(c,2))
print('value of log10c is =',cmath.log10(c))
print('value of sqrt is =',cmath.sqrt(c))
print('value of sine is =',cmath.sin(c))
print('value of inverse sine is =',cmath.asin(c))
print('value of sine hyperbolic is =',cmath.sinh(c))
print('value of inverse sine hyperbolic is =',cmath.asinh(c))
Above python code Output:-
complex number modulus is= 3.605551275463989
complex number phase is= 0.982793723247329
complex number polar coordinates is= (3.605551275463989, 0.982793723247329)
rectangle co-ordinate value is= (2+2.9999999999999996j)
value of pi is = 3.141592653589793
value of e is = 2.718281828459045
value of exonant is = (-7.315110094901103+1.0427436562359045j)
value of log2c is = (1.850219859070546+1.417871630745722j)
value of log10c is = (0.5569716761534184+0.42682189085546657j)
value of sqrt is = (1.6741492280355401+0.8959774761298381j)
value of sine is = (9.15449914691143-4.168906959966565j)
value of inverse sine is = (0.5706527843210994+1.9833870299165355j)
value of sine hyperbolic is = (-3.5905645899857794+0.5309210862485197j)
value of inverse sine hyperbolic is = (1.9686379257930964+0.9646585044076028j)

Random number generation functions in random module:-
  • random()                    #generate random number between 0.0 & 1.0.
  • randint(start,stop)     #generate random number in given range.
  • seed (x)                     #map seed value used in random number generation logic.
  • randrange(start,stop,step)                 #it returns a randomly selected element from the given range created by the start,stop and step and step argument
  • choice()               #It returns element randomly from the given list.if list is empty then it so index error.
  • shuffle()              #this function is used to randomly records the elements in the list.
  • print()                        # it can be used for sending  output to screen.

import random
print("Value of round =",random.random())
random.seed(20)

print(random.random())
random.seed(10)
print(random.random())

print("Value of round_integer =",random.randint(1,100))
print("Value of round_integer =",random.randint(30,95))

print("Value of round_range =",random.randrange(1,10,2))
print("Value of round_range =",random.randrange(1,10,3))

print("Value of round_choice =",random.choice('Ramashanker'))
print("Value of round_choice =",random.choice([10,14,50,70,23,26,80,93]))
number=[10,50,40,20,28,47]
print("Value of round_shuffle =",random.shuffle(number))
print("Value of round_shuffle_recorded =",number)
Above python program output:-
Value of round = 0.10603902641997465
0.9056396761745207
0.5714025946899135
Value of round_integer = 55
Value of round_integer = 91
Value of round_range = 9
Value of round_range = 1
Value of round_choice = a
Value of round_choice = 93
Value of round_shuffle = None
Value of round_shuffle_recorded = [28, 47, 10, 50, 40, 20]
Note:- we have to import that respective modules first in your program,to use respective functions present in that modules.

Watch complete Video:-

0 comments:

Post a Comment

Powered by Blogger.