Mathematical Functions:
Ex1:
>>>import math #importing math module to call its member functions ex: sqrt(),pow(),...etc
>>> a=math.sqrt(16)
>>> print(a)
4.0
>>> b=math.pow(2,3)
>>> print(b)
8.0
>>>
print(math.pow(2,4))
16.0
Ex2:
>>> a=sqrt(16) #Error
bcoz we did not import the related module 'math'
>>> import math
>>> a=sqrt(16) #Error
we should call this function w.r.t module name
Ex3: To use only single function from the math module
>>> from math import
sqrt
>>> a=sqrt(49)
>>> print(a)
7.0
>>>
To calculate square root value
of a number, we need not develop any logic, we can simply use the sqrt() function
that is already given in the ‘math’ module.
For example to calculate the square root of 16 we can call the function as sqrt(16).
Math is a module that contains
several predefined functions to perform mathematical operations.
If we want to use any module
in our program,
Steps:
1.
We should ‘import’
the math module
Ex: import math
2.
We can use any of
the function which is available in math module.
Ex: a=math.sqrt(16)
Note1: We can refer to sqrt() from math module by writing
module name before the function.
Or
import math as k
>>> a=k.sqrt(16)
>>> print(a)
4.0
Note 2:import math statement is useful to import all
the functions from math
module.
Note 3: If we want to import only one or two functions from
math module, we can write as like below:
from math import sqrt #this will make
only sqrt() available to our program
Example:
From math import sqrt
a=sqrt(16) #ok
b=factorial(5)#error as this
program only available sqrt()
Note: 4: Similarly to import more than one function, one can
write like below:
from math
import factorial,sqrt
a=sqrt(16)
b=factorial(5)
print(a,b)
output:
4.0
120
Related video: https://www.youtube.com/watch?v=-AJntqLTi94&feature=youtu.be
Next Topic: Assessment 7
No comments:
Post a Comment