Modules Packages & File Handling Exception Handling Logging Module Debugging OOPs Principles Multi-Threading Numpy Pandas Arrays Modules: ======== ==> collection/group of: functions, variables, classes saved into a file ==> python file ==> .py file # this is the .py file # module a = 1221 # global variable def addition(p,q): print("The Sum of two numbers = ",p+q) def product(p,q): print("The Product = ",p*q) ==> In the above module: we have: one variable two functions ==> each element/definition within the module ==> a member ==> to use any of the members of the module in your program, you should import module. Syntax: import module-name ==> To access the member from the imported module: Syntax: module-name.member-name import module1 print("The Global Variable from module1 Module is = ",module1.a) print("Calling Addition method from module1:") module1.addition(100,121) print("Calling Product Method from module1:") module1.product(123,11) ================================================= Module Aliasing: ================ ==> Rename the module at the time of importing into another python file is called as "Module Aliasing". Syntax: import module-name as Alias-name/New-Name import module1 as XApp print("Variable from outer module is = ",XApp.a) XApp.addition(121,1221) XApp.product(11,123) ======================================================= How to import selected member from the module into python file? =============================================================== Syntax: from module-name import member-name from module1 import addition,product addition(1234,4321) # print(module1.a) # module1.product(12,13) product(123,321) ==================================== from module1 import * print(a) addition(10,20) product(12,13) ================================== Do we import two modules into same file? ========================================= from module1 import addition,product import module2 as m2 addition(1234,4321) # print(module1.a) # module1.product(12,13) product(123,321) m2.product(1234,23) ============================================= Do we alias members of the modules? ==================================== Yes. from module1 import addition as add,product as pro # addition(123,4321) add(1234,4321) pro(123,87) ================================== dir() ==== ==> to find the members of a module, dir() can be used. # this is the .py file # module a = 1221 # global variable def addition(p,q): print("The Sum of two numbers = ",p+q) def product(p,q): print("The Product = ",p*q) print(dir()) ============================================== __name__: ========= ==> a special variable in python used to check whether the python file is executing as normal file or as module. ==> if __name__ == '__main__' ==> python file is executing as normal file. ==> if __name__ == module-name ==> python file is executing as module def fun1(): if __name__ == '__main__': print("This file is executing as individual python file.") else: print("This file is executing as module.") fun1() =========================== import practice5 practice5.fun1() ============================================= Math Module: ============ from math import * print(sqrt(79)) print(ceil(1.2233)) print(ceil(1.9579)) print(floor(1.2233)) print(floor(1.99999999)) print(ceil(123)) print(floor(123)) print(sin(30)) import math help(math)