Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The Python standard library has a number of modules that you can import into your programs and use for your own purposes. This page has a list of them starting here in section 7, under String Services. And you'll see that there are quite a few of them. So we're going to just go through a few of these, a few of the more common ones and show you as an example how you can use them. And I suggest that you go through this list at least once and just look out what they are. And just look at their descriptions.
Just read through the list, so you have an idea of the kinds of things that are available to you. So here in Eclipse, we're going to make a working copy of modules.py. Call this modules-working.py, and we'll open that up. You'll notice that here we're importing sys, and here on the web page you'll see sys is for system-specific parameters and functions. If we open that page, you'll see that there's full documentation on all of the things that you can do with sys.
So, one of these is to get the Python version. This is with sys version info. And so if we run this, you'll see that we say, this is Python version 3.1.2. You can also get the system platform. You can say print(sys.platform) and if we save that and run it, you'll see that this is win32. On my Mac at home it says Darwin and whatever operating system you're running Python on, it will give you an idea.
And this is a category of operating system. It's not actually the operating system. You can get the operating system from the OS module, but this gives you an indicator of what types of services are going to be available on this platform. Like, for instance, if I'm on a win32 platform, I'm not going to do things that are UNIX specific and vise-versa. So, that can be a useful token to see. If we import OS, this is another module that you'll find in that list, you'll notice that I can import inside of the function.
I don't need to do my import at the top. This allows me to actually selectively import things, say I read that sys.platform variable and say I'm going to do certain things one way or certain things another way depending on which platform I get. So I can import modules selectively, and the system won't try to import them if they're not on the right platform or whatever. So, here we can get the os.name for example, and if I save that and run it, you'll see this says nt.
So, that's the name that it's giving this particular type of Windows. And it distinguishes it like from, for example the older Windows 3 or something like that. I can get variables from the environment. I can print os.getenv and say I want the PATH variable. Save and run. And there is the PATH variable from the operating system. So, I can get current working directory, and we'll save and run that.
And there is the current working directory. Of course, this module has a lot of functions in it. Just one more I want to show is the urandom function. This is sometimes useful. Save and run that. This is a function that'll give a string of random bytes. And of course you see that this is the byte type, and I set 25 so it's 25 bytes long. Another useful module is the urllib module, and we'll go ahead and import from that urllib.request, and we'll grab a web page from the internet.
urllib.request.urlopen, and we'll give it a URL here. We'll go ahead and get my homepage, print(page). There we go. Save that and run it. We get this object. And that's an iterable object. So, we can iterate on it and say for line in page: print, and convert each line to a string because they come out binary, and we provide an encoding.
And they have no lines at the end of them. So we'll give it this line ending there. And we save and run and there is an entire web page. So, that's a really useful one. There are a lot of very useful modules in here. I'm just going to take a quick look at a couple of more. One is the random module. It's a very rich random number library. For example, you can print a random number from in a range. randint, A range between 1 and 1000. Save and run.
And then we have the number 726. If we run it again, we get a different number and a different number and a different number. And that's very useful. Another very useful method in this library is the shuffle method. It takes a list. I'm going to just give it a list with a range in it, and if we print that list, you'll see that we have a range of 25 numbers. We can shuffle the list, random.shuffle, and print it again.
And there we have the list shuffled randomly. If we do that a few times, save and run, you can see we get different shuffles each time. Finally, in our little partial tour of the standard library, look at the datetime module. So we'll save that and run it, and there we have the time as of right now when I'm recording this.
In fact, we can do this. now.year, now.month, now.day, now.hour, now.minute, now.second and now.microsecond, and save that and run it. And there we have all of those components, separately usable with these properties of the datetime object.
So, these are just a few of the standard modules that are available with Python. Again I strongly recommend that you look through the documentation for these standard modules. At least read their descriptions so that you get an idea of what's available there, so that as you're going through writing your own code, you're not tempted to reinvent the wheel. The modules that are included with Python and the Python distribution tend to be very well written. They tend to be very feature rich and they tend to be quite well optimized and reliable. So, I suggest that you use them when you have an opportunity.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104359 Viewers
56 Video lessons · 116265 Viewers
71 Video lessons · 85498 Viewers
131 Video lessons · 40936 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.