PREV NEXT
1. Module is nothing but categorised larger parts as a smaller parts.
2 .In Python Module is nothing but Python File,Where Functions,Variables,objects and classes are defined
3. By using this we can use the existing things instead of writing new.
Advantages:
1. By using the module concept we can reuse the modules directly in new applications
2. Same type of attributes we can place in one module
a. predefined modules
Python is having huge set of predefined libraries,few are the examples from there datetime,random,math,os,time,mailbox etc
i. Methods in math module
ii. Constants in Math Module
iii. Method in random module
- Random Module is used to generate random numbers between interval,this random module having multiple methods to generate.
- Look in to below are the methods to generate the random numbers
1. Randrange() Function:
This methods we can use in different ways .They are
i. Single argument Randrange() function
This randrange will take the input as single argument.This is used to produces a random integer values less than they specified value as an argument
Syntax:
random.randrange(stop)
stop:
It is a bounadry value to generate random number
Note:
If the stop will not zero and negative numbers as an argument,if in case proveide it will gives the valueError
i. Three argument Randrange() function
This randrange will take the input as three arguments.This is used to produces a random integer values less than they specified value as an argument(start,stop) vales.
Syntax:
random.randrange(start,stop,step)
start:
It is the start value of the range function and may will include in selection
stop:
It is a bounadry value to generate random number,but will not include in selection
step:
This is the value which we want increment ,But if step not specified any value it will take as zero
Note:
If the stop will not zero and negative numbers as an argument,if in case proveide it will gives the valueError
b. User defined Modules
In python we can create user defined set of modules also,here we can use python file n ame as a module suppose we order.py,bill.py,collection.py this file names we can use as a modules like order,bill,collection
Example:
Consider the below example ,save the script with the name of “calculator.py”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def sume(x,y): z=x+y return z def subs(x,y): z=x-y return z def mult(x,y): z=x*y return z def divi(x,y): z=x/y return z |
How Import Modules:
There are multiple ways to import the modules into the python code.They are
1. By using import keyword:
import keyword is used to import a module.
Syntax:
import module1,module2,module3…..modulen
a. Import predefined modules and using
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import random import math import datetime x=7.5 y=5 print("****Date time Library using***********"); print("Today is", datetime.datetime.now()) print("****Math time Library using***********"); print (math.ceil(x)) print (math.floor(x)) print (math.sqrt(y)) print (math.exp(3.0)) print (math.log(2.0) ) print (math.pow(2.0,3.0)) print (math.sin(0)) print (math.cos(0)) print (math.tan(45)) print("****Random time Library using***********"); print(random.randrange(4,11)) print (random.randint(0, 9)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
****Date time Library using*********** Today is 2018-10-23 17:46:24.786691 ****Math time Library using*********** 8 7 2.23606797749979 20.085536923187668 0.6931471805599453 8.0 0.0 1.0 1.6197751905438615 ****Random time Library using*********** 10 3 |
b. Import user defined module
Please consider the above “calculator.py” example
Example:
1 2 3 4 5 6 7 8 9 |
import calculator print ("addition is:",calculator.sume(6,3)) print ("substraction is:",calculator.subs(6,3)) print ("multiplication is:",calculator.mult(6,3)) print ("division is:",calculator.divi(6,3)) |
Output:
1 2 3 4 5 6 7 |
addition is: 9 substraction is: 3 multiplication is: 18 division is: 2.0 |
2. By using from and import keyword:
by using from and import keyword we can import specific attribute from the module.
Syntax:
from module_name import attribute1,attribute2………attributen
a. Predefined modules:
Example:
from random import randrange,randint
from math import ceil,floor,pow,sqrt,exp,sin,cos,tan,log
from datetime import datetime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
x=7.5 y=5 print("****Date time Library using***********"); print("Today is", datetime.now()) print("****Math time Library using***********"); print (ceil(x)) print (floor(x)) print (sqrt(y)) print (exp(3.0)) print (log(2.0) ) print (pow(2.0,3.0)) print (sin(0)) print (cos(0)) print (tan(45)) print("****Random time Library using***********"); print(randrange(4,11)) print (randint(0, 9)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
****Date time Library using*********** Today is 2018-10-23 18:09:34.020638 ****Math time Library using*********** 8 7 2.23606797749979 20.085536923187668 0.6931471805599453 8.0 0.0 1.0 1.6197751905438615 ****Random time Library using*********** 6 1 |
b. User defined modules:
Example:
from calculator import sume,subs,mult,divi
1 2 3 4 5 6 7 |
print ("addition is:",sume(6,3)) print ("substraction is:",subs(6,3)) print ("multiplication is:",mult(6,3)) print ("division is:",divi(6,3)) |
Output:
1 2 3 4 5 6 7 |
addition is: 9 substraction is: 3 multiplication is: 18 division is: 2.0 |
3. By using import complete module:
By using this method we can import all attributes in that module
Syntax:
from module_name import *
a. Predefined modules:
Example:
from random import *
from math import *
from datetime import *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
x=7.5 y=5 print("****Date time Library using***********"); print("Today is", datetime.now()) print("****Math time Library using***********"); print (ceil(x)) print (floor(x)) print (sqrt(y)) print (exp(3.0)) print (log(2.0) ) print (pow(2.0,3.0)) print (sin(0)) print (cos(0)) print (tan(45)) print("****Random time Library using***********"); print(randrange(4,11)) print (randint(0, 9)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
****Date time Library using*********** Today is 2018-10-23 18:31:48.196217 ****Math time Library using*********** 8 7 2.23606797749979 20.085536923187668 0.6931471805599453 8.0 0.0 1.0 1.6197751905438615 ****Random time Library using*********** 9 7 |
b. User defined modules:
Example:
from calculator import *
1 2 3 4 5 6 7 |
print ("addition is:",sume(6,3)) print ("substraction is:",subs(6,3)) print ("multiplication is:",mult(6,3)) print ("division is:",divi(6,3)) |
Output:
1 2 3 4 5 6 7 |
addition is: 9 substraction is: 3 multiplication is: 18 division is: 2.0 |