[MUSIC] Up until this point we've put all of our code into a single file, right? We've just edited in CodeSkulptor and hit the save button. Okay, in reality, you often want to build programs out of modules. You'd like to have little pieces from here and there and everywhere. There are some modules that have been built specifically for Python, and so they're included with Python, or in our case, CodeSkulptor. There are modules that maybe somebody else wrote, that you would like to use, and you don't want to have to rewrite them, and you might yourself write some modules. It may be convenient for you to break up your program into multiple pieces, both so that it's more easy to understand for you now, and so that you can reuse parts of it later. So let's look at how we do that in Python. Okay, so in order to get access to a module in Python, you have to import it, and that's what the import statement does. So here at the top of this program I have import math, and what that's going to do is import the Math Module. So the first question you should ask is, what is the Math Module? Well, the math module is something that comes with Python, so we can read about it in the docs. So let's go over to the docs here, and let's find Math Module, here we go. And you can see that the Math Module has a whole bunch of operators and functions, and we can gett documentation on them. Okay, so let's go back, all right. I'm also importing another module, and I'll talk about that in a minute, all right? So the first thing the math module has, and any module can have this, but maybe depending on what it's trying to do, is some constants. So let's print the constants, math.pi and math.e, all right? And so this is the first thing you have to understand, if I'd like to access something in the Math Module, I first import it, then I can access things with this dot notation. So math, is saying mathmodule.pi, meaning pi, which is a variable inside of the Math Module, okay? So now I've printed out the constants pi and e, all right? There are also a bunch of functions inside the Math Module. So let's print math.sqrt(25), print math.trunc(14., And print math.sin(math.pi / 2.0). Okay, to try that, and now we've called a bunch of functions that are available to us within the Math Module. Now, how do I figure out all these things? Well, like I said, you can go to the docs, but there is also a Python built-in function called dir. And what dir does is basically directory, give me a directory of the Math Module. And so I do that, and I get this big list of things here that tells me what I can do. And you can see there's e, right that we used, there's pi, okay? There's sin, sqrtt and trunc, okay? So, this is a way that you can see what is available. Now let's go back up to the top here where I imported examples modules. There's something else here, I said import examples_module as example. You can use as in an import to change the name that I want to use in the file here, okay? So when I said import math, it imports the module called math, and allows you to access it with the name math. So when I want to get at pi, I type math.pi. I could have said import math as laundry, [LAUGH] okay? And you shouldn't do this, right? It makes your programs very unclear to people when you rename a module like that, all right? But sometimes it's convenient if the module name is extremely long. So given the way that CodeSkulptor works, the module names might end up being a little bit unwieldy, so we import examples_module. I just want to call that example, so I say import examples_module as example. Now again, what that means, the module name is examples_module, but I'm going to access it by using the word example in this file. So I say print dir(example), look at that, what's inside example? Well, we'll see example has this thing called message, all right? And I'm ignoring that __name__ thing, I'll show you that in a second, okay? But let's print example.message, and see what it has to say. [LAUGH] Python is fun, well of course it is, all right? Now that name thing is a sort of an internal Python thing, let's print it out. You really don't need to care about this, but just for fun, let's do it. Hey, what do you know? It's the actual name of the module. And notice that when I printed out example.__name__, I got the actual name of the module, which is examples_module. So this is a way, if you rename it, that you can actually find out what its real name is. Now I want to show you one aspect of importing modules that is unique to CodeSkulptor. Let's say that I am writing a CodeSkulptor file, and let's say in this file, I just have some constant called number, I set it = 42, okay. Now you would probably write some more interesting code, but this will serve to be a large enough example so that we can understand what's going on. I click the save button, and as we know, the user hash changes up in the top here. And what I need to do if I want to import this into another piece of code, is to remember this part here, okay? So I take that part of the URL, I copy it, and let's just open a new CodeSkulptor file. All right, and let's delete what's there. Okay, I can now import that, and I probably don't want to keep writing that everywhere that I want to use something, so let's call this as mymodule, all right? Now, I can access things inside that module, so let's print our constant, all right, and run it, okay. And we get the number 42, all right, so this works. And one thing that you ought to keep track of is, when you actually change the other module, you have to come back here and potentially change the import number right? I might want to change this to be _1, or _2 as I update things, right? And there's the benefit here of doing as mymodule, that means I don't have to change any of the rest of the code, okay? It's still going to be mymodule.number, all right, no matter which version here. Okay, so if I change the code, I update functions, whatever, I change this import statement and then my new, program will get the newly updated module as well, okay? Python programs generally don't stand alone, so pretty much every Python program you write is going to have to import some modules, okay? It's not that difficult, you simply use the import statement, and then you use the module name and a dot in order to access the elements of that module. Now, I want to caution you with using as to rename modules. Please don't do this gratuitously just because you don't like the module name, all right? Use it when the module name is large and unwieldy, and there's a shorter name that's both clear and descriptive, okay? And if you do that, then you're increasing the clarity of your program, rather than decreasing it. So hopefully now, you'll be comfortable with using modules in your programs.