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