Modules: ======== group of functions, variables and classes into a file ==> a module. ==> .py file ==> a module { functions, variables, classes objects } Module: ======= x = 1234 # variable def add(p,q): # function-1 s = p + q return s def product(x,y): # function-2 m = x * y return m class employee: # class def __init__(self,name,role,age,salary): self.name = name self.role = role self.age = age self.salary = salary def employeeData(self): print("The Details of the Employee:") print("Name = ",self.name) print("Disgnation = ",self.role) print("Age = ",self.age) print("Salary = ",self.salary) ========================== practice file: ============== import mod11 # Accessing the variable of mod11 # syntax: module-name.variable-name print(mod11.x) # checking of even or odd if mod11.x % 2 == 0: print(mod11.x,"is an even number.") else: print(mod11.x,"is an odd number.") # accessing of the functions # syntax: module-name.function-name(val1, val2, val3,..) result1 = mod11.add(1234,3456) result2 = mod11.product(26,27) print("The Sum = ",result1) print("The Product = ",result2) # Accessing of the class # creating the object obj1 = mod11.employee("Sahithi","Software Engineer", 22, 600000) obj1.employeeData() ============================================ Importing the module with different name: ========================================= import mod11 as calculation # accessing variable print(calculation.x) # access the functions print(calculation.product(10,20)) # create the object for the class of mod11 using alias name obj = calculation.employee("Kumar", "Sr. SOftware Engineer", 28, 1500000) obj.employeeData() =========================================== Importing of Selected member from the module ============================================ Syntax: from module-name import member-name, member-name1,... to access the members: Syntax: member-name # importing the selected member from the class from mod11 import employee,add # syntax: member-name # object-name = class-name() emp = employee("Rahul","Software Developer", 29, 1600000) emp.employeeData() print("The Sum = ",add(100,200)) ================================ Member Aliasing: ================ Syntax: from module-name import member-name1 as new-name1, member-name2 as new-name2,... from mod11 import x as var, add as Addition, product as Multiplier, employee as Employee print(var) print("The Sum = ",Addition(123,345)) print("The Product = ",Multiplier(12,23)) emp = Employee("Ashwini","Sr. Software Developer", 28,1600000) emp.employeeData() ============================================== dir(): ===== is an inbuilt method which we can use to get all the member names from the specified module. Syntax: dir() ========================================== Math Module: ============ from math import * print(sqrt(97)) print(ceil(1.234)) # for the given float, the next highest integer value can be returned print(floor(1.234)) # for the float, the before integer value (less than the given) print(pow(9,7)) print(fabs(12)) print(fabs(-12)) print(log(10)) print(log2(10)) print(sin(23)) =========================================== Random Module: ============== from random import * # random() ==> generate a random number from 0 to 1 print(random()) # randint() ==> generate a random integer from the specified range print(randint(10,100)) # uniform() ==> to generate a float value from the specified range print(uniform(1,10)) # randrange() ==> can generate an integer randomly from the specified range with difference/step print(randrange(1,100)) print(randrange(1,100,10)) # 1, 11, 21,31,41,51,61,71,81,91 # choice() ==> can define with collections # to generate the element from the collection randomly. list1 = ["Apple", "Banana", "Papaya", "Mango", "Kiwi", "Jack-Fruit", "pineapple"] print(choice(list1))