Locked lesson.
About this lesson
Using third party modules in your code, and creating your own.
Quick reference
Modules
A module is a file that holds Python code which you can import and use.
When to use
Use them whenever you want to add functionality to your code.
Instructions
To see a list of built-in modules that you can use:
- https://docs.python.org/3/py-modindex.html
To create your own modules, just create a file and save it with a .py file extension, then import it to your code:
import MODULNAME
Be sure to save your module file in the same directory that you saved your original program code.
Hints & tips
- https://docs.python.org/3/py-modindex.html
- 00:04 In this video, I want to talk about modules.
- 00:06 And we've already looked at modules very quickly,
- 00:09 way back to the very beginning at the course when imported this OS module.
- 00:13 And in the last video we imported the random module.
- 00:17 And a module is just a file that contains Python code.
- 00:21 It's sort of like a function,
- 00:23 a little program within your program that you can import and use however you want.
- 00:27 So just like functions there are built in functions.
- 00:30 And you can make your own functions.
- 00:31 Same thing with modules, there are lots of built in modules that come with Python and
- 00:35 you can create your own.
- 00:37 So we're going to create our own in this video very quickly.
- 00:39 But before we do that I want to look at a list of sort of the main
- 00:43 modules that you could play around with that come with Python already.
- 00:46 So if we head over to Google and just type in Python 3 modules,
- 00:51 we see this Python module index.
- 00:53 And this is the Python 3 official docs, the documentation.
- 00:57 And you could just see here's a big long list of all the modules.
- 01:00 And if we kind of scroll through here, we could go,
- 01:02 here's that OS we've been using this whole time.
- 01:05 Let's see, there's the random one.
- 01:06 And if you want to learn about these things just click on it and you can come
- 01:09 through and read the documentation and get all the information you want.
- 01:13 So we just looked that the random module in the last video.
- 01:15 Here we have randint with a,b, just like what we just did.
- 01:19 So you can come here and read all about it, learn all the tricks and
- 01:22 the neat things you can do with these modules.
- 01:25 And that's in the Python documentation.
- 01:27 So take a minute to browse through here and just kind of look at all
- 01:30 the cool stuff that just comes with Python that you can use.
- 01:32 And to use them, generally, you just type in this import and
- 01:36 then the name of the module.
- 01:37 Sometimes you might do something like from random import ranint, right?
- 01:44 because we use that randint function in the last video.
- 01:47 For the most part, you're just going to import the name of the module.
- 01:50 So those are modules.
- 01:52 Now very quickly we're just going to create one.
- 01:53 I'm going to come over here to our directory, right-click and
- 01:56 create a new file.
- 01:57 And I'm just going to call this namer_module.py.
- 02:01 And generally, it's like any Python file, it ends in py.
- 02:05 And let's just recreate our namer function, remember that, namer?
- 02:08 Name and then return.
- 02:11 Let's see, what was it, hello and
- 02:14 then, Name, right?
- 02:19 So to use this name or module, and I should say when
- 02:22 you create a module save it in the same directory that your main program is in.
- 02:27 because that's the first place Python's going to look for your module.
- 02:29 And so we're in this Python course directory, so that'll work fine.
- 02:32 Otherwise, you could save it in the directory where you saved Python if you
- 02:36 downloaded it onto your computer.
- 02:38 That's sort of the next place Python will look.
- 02:40 But your best option is just to keep it in the same directory.
- 02:43 Let's import this guy, so import namer module.
- 02:47 And now we're good, we've now imported it and we can use it however we want.
- 02:51 So we can use this like we would any other,
- 02:54 like a function call that we did in the earlier videos.
- 02:56 So we could go print.
- 02:58 And then we just call the name of the module, namer_module.
- 03:02 And then .namer because inside our module we're going to call the namer function.
- 03:08 So we just slap a .namer.
- 03:09 That's an object oriented way to do it.
- 03:11 And then we need to pass it a parameter, let's call it John.
- 03:15 And if we save this and run it, boom, we get Hello John.
- 03:18 We could pass a different parameter, a different argument.
- 03:22 Boom, Hello, Tim.
- 03:22 So it acts just like our function did before.
- 03:26 But now that function and anything else we care to put in here,
- 03:30 is in its own file that we can use.
- 03:32 It allows us to separate different chunks of code and
- 03:36 to keep things better organized.
- 03:38 And it's really easy to use this, just call it.
- 03:40 All we have to do is make sure we've imported the module at the beginning
- 03:44 of our program.
- 03:44 So those are modules super useful,
- 03:47 super important really for any kind of programming language.
- 03:50 So easy to do with Python and pretty cool.
- 03:52 So in the next video we're going to look at getting user input.
Lesson notes are only available for subscribers.