WEBVTT

00:00.050 --> 00:01.100
And just one more thing.

00:01.100 --> 00:07.100
Before we finish this Python section, let's talk about Python modules and see how to use them.

00:07.100 --> 00:12.650
So in Python or any other language you get a core set of functions and keywords.

00:12.650 --> 00:16.730
For example, the print function is part of the core Python functions.

00:16.730 --> 00:23.570
You can use the keyword def to create a function, etc. but this core base is somehow quite limited.

00:23.570 --> 00:29.450
If you want to connect to Gpio or start a web server or anything more complicated, you will have to

00:29.480 --> 00:33.950
write a lot of code all by yourself and this will take forever.

00:33.980 --> 00:38.540
That's why you can use libraries which are called modules in Python.

00:38.540 --> 00:44.990
The libraries contain a set of plug and play functionalities to solve a specific problem.

00:44.990 --> 00:48.020
Usually one library contains different functions.

00:48.020 --> 00:54.950
Later in this course we will use a Python module to use gpios, another one for the camera, etc. you

00:54.950 --> 00:58.550
can also have Python modules to do mathematics operations.

00:58.550 --> 01:00.920
Compute all kinds of statistics.

01:00.920 --> 01:04.250
Well, the number of possibilities is endless.

01:04.250 --> 01:10.290
And here I'm going to show you just one library and one function, you can get from it as an example.

01:10.290 --> 01:16.860
So let's say you want to make your program stop for one second, two second, whatever time you want.

01:16.890 --> 01:20.430
How to do that with just loops, conditions, functions and variables?

01:20.430 --> 01:22.200
Well, that's going to be very hard.

01:22.200 --> 01:24.660
So you can use the module called time.

01:24.660 --> 01:30.150
And with time you can make your program what we call sleep for a specific amount of time.

01:30.180 --> 01:32.730
How to import this time module.

01:32.730 --> 01:36.150
Well you have the import keyword you can see in Python.

01:36.150 --> 01:41.010
So you write import and then the name of the module which here is time.

01:41.010 --> 01:44.250
And just to test that we can just run this program.

01:44.610 --> 01:46.200
And you can see nothing happens.

01:46.200 --> 01:52.410
But if nothing happens if we don't have any error it means that this line was correctly executed.

01:52.440 --> 01:55.320
Now let's say I add a more letters.

01:55.320 --> 01:57.330
So this module doesn't exist.

01:57.990 --> 02:01.380
Now we have a module not found error okay.

02:01.410 --> 02:03.960
No module named with this name.

02:03.960 --> 02:06.780
So you can see if I just write import time.

02:06.780 --> 02:11.040
It's not doing anything because it's actually recognized.

02:11.040 --> 02:19.550
And then in this module time we have a function called time dot sleep, and I can put a integer, for

02:19.550 --> 02:20.870
example, or a float.

02:20.900 --> 02:23.870
I can put one second like this as an integer.

02:23.900 --> 02:25.070
It would also work.

02:25.100 --> 02:29.450
This means that I'm going to make the program stop for one second.

02:29.480 --> 02:32.420
That can be very practical if you want to measure.

02:32.420 --> 02:35.540
For example, I'm coming back to this temperature sensor example.

02:35.540 --> 02:43.010
If you want to get one temperature every second, then you can just use this function here between every

02:43.010 --> 02:44.300
sample that you get.

02:44.300 --> 02:48.980
And just to give you an example, I will print a like that.

02:48.980 --> 02:53.900
And then I will print B after like this.

02:53.900 --> 02:57.980
And if I run let's look at what's happening here on the shell.

02:58.010 --> 03:03.650
So if I run that you have a and then you have B but not immediately okay.

03:03.680 --> 03:06.350
B is coming one second after a.

03:06.380 --> 03:06.680
All right.

03:06.680 --> 03:12.320
So that's one example of one module that we import with one function that we use.

03:12.320 --> 03:18.020
And as we progress with the course we are going to use many more modules that will allow us to use cool

03:18.020 --> 03:21.170
functionalities with Python and the Raspberry Pi.
