#45 Python Tutorial for Beginners | Modules

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan Reddy and less politically series on Python now at this point we have talked about functions right and then we have done so many stuff till now we know how to add numbers we know how to find factorial but think about this when you work on a project of course you will be solving a real-life problem right and that may be a complex project now when you say a complex project it should be simple for the users right you should not build a complex project for a user but then for the program well any every project is a complex project because it will be writing a lot of code right maybe thousand lines of code now this is where the problem starts you know see we have a point several debugging now when you say debug it simply means removing bugs right what about coding them if debugging is removing bugs then coding is actually adding bugs right so every time you cool you add new bugs now even if you don't do it intentionally it happens you know because we are trying to solve a problem and around that you were adding some code which will increase bugs okay now how do you manage this stuff right because if you increase bugs of course at some point you need to read your own code E to understand how to remove those bugs and your software should be maintainable when I wrote this code or the God and I understood what I did and now only God knows right so it's it happens it up enjoyed up code after some time even you would not understand what you have written because when you have thousand lines of code it's difficult to track the second problem is if you write everything in the same page or your favorite thing at the same place it is difficult to manage it and if you change one point it may affect other cool as well and that's well what you will do is if you want to solve this problem we will be using a concept of modules so stop fighting one big software in one file you will break it down into small small parts right example let's say if you have ABCD as a software you want to build a software in which you have these four features and that's a one project right it's a combined project so what you will do is before even before starting the actual project what you will do is you will break it down the components on papers you will write hey we have ABCD now we have to break it down in logical parts right of course you cannot simply take the software you have to think about it you have to break it down your logical parts right so all the same features should belong same module so you will say okay we have ABCD when a and C can be one module and B and D can be one module okay so that's what we have done and then further you realize okay we can actually also break it down a and C so you have saying a module C module B module and D modules so we are breaking things down the advantage is you will be building four different modules right the first advantage would be if you want to change something in a which it will not affect other modules right because they are separate down it may if you are doing some tight coupling stuff there but normally it doesn't affect so when you write up code if you change something in a it will not affect other modules or it may not affect it other modules well the next thing is you can also reuse your modules example let's say after this project you are building a new project with some similar features example now this time you are building maybe G ADP so we have this other project now here you can actually use a and D module because you have already built it right so that's the advantage of using modules now can we do that in a project in fact will not be building a complex project here let us do it simply but then if we talk about a model what it will have so in Python we have this concept of variables we have functions we have classes right we'll be talking about classes later but we have that concept so in one module you can have all the different stuff right so we can have variables we can have functions and we can have a class or you can imagine at this point you're one module will be one file so we are doing a file here we have demo dot pi this is your one module right we can create another file as a separate module let me show you something so let's say this is my main file right and here I want to add two numbers so what normally we do is we simply say a is equal to nine and you say B is equal to seven and here you will add those two values right maybe you want to perform all the operations right addition subtraction division multiplication so you can do that here itself or I can create functions and now we know how to create functions right so we can do that here but we can create modules right so you can create those four functions here in the same module or you can create a separate module which will have all these functions and that makes more sense right having a separate module so let's do that so what I will do is to create a separate module I will create a new file so I will say new and let's go to python file building this as a calc because this gap will have all the functions which you need so this is a separate module for you and let's define the function so we'll set def and the function name would be add which takes two parameters which is a and B in this case and we simply return a plus B that's our first function likewise we can create for function at me do it quickly so you can see now we have this model which is calc which has four functions right we can see that here now I want to use this function in another module which is demo how can I use that so because if I simply type ad it will not work right you cannot simply call and it of course it will not work that at least so even if I pass a comma B by saying AC equal to add a comma B it will not work because it is in separate module so we have used some in build modules till this point right so if you know what we have to work with import right so we should say import something and then we have also used from or module import specific functions or the classes we have done that right but we have done that for the in build modules how about our own module because calc is my own module now so what I will do is if you want to use calc we do import it and the you do that is by saying import calc the job is done you're saying input calc and if any one you want to use it you will simply say calc dot add so this is one way right can use a module name and then say dot add or if you don't want to say calc every time because of course if you want to add if you want to subtract you will be using Kaltura subtract add multiply multiple times instead of that you can say from calc input whatever you want to input I can say import add so in the one line you can say add in the next one you can say imports are pinpoint multi all you can say import start right it will import all the functions for you and now you don't need to call Cal it will simply say and it will work right it's a beauty right so this is how you break down your project to small small parts and of course it doesn't make sense to have a separate module for separate functions right so all your logical functions should work together in one module not just function classes variables whatever you want right and now once you got this you can print the value of C and it perfectly works let's run it okay so you can see regards the output which is 16 1 & 6 so that's what we wanted right 9 plus 7 is 16 we can also do that for subtraction everything will work here and you can see we got you so that's how use modules you can create multiple what is not just one or two you can create n different files as a different modules and you can import them the way you want right in fact in Python itself we have different modules available so that's it from this video I hope you enjoyed this video because in this video we have talked about modules which is very important for your project so do click on the like button there and do subscribe for further videos thanks for watching everyone
Info
Channel: Telusko
Views: 397,931
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: 1RuMJ53CKds
Channel Id: undefined
Length: 7min 20sec (440 seconds)
Published: Sat Aug 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.