1 00:00:00,760 --> 00:00:06,310 Up to this point we've learned about a few ways to organize our Python code. 2 00:00:06,310 --> 00:00:07,700 The first thing we did. 3 00:00:07,810 --> 00:00:13,160 Well we just wrote Python in a file and that organizes it right. 4 00:00:13,270 --> 00:00:17,950 Our entire code has been so far on one single file. 5 00:00:17,950 --> 00:00:21,520 So that was one way that we organized our code. 6 00:00:21,590 --> 00:00:29,420 Next we learned about functions and functions were used as a way for us to not write code that we repeat 7 00:00:29,510 --> 00:00:30,800 over and over. 8 00:00:30,800 --> 00:00:37,220 We could write functions that we can use anywhere we want in our file so that we can call these functions 9 00:00:37,340 --> 00:00:45,650 over and over and we can name the functions appropriately so that our code makes sense packages it nicely 10 00:00:45,650 --> 00:00:54,880 for us to use all of these things were meant for us to keep our code clean make our code more maintainable 11 00:00:55,090 --> 00:01:03,220 thus keeping our code organized because as you write more lines of code it gets harder and harder to 12 00:01:03,220 --> 00:01:05,230 keep everything in your head. 13 00:01:05,500 --> 00:01:11,500 If other people join your team to work on your project they need an easy way to figure out how things 14 00:01:11,500 --> 00:01:11,970 are working. 15 00:01:12,930 --> 00:01:20,340 But we still have a problem and that problem is that everything is contained in this one file all the 16 00:01:20,340 --> 00:01:27,900 functions that we've been writing has been on just one dot pi file but in real life this isn't the case. 17 00:01:28,110 --> 00:01:34,470 We have a lot more code and we can't just have one file with millions line of code. 18 00:01:34,470 --> 00:01:36,720 So how do we stay organized 19 00:01:39,630 --> 00:01:45,010 if we had multiple files of python like this. 20 00:01:45,190 --> 00:01:47,620 Because our project is getting bigger and bigger. 21 00:01:47,650 --> 00:01:48,610 Would this work. 22 00:01:48,610 --> 00:01:52,660 Is there a way for us to link all of these files together. 23 00:01:54,230 --> 00:02:01,370 As a matter of fact there is and this is very common practice all across industry and we call this way 24 00:02:01,370 --> 00:02:08,050 of organizing code modules and modules are simply well these files. 25 00:02:08,180 --> 00:02:16,990 Each one of these files each dot pi file is a module and by building these modules kind of like we built 26 00:02:17,260 --> 00:02:22,630 different functions kind of like we built different classes because inside of these files we can have 27 00:02:22,630 --> 00:02:30,640 classes we can have functions but we can also have different files so a layer higher to divide up our 28 00:02:30,640 --> 00:02:36,710 code and in this section we're going to learn all about modules and try to master it and see how we 29 00:02:36,710 --> 00:02:44,780 can write organized code let's have a look you can see over here if I click on files in our rebel we 30 00:02:44,780 --> 00:02:46,050 have a main dot Pi. 31 00:02:46,700 --> 00:02:50,130 And every time I click run this fall guy run. 32 00:02:50,390 --> 00:02:53,390 But what if we have more files. 33 00:02:53,390 --> 00:03:03,310 What if I add another file here and we'll call it utility dot pi so based on what I just tell you these 34 00:03:03,400 --> 00:03:12,740 two things are what their modules right mean dot pi is a module you tilted up pi is a module and when 35 00:03:12,740 --> 00:03:19,130 writing modules we use the same principles as variables in python which is snake case. 36 00:03:19,130 --> 00:03:26,390 That is if there are spaces we use underscore everything is lowercase letter so how do we divide up 37 00:03:27,380 --> 00:03:29,660 these modules. 38 00:03:29,660 --> 00:03:32,570 And this comes with practice when you're just beginning. 39 00:03:32,570 --> 00:03:36,560 It's hard to say Hey what goes into what file what goes into what file. 40 00:03:36,740 --> 00:03:39,370 But it's similar to classes right. 41 00:03:39,380 --> 00:03:47,160 Similar to functions we want to divide up our code into chunks that make sense again. 42 00:03:47,250 --> 00:03:49,640 Let's say we're working at Netflix. 43 00:03:49,640 --> 00:03:54,980 We might have a file dedicated to videos. 44 00:03:54,980 --> 00:04:03,680 We might have another file or module dedicated to the logging of a user maybe another file dedicated 45 00:04:03,740 --> 00:04:08,990 to analytics and figuring out recommendations for users. 46 00:04:09,140 --> 00:04:17,020 We want to group these classes and functions together inside of a file that makes sense. 47 00:04:17,100 --> 00:04:22,890 Now one of the most common ways to use a module is something like a utility. 48 00:04:23,110 --> 00:04:29,970 Let's say we want to create a utility module that is a module that has a lot of what kind of like a 49 00:04:29,970 --> 00:04:31,180 tool belt. 50 00:04:31,200 --> 00:04:37,500 Very simple functions that we can use all across our project because we can imagine here having multiple 51 00:04:37,500 --> 00:04:38,590 files. 52 00:04:38,790 --> 00:04:41,150 So let's create a very simple example. 53 00:04:41,220 --> 00:04:50,130 I'm going to say a multiply function exists here and this takes a number one and number two and in here 54 00:04:50,130 --> 00:04:57,110 we're simply going to return num 1 times num to a very simple function. 55 00:04:57,630 --> 00:05:01,380 And once again we'll also have a divide function. 56 00:05:01,380 --> 00:05:08,520 And yes this is kind of useless because we can just use the plus or to divide operant in python. 57 00:05:08,520 --> 00:05:12,580 But stick with me here and trying to show you an example. 58 00:05:12,600 --> 00:05:13,010 All right. 59 00:05:13,020 --> 00:05:19,070 So number two I'm going to return num 1 divided by num too. 60 00:05:19,470 --> 00:05:21,330 Nice and simple. 61 00:05:21,330 --> 00:05:30,390 So this is our utility module that has all these functions for us now. 62 00:05:30,450 --> 00:05:33,470 Notice here that we're actually not doing anything in this file. 63 00:05:33,510 --> 00:05:40,170 I'm simply defining functions but I'm not running any of the functions but let's say we have this utility 64 00:05:40,170 --> 00:05:44,650 module and I want to use it in my main dot pi file. 65 00:05:44,700 --> 00:05:53,740 How would we go about doing this well the way we communicate between these two files is quite simple. 66 00:05:53,840 --> 00:06:01,790 All we need to do is use the import command we import and then give it the file name that we want to 67 00:06:01,790 --> 00:06:02,310 import. 68 00:06:02,390 --> 00:06:03,470 What do we want to import. 69 00:06:03,470 --> 00:06:06,890 We want to import utility just like that. 70 00:06:07,040 --> 00:06:13,010 And notice that I'm not adding the DOT PI because we assume that whatever we're going to import is going 71 00:06:13,010 --> 00:06:14,300 to be a python file. 72 00:06:14,300 --> 00:06:18,440 So we don't need to do that just the name just like that. 73 00:06:18,530 --> 00:06:23,260 Now let's print utility here if I run this 74 00:06:26,320 --> 00:06:27,030 Whoa. 75 00:06:27,060 --> 00:06:34,470 Do you see that we have module utility from and then this is a file path that this Web site generates 76 00:06:34,470 --> 00:06:34,980 for us. 77 00:06:34,980 --> 00:06:37,150 So it doesn't really matter. 78 00:06:37,340 --> 00:06:40,970 Now there's a few things that happened here. 79 00:06:40,980 --> 00:06:52,100 One is that we generated this PI cache folder underscore underscore pi cache and oh boy what is this 80 00:06:52,280 --> 00:06:55,520 gibberish hmm mm hmm. 81 00:06:55,540 --> 00:06:57,820 This is this is pretty confusing right. 82 00:06:57,820 --> 00:07:06,590 Well this PI cash is created every time we run a file with let's say enforcements. 83 00:07:06,700 --> 00:07:16,280 So when we're using modules you see what pi cash does is when we click Run the interpreter is going 84 00:07:16,280 --> 00:07:23,240 to create this PI cache folder and it's going to say hey I'm running this file this one main dot pi 85 00:07:23,240 --> 00:07:23,900 file. 86 00:07:23,900 --> 00:07:30,880 Anything that made that pi file imports let's say utility in our case I'm going to cash it. 87 00:07:31,730 --> 00:07:35,200 So see how here it says Dot P Y C. 88 00:07:35,510 --> 00:07:39,240 This is because it's using the C Python interpreter. 89 00:07:39,290 --> 00:07:44,630 Remember this is the C Python which is the interpreter written and c. 90 00:07:44,840 --> 00:07:54,920 So this is actually a compiled file so that next time I click Run here on my main dot pi nothing changes 91 00:07:55,190 --> 00:08:03,020 because what's going to happen is instead of loading up utility dot pi it's going to load up this compiled 92 00:08:03,320 --> 00:08:12,440 version of utility because nothing has changed and utility dot pi and this makes things faster. 93 00:08:12,440 --> 00:08:18,520 When I tried to run main dot pi again that's what caching is caching is hey I'm going to remember this 94 00:08:18,540 --> 00:08:25,090 and this is the compile version so I don't have to go through the compilation step again but notice 95 00:08:25,090 --> 00:08:31,290 that the main dot pi file does not get compiled because while we run it every time. 96 00:08:31,290 --> 00:08:39,100 Now if we change the utility function to not have the divide in there I click Run and you don't notice 97 00:08:39,100 --> 00:08:39,480 it here. 98 00:08:39,490 --> 00:08:45,610 But this has now been rerun because we have a different file. 99 00:08:45,760 --> 00:08:48,790 You see that we have multiply here but not divide. 100 00:08:48,790 --> 00:08:53,850 So let's bring back divide again go back to here click Run 101 00:08:56,960 --> 00:08:57,680 and there you go. 102 00:08:57,680 --> 00:09:02,190 We have divide here to now but this is something that we don't touch. 103 00:09:02,200 --> 00:09:07,430 This just comes underneath the hood with any editor that we use. 104 00:09:07,750 --> 00:09:13,720 And in a few videos we're going to show you how this also happens when you use something like pie chart. 105 00:09:13,750 --> 00:09:20,350 This is just something that editors allow us to do just so our programs can run faster. 106 00:09:20,380 --> 00:09:23,400 So I now have access let's make this a little bit smaller. 107 00:09:23,410 --> 00:09:26,260 I now have access to utility and here's the interesting part. 108 00:09:26,290 --> 00:09:33,880 If I do dot here look at that I have access to divide and multiply so that I can multiply numbers let's 109 00:09:33,880 --> 00:09:42,100 say 2 and 3 click right and I am able to use the utility module in my main file. 110 00:09:42,100 --> 00:09:42,950 Very very cool. 111 00:09:42,970 --> 00:09:48,060 If I do divide and I click Run I can use this as well. 112 00:09:48,070 --> 00:09:49,560 Very very cool. 113 00:09:49,840 --> 00:09:55,600 And I can import as many times as I want if I had a different file in here and I want to import another 114 00:09:55,600 --> 00:09:55,960 thing. 115 00:09:55,990 --> 00:09:58,820 Let's say there is a utility to file. 116 00:09:58,870 --> 00:10:05,980 Well I can import that and import as many things as I want and use them across my files. 117 00:10:05,980 --> 00:10:09,000 How cool is that. 118 00:10:09,010 --> 00:10:16,150 So now we have a way to organize our code and have all these files communicate together because in real 119 00:10:16,150 --> 00:10:22,450 life we have big projects and these big projects are going to be worked on by different teams different 120 00:10:22,450 --> 00:10:26,830 programmers and we need a way to work with all these files. 121 00:10:26,830 --> 00:10:33,940 And by using the import statements between modules we're able to use functionality just like we were 122 00:10:33,940 --> 00:10:41,020 able to use the functionality that functions provided us between different files and most of the time 123 00:10:41,380 --> 00:10:48,160 if you're working at a company the first couple lines on a dot pi file are usually import statements 124 00:10:48,160 --> 00:10:54,410 because usually you're communicating with other files let's learn some more in the next video.