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