0:00
Hello everyone, welcome to chapter 11 that is module and packages
0:06
So basically I will be explaining what are this module, how to define them, how to create them
0:11
what are the packages whenever we are doing import statements in Python, for example
0:17
import pandas, import mat, import numpy, or any of the libraries whenever you are importing
0:25
what is happening behind the scenes. So you are just importing something but how these functions and methods are being called
0:32
we'll see in modules and packages. So basically what is the difference between functions versus module versus library
0:40
These are the three terms that I use right. So group of lives with some name is called as functions
0:45
For example, we are just creating the function with DEF keyword. Then there will be function name, there will be parameters and there will be certain code definition
0:55
and return statements. right this is something that we have seen then a group of functions save to a file is called as modules
1:03
for example this is one function this is another function and so on there are n number of
1:08
functions inside one dot p.y file that is nothing but called as module right and then a group of
1:16
modules is nothing but library now all this dot p y file is modding so we have module one module two
1:24
and so on we have library right so now this is a representation what will one what
1:31
we do which is having different functions inside them is called as library
1:35
and function has and number of lengths that we have already seen so now every
1:40
dot p.y file acts as a module let's say we have a file named code.py which has
1:47
this code this is identifier this is function one this is function two and now
1:52
which has one variable and two functions if you want to use members of this module in our program
1:58
we should import that module right so if i am having another file name tisdp y i can write
2:04
import code since that is the name of the file so i can import code once i'm importing code all the
2:10
functions and variable definitions in that file i can access them from this file as well by just
2:15
doing dot operation so it will be code dot x then i can also do code dot
2:22
and I can send a parameter as for the definition on that function it wanted two inputs
2:28
so I am sending an appart in E so we'll see this in VS code so initially we have seen
2:34
Jupiter notebook to code wherein we can just write one cell and then execute but there is
2:40
a Vs code editor these there are at number of editors in Python like you can work with
2:46
PyChamp you can also work with spider which is there in build in
2:52
inside anaconda and but now I will be editing the codes in vS code
2:57
So I'll be showing those in example. Before that what is this name underscore underscore
3:03
A special magic method that we already seen underscore underscore underscore two types from front and back is nothing but a magic method
3:12
This is added internally to a program. This variable score information regarding whether a program is executed as an individual program or as a module So for example whenever this name underscore underscore is written and it is also equal to mean that we will see in the code files If this particular thing is
3:32
there in your code so it will be treated as an individual program or otherwise you can call
3:37
this pile to another file and then it will be treated as module. Now if a program is
3:43
exhibited at individual program then value of this variable is main. So when name equal to equal to
3:49
main it is individual program if program is executed as a module from some other
3:55
program then value of this variable is a name of module where it is defined so
4:00
basically we will do import statements to import this particular module and another
4:05
program like in the by using name variable we can identify whether the program
4:10
executed directly or as a module we'll see this in example as well
4:14
so the theoretical explanation of this now there are different methods that we can call by just calling a library
4:25
So different functions inside useful module. So you can also use help for any module by using help function in case you wanted to know what
4:33
are those methods and function definitions for the library. You can just use ethics and then library and in
4:40
The math module has different functions where seal load, PAMPS, LOW, CETs
4:46
So how to do that? or to use them in your code directly you can use from Math input star
4:52
Whenever you are calling star means everything inside that particular Math module will be
4:56
imported to your file. And now you are doing certain operations like SQLity of 4
5:01
So directly you do not need to define what is this SQLRD. It will understand that from Maths Library is referring to SQLRT method, which is just
5:11
doing the square root of 4. So output will be 2. Similar way, CEL and floor
5:15
All these math functionalities you can use the math functionality. you can use directly by just importing the module
5:20
The similar way we have a random modding so whenever you wanted to do certain random operations
5:26
from random import everything and now I am just randomly generating a number within the range
5:32
of from I in range of 3. That means 0, 1 and 2
5:37
This will be 3 times executed and the output we being random number
5:43
Now RAND identity functions on the random module if you wanted to use specifically
5:47
it will just create integer it was just creating a random number which is not integer
5:52
here it will create an integer with a range of 1 to 100 that means starting from 1 ending to 99
6:02
now what is packages so encapsulation mechanism to group or related no other universe single unit a folder or an directory which represent a collection of
6:14
Python files. Any folder or directory contains INITI files is considered as Python package. An I-90 file can be empty. So whenever you want to define a package, you need to have underscore underscore in it as an empty file which will consider your total files as a package. Okay. So basically it should have one I-N80 file. Then it can also have another folder. For example, module one
6:44
with I file and another different files it can also have another folder with I file and different right
6:55
So all this folder 1, so how you can prepacket as it has one folder with module 1, for example
7:04
another folder with module 2 and underscore underscore init.py file. So all this together you can consider as a package
7:15
and you can import it to some other code file. So this is a way you can define your packages
7:23
Now main advantages of package are we can resolve the naming conflicts because we know all the files, different files inside your different folder
7:31
We can identify our complaints uniquely and it can improve the modality of the application
7:38
So for example, this is just an example. This is one of the file, then one folder with these two files and another
7:44
files and another pile outside the folder and that is empty file
7:50
So now whenever inside this module.p.pile I have just beta1 function and now I can
7:57
import package 1.m.module 1 and from package 1. module 1 I can import this f1 function as
8:05
well. We will see this examples inside VS4. So overall the structure is we have a different
8:14
functions, functions, classes, variables that we can write inside our dot pwapile
8:20
That pY file is nothing but our module. So we can have n number of modules and number of dot pypies together
8:28
Inside a folder we will call it as package. With package we will also have init as empty files and all these packages together we will
8:38
consider as library. So this is a hierarchy on which we can create a different files to
8:44
and work on different applications by using Python programming. So let me move to VS code
8:51
For example, this is a folder structure which I have created just for an explanation purpose
8:56
I have one underscore underscore name folder with these two files. Then I have modules folder with these two files
9:04
I have packages folder with this particular file and pack folder with these two files
9:10
So we'll see one by one examples of them inside the VAS code. inside the VS code. This is a VS code UI. Right now I am inside that particular folder. You can just go to the folder by it in CB change directory operations. Now for example, inside the name I am there inside module dot p.wai. So I created one file. I created a function and then I am just calling this function. But now there is one one file test dot PYN and I am just importing a module, the name of the file that is module
9:43
So, there are two ways to run this particular Python file. One is directly calling this file
9:50
Another is calling this file. So for that I have written underscore n-unascom name equal to mean
9:57
If at all I am calling this file, the code will be executed as a program
10:02
But if at all, I am calling Text.p. Since I am importing this file, the code will be executed at module from some other program
10:09
So this will be an output to it right So now I am going to v and name This is folder Now I am just executing Python module PY
10:22
Since I am executing this file, output will be code is executed as a program
10:27
Now let us see and run this T.Y5, what will be without root
10:32
So now I am calling T.PY, the code is executed as a module from some other program
10:38
So this is the way the module is used to be done some other program. models can be created. Now let us see one more example. I have created one point
10:47
point p.y file with one identifier variable, one function definition and second function
10:52
definition. And inside another file I am just importing that particular code 1.p.y and then
10:58
I am just calling the x one function variable 1, function 2. And I am also printing all the
11:06
the directories, all the functions and metrics related to code 1. Really we will see how this code 1 not PY and test 1
11:17
1.p.i is executed. So I'm just doing CD-i-1 to go back to the repository and doing clear over here
11:25
And now I want it to go to CD modules. Right. So now inside modules we have to 5
11:33
One is having different definitions of the function and another is. having the import of first function
11:39
So we will call one by one by just doing Python test 1.p
11:44
So all the definitions that we have performed inside code 1.p.y are imported in test.t.p.y
11:52
and outputs are like we can see as per our print statements
11:56
So this is another way our packages and modules are used and utilized in our course
12:02
So third one let us see is packages. So I will be going back to Packages Repository
12:13
And inside Packages Repository as we have discussed we have a folder name Pack and inside
12:18
that we have an empty init file and then we have Modul.p
12:23
So this is a definition that we have seen inside our Packages definitions, right
12:29
So this is the same way I have just created a different files with different names and right
12:33
Now I am inside module.py. So here we have just one function definition
12:38
This is a from module present in pack folder. And now from outside, outside of this pack folder you can see there is test dot pwai
12:47
And I am just calling. First I am importing module from pack and from module dot functions I am calling that function
12:54
So basically whenever I am going to execute this test. So hello this is from module present in PAC
13:03
So it will go to this pack folder. It will import this module
13:07
Inside that module there is a fun. That is what we are defined inside the functions and that function will be called and this
13:13
print statement will be printed as output. So this is a way how our packages and what do you work
13:21
So that is from this chapter basically and thank you so much for joining us for this Python
13:26
basics course. I hope this will be helpful to you. In case of any queries, you can always go back and give to this particular channel or write us at our LinkedIn portal
13:37
Thank you so much. See you in the next course