|  | 
|  | 1 | +#https://newdigitals.org/2024/01/23/basic-python-programming/#modules | 
|  | 2 | +#Modules | 
|  | 3 | +''' | 
|  | 4 | +Python Module is a file that contains built-in functions, classes and variables. | 
|  | 5 | +Consider a module to be the same as a code library. Let’s create a simple calc.py in which we define two functions, one add and another subtract. | 
|  | 6 | +
 | 
|  | 7 | +''' | 
|  | 8 | +# A simple module, calc.py | 
|  | 9 | +def add(x, y): | 
|  | 10 | + return (x+y) | 
|  | 11 | +  | 
|  | 12 | +def subtract(x, y): | 
|  | 13 | + return (x-y) | 
|  | 14 | +''' | 
|  | 15 | +Save this code in a file named calc.py within the working directory. | 
|  | 16 | +Now, we are importing the calc that we created earlier to perform add operation. | 
|  | 17 | +''' | 
|  | 18 | +# importing module calc.py | 
|  | 19 | +import calc | 
|  | 20 | +  | 
|  | 21 | +print(calc.add(10, 2)) | 
|  | 22 | +  | 
|  | 23 | +Output: | 
|  | 24 | +12 | 
|  | 25 | + | 
|  | 26 | +#The * symbol used with the import statement is used to import all the names from a module to a current namespace. | 
|  | 27 | +# importing sqrt() and factorial from the | 
|  | 28 | +# module math | 
|  | 29 | +from math import * | 
|  | 30 | +  | 
|  | 31 | +# if we simply do "import math", then | 
|  | 32 | +# math.sqrt(16) and math.factorial() | 
|  | 33 | +# are required. | 
|  | 34 | +print(sqrt(16)) | 
|  | 35 | +print(factorial(6)) | 
|  | 36 | +Output: | 
|  | 37 | +4.0 | 
|  | 38 | +720 | 
|  | 39 | + | 
|  | 40 | +#Directories List for Modules: sys.path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search for the required module. | 
|  | 41 | + | 
|  | 42 | +# importing sys module | 
|  | 43 | +import sys | 
|  | 44 | +  | 
|  | 45 | +# importing sys.path | 
|  | 46 | +print(sys.path) | 
|  | 47 | + | 
|  | 48 | +#We can rename the module while importing it using the keyword. | 
|  | 49 | +# importing sqrt() and factorial from the | 
|  | 50 | +# module math | 
|  | 51 | +import math as mt | 
|  | 52 | +  | 
|  | 53 | +# if we simply do "import math", then | 
|  | 54 | +# math.sqrt(16) and math.factorial() | 
|  | 55 | +# are required. | 
|  | 56 | +print(mt.sqrt(16)) | 
|  | 57 | +print(mt.factorial(6)) | 
|  | 58 | +Output: | 
|  | 59 | +4.0 | 
|  | 60 | +720 | 
|  | 61 | +#We can import specific names from a module without importing the module as a whole | 
|  | 62 | +# import only pi from math module | 
|  | 63 | +from math import pi | 
|  | 64 | +  | 
|  | 65 | +print(pi) | 
|  | 66 | +  | 
|  | 67 | +Output: | 
|  | 68 | +3.141592653589793 | 
|  | 69 | + | 
|  | 70 | +#The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc.): | 
|  | 71 | +#Let’s the dictionary person1 in calc.py | 
|  | 72 | +person1 = { | 
|  | 73 | + "name": "John", | 
|  | 74 | + "age": 36, | 
|  | 75 | + "country": "Norway" | 
|  | 76 | +} | 
|  | 77 | +#Running the code snippet | 
|  | 78 | +import calc as mymod | 
|  | 79 | +  | 
|  | 80 | +a = mymod.person1["age"] | 
|  | 81 | +print(a) | 
|  | 82 | +  | 
|  | 83 | +Output: | 
|  | 84 | +36 | 
|  | 85 | + | 
|  | 86 | + | 
0 commit comments