‫Up to this point, we've learned about a few ways to organize our Python code.
‫The first thing we did well, we just wrote Python in a file and that organizes it, right?
‫Our entire code has been so far on one single file.
‫So that was one way that we organized our code.
‫Next we learned about functions, and functions were used as a way for us to not write code that we
‫repeat over and over.
‫We could write functions that we can use anywhere we want in our file so that we can call these functions
‫over and over and we can name the functions appropriately so that our code makes sense, packages it
‫nicely for us to use.
‫All of these things were meant for us to keep our code clean, make our code more maintainable, thus
‫keeping our code organized.
‫Because as we write more lines of code, it gets harder and harder to keep everything in your head.
‫If other people join your team to work on your project, they need an easy way to figure out how things
‫are working.
‫But we still have a problem, and that problem is that everything is contained in this one file.
‫All the functions we've been writing has been on just one dot pi file.
‫But in real life, this isn't the case.
‫We have a lot more code and we can't just have one file with millions line of code.
‫So how do we stay organized?
‫Well.
‫If we had multiple files of Python.
‫Like this because our project is getting bigger and bigger.
‫Would this work?
‫Is there a way for us to link all of these files together?
‫As a matter of fact, there is and this is very common practice all across industry, and we call this
‫way of organizing code modules.
‫And modules are simply, well, these files.
‫Each one of these files, each dot pi file is a module.
‫And by building these modules, kind of like we built different functions, kind of like we built different
‫classes because inside of these files we can have classes, we can have functions, but we can also
‫have different files.
‫So a layer higher to divide up our code.
‫And in this section we're going to learn all about modules and try to master it and see how we can write
‫organized code.
‫Let's have a look.
‫You can see over here, if I click on files in our ripple, we have a main pi and every time I click
‫Run, this file got run.
‫But what if we have more files?
‫What if I add another file here and we'll call it Utility Dot Pi?
‫So based on what I just taught you, these two things are what?
‫They're modules, right?
‫Main Pi is a module utility.
‫PI is a module.
‫And when writing modules we use the same principles as variables in Python, which is snake case.
‫That is, if there's spaces we use underscore, everything is lowercase letter.
‫So how do we divide up these modules?
‫And this comes with practice.
‫When you're just beginning, it's hard to say, hey, what goes into what file, what goes into what
‫file?
‫But it's similar to classes, right?
‫Similar to functions.
‫We want to divide up our code into chunks that make sense.
‫Again, let's say we're working at Netflix.
‫We might have a file dedicated to videos, we might have another file or module dedicated to the login
‫of a user, maybe another file dedicated to analytics and figuring out recommendations for users.
‫We want to group these classes and functions together inside of a file that makes sense.
‫Now, one of the most common ways to use a module is something like a utility.
‫Let's say we want to create a utility module that is a module that has a lot of well, kind of like
‫a tool belt, very simple functions that we can use all across our project because we can imagine here
‫having multiple files.
‫So let's create a very simple example.
‫I'm going to say a multiply function exists here.
‫And this takes a numb one and numb to.
‫And in here we're simply going to return num one times num to a very simple function.
‫And once again, we'll also have a divide function.
‫And yes, this is kind of useless because we can just use the plus or the divide operand in Python.
‫But stick with me here.
‫I'm trying to show you an example.
‫All right.
‫So number two, I'm going to return num one divided by num two.
‫Nice and simple.
‫So this.
‫Is our utility module that has all these functions for us.
‫Now notice here that we're actually not doing anything in this file.
‫I'm simply defining functions, but I'm not running any of the functions.
‫But let's say we have this utility module and I want to use it in my main py file.
‫How would we go about doing this?
‫Well, the way we communicate between these two files is quite simple.
‫All we need to do is use the import command we import and then give it the file name that we want to
‫import.
‫What do we want to import?
‫We want to import utility just like that.
‫And notice that I'm not adding the dot py because we assume that whatever we're going to import is going
‫to be a python file.
‫So we don't need to do that.
‫Just the name.
‫Just like that.
‫Now, let's print utility here.
‫If I run this.
‫Whoa.
‫Did you see that?
‫We have module utility from and then this is a file path that this website generates for us, so it
‫doesn't really matter.
‫Now, there's a few things that happened here.
‫One is that we generated this py cash folder.
‫Underscore.
‫PY cash.
‫And.
‫Oh, boy, what is this gibberish?
‫Hmm.
‫This is this is pretty confusing, right?
‫Well, this pie cache is created every time we run a file with, let's say, import statements.
‫So when we're using modules.
‫You see, what Pi Cash does is when we click Run.
‫The Interpreter is going to create this podcast folder and it's going to say, Hey, I'm running this
‫file, this one main pi file, anything that may not py file imports, let's say utility.
‫In our case I'm going to cash it.
‫So see how here it says dot pi.
‫See, this is because it's using the C Python interpreter.
‫Remember, this is the C python, which is the interpreter written and C, so this is actually a compiled
‫file.
‫So that next time I click run here on my main pi, nothing changes because what's going to happen is
‫instead of loading up utility pi, it's going to load up this compiled version of utility because nothing
‫has changed.
‫And utility pi.
‫And this makes things faster.
‫When I tried to run Main Pi again, that's what caching is.
‫Caching is, hey, I'm going to remember this and this is the compile version so I don't have to go
‫through the compilation step again.
‫But notice that the main pi file does not get compiled because, well, we run it every time.
‫Now, if we change the utility function to not have the divide in there, I click run and you don't
‫notice it here.
‫But this has now been rerun because we have a different file.
‫You see that we have multiply here but not divide.
‫So let's bring back divide again.
‫Go back to here, click run.
‫And there you go.
‫We have divide here, too, now.
‫But this is something that we don't touch.
‫This just comes underneath the hood with any editor that we use.
‫And in a few videos, we're going to show you how this also happens when we use something like pie chart.
‫This is just something that editors allow us to do just so our programs can run faster.
‫So I now have access.
‫Let's make this a little bit smaller.
‫I now have access to utility.
‫And here's the interesting part.
‫If I do dot here, look at that.
‫I have access to divide and multiply so that I can multiply numbers.
‫Let's say two and three click run.
‫And I'm able to use the utility module in my main file.
‫Very, very cool.
‫If I do divide and I click Run.
‫I can use this as well.
‫Very, very cool.
‫And I can import as many times as I want if I had a different file in here and I want to import another
‫thing.
‫Let's say there is a utility to file.
‫Well, I can import that and import as many things as I want and use them across my files.
‫How cool is that?
‫So now we have a way to organize our code and have all these files communicate together.
‫Because in real life, we have big projects and these big projects are going to be worked on by different
‫teams, different programmers.
‫And we need a way to work with all these files.
‫And by using the import statements between modules, we're able to use functionality, just like we
‫were able to use the functionality that functions provided us between different files.
‫And most of the time, if you're working at a company, the first couple of lines on a dot pi file are
‫usually import statements because usually you're communicating with other files.
‫Let's learn some more in the next video.