The standard library includes a wide selection of useful modules for a great variety of uses. These are included in your default Python installation.
- [Instructor] The Python Standard Library includes a wide selection of modules available for your use in a standard Python installation. If we scroll down here you see, this is the whole library so it also includes functions, and classes, and all kinds of things; but starting in section six, these are all modules. And for example, here's the documentation for the sys module, and this is system-specific parameters and functions.
And so you have many, many things you can do. So let's take a look at how this works. Here in Komodo I've opened a working copy of modules.py from chapter 14 of the exercise files and you notice the import statement, that's where I import a module, and then I use a module like this just as any other object. I say sys dot and version_info is an element inside of the class of the module and version_info returns a collection of information, and so I can use it with an asterisk here and it will give me a list of parameters, and in this case the list of three for the major, minor, and minor parts of the Python version number.
When I run this it says, Python version 3.6.3. Of course, your version may be different if you have a later version of Python. In any event, the system or sys module, provides a number of things. For example we can get platform. Now this is the platform, not the operating system, and so when I run this it says darwin because I'm on a Mac and that's what the Mac reports its platform as. If you're on a PC or something else then you'll get a different string.
On the other hand if we import os, we get operating system related functions, and I can say, os.name and when we run that, we get posix, because a Mac runs macOS or OS10, which is an implementation of BSD Unix, which reports as posix because it's a standards compliant Unix variant. 'Course if you're on Windows or something else, you'll get a different string here.
OS gives us a number of interesting and useful things. There is a function called getenv, and I can get from the environment, I can get a particular variable and in this case I'm going to get the path, and when I run this you see there's my path. Again, yours may be different, it probably is. I can get the current working directory, getcwd, and there's my current working directory. Again, yours will be different. Or I can get a random number.
This is from the operating system random generator. And I'm going to get a random number 25 bytes long, and when I run this you notice I get 25 bytes, so that's a byte object. If I want to I can see that in hexadecimal using the hex method on the byte object, and when I run that I get this nice hexadecimal number, which is cool and useful. And every time I run it, I get a different number. There is a whole random module which does more cool things with random numbers.
I can say x = list, or rather, we'll start with this, X = random.randint. So I'm giving it a random integer between 1 and 1,000, and print(x). And that's a random integer between 1 and 1,000 when I run it, every time I run it I get a different number, and you should too. I can shuffle a list, so remember a list is mutable, so I need a list, not a tuple.
List of, give it a range, say 25 numbers, and when I print it I get a list of 25 numbers, and I can say, random.shuffle(x), and then I print(x) again, and now it's shuffled. And every time I run it, I get a different shuffle. So that can certainly be useful. There is a date time module, just looking at a few of them here. Obviously there are a lot more.
There's a date time module which can be very useful. Let's say now = datetime.datetime.now() and if I print(now) you see that I have this string representation, year, month, day, hour, minute, second, and microsecond. On the other hand I can say now.year and just get the year part, now.month, day, hour, minute, and I can get each of these as a separate number.
Seconds and microseconds, and when I run this, you see those are all the separate numbers, year, month, day, et cetera. I strongly suggest you go through the documentation and find out what modules are available, so that you can use them and not reinvent the wheel. Modules included in the standard library tend to be very well written, feature rich, and useful.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api