Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Using a built-in library

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now before we ramp up this chapter let's write one more program in our Python REPL, and again it doesn't really make sense to call it a program
0:09 but it kind of is. We're going to cover two new concepts in this video here. We're going to talk about using libraries.
0:18 Now, Python has an incredible amount of features built into it. It has ways to get stuff off the internet to work with the calendars
0:28 and the clock, and date/times and work with collections and do all sorts of interesting things. You could even create a little web server
0:35 out of the stuff that comes bundled in Python but it's not immediately available for you. You have to ask for it as part of your program.
0:42 So, let's see how that goes. We're going to start up our Python REPL again. Remember, macOS and Linux, you type Python3
0:49 Windows 10, you might type Python3 if you got it from the Window's store otherwise you type Python. So, here we are and what we want to do is
0:56 we want to work with this library called sys. Now sys comes with Python you don't have to do anything to get it. Once you have Python, you have sys.
1:03 But if I type that, it says Whoa, we don't know what sys is. So, let's fix that. The way that you work with these libraries is
1:11 you use the import keyword. So we're going to say import we want to work with import sys. If that doesn't crash, if that works
1:20 then that means that sys was loaded. You could ask for import some random set of characters and it would crash because
1:26 there is no library called that, right? But there is a library called sys so that worked and we loaded it. Now if we ask what sys is
1:32 It's a built-in module and sys has a bunch of cool stuff. So we could say sys.versioninfo and here you can see it gives some kind of
1:42 complicated response that seems to indicate there's a major version is 3, minor version is 7 micro version is 6, releaselevel is final
1:51 and serial equals 0. That should correspond pretty closely to 3.7.6 which is the version of Python that we have.
1:57 But now that we have this library loaded we can actually use it, all right? This isn't going to be super interesting but we could come up with
2:04 some kind of string or message where we want to print out a message that Python says, Hello instead of just, Hello, and welcome to Python
2:12 it'll be, Hello, and welcome to Python version 3.7.6 on my machine, but maybe it would say 3.8.1 on yours.
2:18 It will say whatever the version of that, is. So, remember we want a string which we go like this, there's our string
2:24 and we want to format it, so we put the f. Let's say, Hello from Python you can just say it like that and, Hey, hello from Python
2:32 but we want is the version info in here. Now remember, we need to say sys.versioninfo.major and then we say. And this is not going to be pretty
2:47 we're going to do a better version here. sys.versioninfo.minor let's just say from Python 3.7 okay 'cause, it's gettin' long.
2:56 Hello from Python 3.7, well this is a lot to type and you might want to work with those numbers elsewhere and so on.
3:02 So, we could say ma equals the major version so sys.versioninfo like that. We could check what ma is, it's 3 and then mi for minor.
3:18 Here we go, and that will let us I'm just using up arrow to get the history back. That'll let us write something way nicer here. So we can say, ma.mi
3:28 like this. Same thing, but this, I think is a little bit more clear. You don't want to try to cram too much to have too much going on in there.
3:36 So, I kind of like to do it this way but you can do whatever works for you. Here we go, Hello from Python 3.7. If you'd written on your machine
3:44 and you had a different version let's say you had 3.8. This exact same code would say Hello from Python 3.8. Pretty cool, huh?
3:52 Now remember, the reason that that works is because we're using this library. It happens to be this library is built into Python and comes with it.
3:59 You can get others and add it to Python that's something awesome and we're going to do that and really take advantage of some cool stuff
4:05 but for the moment, we're just sticking with the so called, Built-ins. Let's see why they're called built-ins.


Talk Python's Mastodon Michael Kennedy's Mastodon