Modules in Python
What is modules ?
Modules are the files that are saved in a python
library .
Module is a collection of functions ,class object
,python statements ,variables and constant.
And modules are sab=ved by .py extention.
So first we will talk about Libraries in Python in which all the modules are saved .
Library--àPackages----à Modules
And In the modules the functions are defined .
Function is a colloection of statements which is made to perform a
specific task.
An these functions are saved
modules and their names are also assign.
Library is collection of Modules
.
Packages is collection of Modules
.
Modules is collection of
functions.
v Types of The Packages in Python:
1.numpy -numpy is package of the
python it is used for numerical python,mathematical,Datascience and machine
learning.
2.scipy- Science library ,Images.
3.pandas-It is used for Data
Analysis.
4.pygame-It is made to make or to
create a vedio game .
5.matplotlib-It is used for
plotting.
v Types of standard Library In Python:
1.Binary-bin() function -This function is used t convert given
number into binay form.
N=10 print(bin(N)) Output: ob1010 |
IN this ob represent binary.
N=10
(10)10=
10/2=5 --à0
5/2=2------à1
2/2=1-------à0
It will start from the last of
the answet of the operation i.e 1 ,0,1,0
I think you have understood that how
bin() function work.
2.octal-oct() function is used to convert given input into octal
numbers .
A=8 B=ox9 C=ob101 print(oct(A),oct(B),oct(C)) Output: Oo10 ,
Oo11 ,Oo5 |
3.hexadecimal-hex() function -this function is used to convert into
hexadecimal number.
A=10 B=Oo11 C=Ob1010 print(hex(A),hex(B),hex(C)) Output: Oxa Oxb Oxa |
4.Integer-int()
function used to convert
given number into integer number.
A=Oo11 B=Oxa C=Ob101 print(int(A),int(B),int(C)) |
5.str() function used to convert given argument into string type.
6.round() function -This function is used to convert
fraction number into its just next number.
Testcases1:
56.9
Output:
57
Testcases2:
43.4
Output:
44
print(round(47,2)) |
vStandard Library Modules In Python-
1.math module-in module there are many functions are
defined in that module.
Functions that are defined in
math module i.e
· sqrt()
import math print(math.sqrt(4)) Output: 2 |
· factorial()
Testcases1:
4
Output:
24
Testcases2:
5
Output:
120
import math print(math.factorial(5)) print(math.factorial(3)) Output: 120 6 |
· pow()
import math print(math.pow(3,2)) Output: 9 |
2.Random module-In random module also there are many
functions defined.
1.random()
Import random Print(random.random()) Output: It will give any number |
2.randint()
import random print(random.randint(1,10)) Output: Output will be any number
between 1 to 10. |
3.randrange()
import random print(random.randrange(1,10)) Output: Its output will be 1 to 9 |
This is basic difference between
randint() and randrange().
4.choice()
import random print(random.choice([1,2,3]) Output: 1 or 2 or 3 |
3.Webbrowser-This is not function it is just used to connect your program with your input
site .
Then using open function() it will go you in that site.
import webbrowser print(webbrowser.open(“site
link “) Output: True and open in that site |
|
4.url library-urllib module
Import urllib.request P=urllib.request.urlopen(“site
link”) Print(P) |
Creating Python Library
/Packages:
M1.py |
#This our first program def S1(): print("Module 1 and function1") def S2(): print("Module 2 and function 2") |
Here we have made our module and
its names as M1.py extention and we have defined two functions are S1() and
S2().
Now if you want to call your
functions in next file then ,
See below
|
Practise Questions
Q.1
import random
P=random.randint(1,3)+2
for i in range(P,8):
print(i,end="
")
Find out maximum and minimu value
of P?
Ans.Minimum value is 3
Maximum 5
Prdict output?
Ans.3 4 5 6 7
Q.2
import random
P=random.randint(4.6)-3
for i in range(P):
print(i,end="
")
Find out maximum and minimu value
of P?
Ans.Minimum value is 1
Maximum 3
No comments: