Modules ======== => Module is a python file which is consisting of functions, variables, classes etc. Ex: test.py x = 102 def addition(a, b): print("The Sum = ",(a+b)) class MyClass: def __init__(self, p, q): self.p = p self.q = q Note: ----- When we want to use any member of module in your program then you should import that module. Syntax: import module-name Ex: to use variable of module import module-name module-name.variable-name to use function from module module-name.function-name() Ex: import test print(test.x) test.addition(10,20) mc = test.MyClass(121, 111) Note: ----- While importing any module into your python program, the PVM can generate compiled file for that module and can store into the hard disk permanently. Module Aliasing: ---------------- => Renaming of the module while importing the module into your python program, we allow to provide the another name to that module. Ex: import test as t print(t.x) t.addition(20,30) Note: ----- => If you want to import the particular members of the module then we can use "from and import" keywords. Here, that particular member can access without any module-name reference. Ex: from test import addition addition(121, 1010) Ex: --- from test import x, addition print(x) addition(10101,12121) Note: ----- At a time we can allow to import more than one module Syntax: import module1, module2, module3,... Note: ----- We can alias all the modules while importing into your python program Syntax: import module1 as m1, module2 as m2, module3 as m3,... Module member aliasing: ----------------------- from module-name import member1 as m1, member2 as m2,.... Ex: from test import addition as add, MyClass as MC Reloading of module: -------------------- When we can import a module again and again but the PVM can load that module for only one time. But we can reload that module. import time from imp import reload import module1 time.sleep(30) reload(module1) dir(): ------ used to find the members of module. Ex: test.py x = 10 y = 20 def f1(): print("Hello") print(dir()) Math Module ============ from math import * print(sqrt(99)) print(pow(23,27)) print(ceil(10.1)) print(floor(10.1)) print(log(10)) print(sin(23)) help(): ------- to know about any module we can use "help()" import math print(help()) Random Module ============= from random import * for i in range(5): print(random()) # generate floating point values as random numbers # randint() ==> generate random integers from the specified range for i in range(5): print(randint(100,500)) # uniform() ==> generate random floating point values from the specified range for i in range(5): print(uniform(1,10)) # randrange() ==> can generate an integer as random value print(randrange(10)) # from 0 to 9 print(randrange(1,10,2)) # choice() listData = ["Banana", "Apple", "Kiwi", "PineApple"] print(choice(listData)) Datetime module ================ import datetime x = datetime.datetime.now() print(x) print(x.year) y = datetime.datetime(1993, 10, 5) print(y) print(x.strftime("%a")) print(x.strftime("%A")) print(x.strftime("%w")) print(x.strftime("%d")) print(x.strftime("%b")) print(x.strftime("%B")) print(x.strftime("%m")) print(x.strftime("%y")) print(x.strftime("%Y")) print(x.strftime("%H"))