Modules
A group of functions, variables and classes saved to a file, which is nothing but module.
Every Python file (.py) acts as a module.
Ex: dharshimath.py
x=888
def add(a,b):
print(“The Sum:”,a+b)
def product(a,b):
print(“The Product:”,a*b)
dharshimath module contains one variable and 2 functions.
If we want to use members of module in our program then we should import that module. import
modulename
We can access members by using module name.
- modulename.variable
- modulename.function()
test.py:
import dharshimath
print(dharshimath.x)
dharshimath.add(10,20)
dharshimath.product(10,20)
Output
888
The Sum: 30
The Product: 200
Note:
Whenever we are using a module in our program, for that module compiled file will be generated and stored in the hard disk permanently.
Renaming a module at the time of import (module aliasing):
Ex:
import dharshimath as m
here dharshimath is original module name and m is alias name.
We can access members by using alias name m
test.py:
import dharshimath as m
print(m.x)
m.add(10,20)
m.product(10,20)
from … import:
- We can import particular members of module by using from … import
- The main advantage of this is we can access members directly without using module name.
Ex:
from dharshimath import x,add
print(x)
add(10,20)
product(10,20)==> NameError: name ‘product’ is not defined
We can import all members of a module as follows fromdharshimath import *
test.py:
from dharshimath import *
print(x)
add(10,20)
product(10,20)
Various possibilities of import: import modulename import module1,module2,module3 import module1 as m import module1 as m1,module2 as m2,module3 from module import member from module import member1,member2,memebr3 from module import memeber1 as x from module import *
member aliasing:
from dharshimath import x as y,add as sum
print(y)
sum(10,20)
Once we defined as alias name,we should use alias name only and we should not use original name
Ex:
from dharshimath import x as y print(x)==>NameError: name ‘x’ is not defined
Reloading a Module:
By default module will be loaded only once even though we are importing multiple times.
Demo Program for module reloading:
import time
from imp import reload
import module1
time.sleep(30)
reload(module1)
time.sleep(30)
reload(module1)
print(“This is test file”)
Finding members of module by using dir() function:
Python provides inbuilt function dir() to list out all members of current module or a specified module.
dir() ===>To list out all members of current module dir(moduleName)==>To list out all members of specified module
Ex : test.py
x=10
y=20
def f1():
print(“Hello”)
print(dir()) # To print all members of current module
Output
[‘__annotations__’, ‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__nam e__’, ‘__package__’, ‘__spec__’, ‘f1’, ‘x’, ‘y’]
Working with math module:
- Python provides inbuilt module math.
- This module defines several functions which can be used for mathematical operations. The main important functions are
- sqrt(x)
- ceil(x)
- floor(x)
- fabs(x)
- log(x)
- sin(x)
- tan(x)
Ex:
from math import *
print(sqrt(4))
print(ceil(10.1))
print(floor(10.1))
print(fabs(-10.6))
print(fabs(10.6))
Output
2.0
11
10
10.6
10.6
Note:
We can find help for any module by using help() function
Ex:
import math
help(math)
Working with random module:
- This module defines several functions to generate random numbers.
- We can use these functions while developing games,in cryptography and to generate random numbers on fly for authentication.
random() function:
This function always generate some float value between 0 and 1 ( not inclusive) 0<x<1
Ex:
from random import *
for i in range(10):
print(random())
Output
0.4572685609302056
0.6584325233197768
0.15444034016553587
0.18351427005232201
0.1330257265904884
0.9291139798071045
0.6586741197891783
0.8901649834019002
0.25540891083913053
0.7290504335962871
randint() function:
To generate random integer between two given numbers(inclusive)
Ex:
from random import *
for i in range(10):
print(randint(1,100)) # generate random int value between 1 and 100(inclusive)
Output
51
44
39
70
49
74
52
10
40
8
uniform():
It returns random float values between 2 given numbers(not inclusive)
Ex:
from random import *
for i in range(10):
print(uniform(1,10))
Output
9.787695398230332
6.81102218793548
8.068672144377329
8.567976357239834
6.363511674803802
2.176137584071641
4.822867939432386
6.0801725149678445
7.508457735544763
1.9982221862917555
random() ===>in between 0 and 1 (not inclusive) randint(x,y) ==>in between x and y ( inclusive) uniform(x,y) ==> in between x and y ( not inclusive)
randrange([start],stop,[step])
returns a random number from range
start<= x < stop
start argument is optional and default value is 0 step argument is optional and default value is 1
randrange(10)-->generates a number from 0 to 9
randrange(1,11)-->generates a number from 1 to 10
randrange(1,11,2)-->generates a number from 1,3,5,7,9
Ex 1:
from random import *
for i in range(10):
print(randrange(10))
Output
9
4
0
2
9
4
8
9
5
9
Ex 2:
from random import *
for i in range(10):
print(randrange(1,11))
Output
2
2
8
10
3
5
9
1
6
3
Ex 3:
from random import *
for i in range(10):
print(randrange(1,11,2))
Output
1
3
9
5
7
1
1
1
7
3
choice() function:
- It won’t return random number.
- It will return a random object from the given list or tuple.
Ex:
from random import *
list=[“Sunny”,”Bunny”,”Chinny”,”Vinny”,”pinny”]
for i in range(10):
print(choice(list))
Output
Bunny
pinny
Bunny
Sunny
Bunny
pinny
pinny
Vinny
Bunny
Sunny