WEBVTT

00:00.080 --> 00:03.170
Let's start with a very simple Python program.

00:03.350 --> 00:07.310
First of all, well, where can you write and execute Python programs?

00:07.340 --> 00:11.990
Here we will use a software that is already installed for you on the Raspberry Pi OS.

00:11.990 --> 00:14.780
And this software is called Thonny.

00:14.870 --> 00:22.940
So to find it you can just go under menu here and click on programming and you will find Thonny here.

00:22.940 --> 00:30.830
So you just click on this and this is going to open Thonny here which is a Python IDE.

00:30.980 --> 00:35.570
And IDE here means integrated development environment.

00:35.570 --> 00:38.240
So it has everything for you to develop with Python.

00:38.240 --> 00:43.940
And the first thing we are going to do here, this is a simplified mode that we have.

00:43.940 --> 00:46.070
We are going to switch to regular mode.

00:46.070 --> 00:50.660
So you click on this here you click on okay.

00:50.720 --> 00:52.670
And now you need to restart it.

00:52.670 --> 00:57.590
So let's restart programming and Thonny.

00:58.400 --> 00:58.820
Okay.

00:58.820 --> 01:04.690
Now we have the normal mode and you can keep it like that, but I'm going to increase the font size.

01:04.720 --> 01:04.990
Okay.

01:05.020 --> 01:05.710
Here you have this.

01:05.710 --> 01:11.320
Or you have the shortcut so that you can actually see what I'm doing on my screen.

01:11.350 --> 01:11.560
Okay.

01:11.590 --> 01:14.260
So I use a bigger font size.

01:15.970 --> 01:20.530
So this tool is a great tool where you can write Python code.

01:20.530 --> 01:23.290
You can save Python programs into files.

01:23.290 --> 01:27.010
And you can also run and debug your programs.

01:27.010 --> 01:32.680
It is very handy because setting up Python with the environment can be quite tricky here.

01:32.710 --> 01:35.980
Nothing to do, just open up thonny and you're good to go.

01:35.980 --> 01:40.390
So we are first going to look at the bottom part here named shell.

01:40.600 --> 01:47.230
Just if you ever close shell by mistake or if it's not here, you can simply open it again.

01:47.260 --> 01:51.520
You just go on view and then shell okay.

01:51.550 --> 01:52.690
And it's back here.

01:53.860 --> 02:00.710
This shell part is an already configured Python environment where you can simply run any Python command

02:00.710 --> 02:03.440
you want and it will be executed right away.

02:03.470 --> 02:06.740
Let's go ahead and do a basic Hello World program.

02:06.740 --> 02:10.340
So what we want to do here is simply to print some text.

02:10.370 --> 02:14.090
You can use the already existing print function.

02:14.090 --> 02:16.730
You can see syntax highlighting here okay.

02:16.760 --> 02:18.890
And you need to open and close parentheses.

02:18.890 --> 02:21.440
So print open close parentheses.

02:21.440 --> 02:24.080
And inside we're going to use double quotes.

02:24.710 --> 02:32.840
And inside those double quotes let's say hello Raspberry Pi okay so that's our Hello World program.

02:32.840 --> 02:34.370
You can press enter.

02:35.030 --> 02:38.000
And you see we have the result of that comment okay.

02:38.030 --> 02:39.590
So we have run a Python command.

02:39.590 --> 02:43.370
And the result of that command is we see hello Raspberry Pi okay.

02:43.400 --> 02:48.500
We simply asked Python to print this piece of text.

02:48.500 --> 02:50.360
So the Python interpreter.

02:50.390 --> 02:56.090
That's how Python works is going to use a Python interpreter to execute each command one by one.

02:56.090 --> 02:57.590
So here we just have one for now.

02:57.590 --> 03:02.850
So it's going to execute this command and print the result or do whatever you ask it to do.

03:02.880 --> 03:03.390
Okay.

03:03.420 --> 03:04.980
Now we are back here.

03:04.980 --> 03:08.820
You can use the up arrow if you want to go back to the previous command.

03:08.820 --> 03:11.460
So you can use the up arrow and just press enter again.

03:11.460 --> 03:14.040
And you can see that we have Hello Raspberry Pi again.

03:14.040 --> 03:17.130
Now you could create entire programs like that on the shell.

03:17.160 --> 03:22.620
But as soon as you close the shell, your program will be gone and it's not going to be very practical.

03:22.620 --> 03:28.440
So what we're going to do from now on is to write the Python programs into a file, okay?

03:28.470 --> 03:32.850
So you can save them, you can reopen them and also run those programs.

03:32.850 --> 03:38.220
And to do that, well, you simply are going to use this text editor right here.

03:38.250 --> 03:38.580
Okay.

03:38.610 --> 03:41.010
So this is the shell but this is a text editor.

03:41.010 --> 03:45.690
So I'm going to write here same thing I'm going to write the print function.

03:45.720 --> 03:49.800
I'm going to open and close the parentheses and then put double quotes.

03:49.800 --> 03:51.240
So one and two.

03:51.240 --> 03:55.230
And inside this I'm going to say hello.

03:55.230 --> 03:57.480
And I'm going to change to RPI.

03:57.510 --> 04:00.530
Now You can know that when I press enter here.

04:00.560 --> 04:01.640
Well, it doesn't do anything.

04:01.640 --> 04:03.830
It actually goes to a new line.

04:03.830 --> 04:05.780
You can see I have one and then I have two.

04:05.810 --> 04:08.180
If I press enter again I go to a new line.

04:08.180 --> 04:11.600
So this is simply what you would expect from a text editor.

04:11.600 --> 04:13.220
So how to actually run this.

04:13.220 --> 04:16.310
Well you have this play button here run current script.

04:16.310 --> 04:17.660
So we're going to run that.

04:17.660 --> 04:20.210
And you can see it's going to run here.

04:20.210 --> 04:23.870
And you see hello RPI.

04:23.870 --> 04:27.080
And if you want to run it again you just click here again.

04:27.110 --> 04:28.820
Now you can save this file.

04:28.820 --> 04:32.120
So as for now you can see it's untitled and not saved.

04:32.120 --> 04:38.180
So to save it you just go on file save or save as okay.

04:38.210 --> 04:41.210
And when you save it's going to open this.

04:41.210 --> 04:49.010
And what it looks like with my configuration it's going a bit it's a bit oversized but it's okay I still

04:49.010 --> 04:49.610
have space.

04:49.610 --> 04:56.270
I'm just gonna well I'm going to go to documents here so you could save the file any way you want.

04:56.300 --> 04:56.540
Okay.

04:56.570 --> 04:58.220
Here we are in the, uh.

04:58.220 --> 05:00.080
We're going to see this a bit more later.

05:00.080 --> 05:04.670
We are in the home directory for your user Pi, but we're going to go to documents.

05:04.760 --> 05:05.420
Okay.

05:05.450 --> 05:09.830
And in documents you may create a new a new folder.

05:09.860 --> 05:15.140
Let's name it Python underscore programs like this.

05:15.470 --> 05:18.350
And you can click on create okay.

05:18.380 --> 05:21.290
So we just save inside a folder.

05:21.290 --> 05:23.000
It doesn't really matter which one.

05:23.000 --> 05:29.330
And for the name I'm just going to name it hello Dot p y.

05:29.360 --> 05:31.190
So the name of the file is hello.

05:31.190 --> 05:34.040
And then I have the p y extension okay.

05:34.070 --> 05:35.630
Pi extension is very important.

05:35.630 --> 05:38.390
That's the extension for any Python program.

05:38.390 --> 05:41.180
Then I can press enter and it is saved.

05:41.180 --> 05:48.650
So then I can go to file um open and I can find the file again I click on escape here.

05:48.680 --> 05:49.220
Great.

05:49.220 --> 05:51.320
You can now create a Python file.

05:51.320 --> 05:52.070
You can save it.

05:52.070 --> 05:54.410
You can load it and you can run it.

05:54.410 --> 05:58.210
And what we've seen is how to print something on the screen here.

05:58.240 --> 06:03.760
Now, in this program where we only print one thing, but you could also add another print.

06:04.450 --> 06:04.810
Okay.

06:04.840 --> 06:06.490
For example test.

06:06.580 --> 06:06.790
Okay.

06:06.790 --> 06:12.700
So you write print, you open parentheses and then you write one double quotes.

06:12.700 --> 06:16.000
And then you close the double quotes and you close the parentheses.

06:16.030 --> 06:17.500
Let's play that.

06:17.680 --> 06:21.010
And you can see we have hello API in test.

06:21.010 --> 06:26.320
So we have the two pieces of text that are printed one after the other.

06:26.320 --> 06:28.660
And you can see here that the order is very important.

06:28.690 --> 06:29.200
Okay.

06:29.200 --> 06:34.540
The program is going to be executed from line one and line two, line three etc..

06:34.570 --> 06:36.130
Or you can see here I have a red dot.

06:36.160 --> 06:39.130
If you click I'm just going to click again and remove it.

06:39.130 --> 06:41.470
So the order for the code is very important.

06:41.470 --> 06:43.750
And we're going to come back to this quite often.

06:43.750 --> 06:47.170
You can also add as you can see you can add empty lines okay.

06:47.200 --> 06:50.170
If you add empty lines it's simply not going to do anything.

06:50.200 --> 06:52.360
You see the result is the same.

06:52.390 --> 06:52.600
Okay.

06:52.630 --> 06:55.120
So an empty line is not going to be executed.

06:55.120 --> 06:57.090
So here we execute line one.

06:57.090 --> 06:59.190
And then line two and three is nothing.

06:59.190 --> 07:02.250
Then we execute line four and then five and six.

07:02.250 --> 07:03.120
There is nothing.

07:03.120 --> 07:09.900
One other functionality I want to show you right now is how to comment one line.

07:09.900 --> 07:12.600
So let's use the hash sign here.

07:12.810 --> 07:14.520
And you can see now it turns gray.

07:14.520 --> 07:20.370
When you use the hash sign it's gonna comment the line, which means that this is not going to be executed

07:20.400 --> 07:20.580
okay.

07:20.610 --> 07:25.170
It's still here on the screen, but it's not going to be executed if I run again.

07:25.170 --> 07:28.560
You can see we only have hello API.

07:28.650 --> 07:29.310
Okay.

07:29.310 --> 07:31.410
And this one is not executed.

07:31.440 --> 07:36.390
Now, if I remove this and I run again, it is executed again.

07:36.420 --> 07:36.630
Okay.

07:36.660 --> 07:42.630
So you can add a comment in order to tell Python to ignore some lines of code.

07:42.630 --> 07:43.980
And this is very useful.

07:43.980 --> 07:49.620
You can use commands a lot with Python because for example, you have some code here and you want to

07:49.650 --> 07:55.650
just ignore one part so that you can test your program without one part and then put it again.

07:55.650 --> 07:58.140
Then you don't need to remove the code entirely.

07:58.140 --> 08:03.210
You just put comment, you test the code and then you can remove the comment if needed.

08:03.240 --> 08:03.930
Okay.

08:03.930 --> 08:09.960
And you can also use comments not just for that, but just for explaining stuff in your code.

08:09.960 --> 08:16.860
So for example the next line will print text.

08:17.040 --> 08:22.740
Okay, so here it's pretty obvious, but sometimes you might want to explain a bit more what some parts

08:22.740 --> 08:28.320
of the code are doing, or to give some more information about why you did something in that way.

08:28.350 --> 08:31.530
Okay, so you can use some comments in this case.

08:31.530 --> 08:36.870
And finally, to finish with this lesson, well here we have written everything perfectly.

08:36.900 --> 08:38.100
Okay, so it works.

08:38.100 --> 08:41.910
But let's say you forget a parenthesis here.

08:41.940 --> 08:43.770
Well, first you might see some weird thing here.

08:43.800 --> 08:45.390
Okay, you can see it's all gray.

08:45.390 --> 08:50.880
But let's say I run the script, you can see that we're going to get an error because here you can see

08:51.180 --> 08:52.620
in this error we have.

08:52.620 --> 08:56.030
So the name of the file, which is this one?

08:56.090 --> 08:57.710
The number for the line.

08:57.740 --> 08:59.210
Okay, here it's line one.

08:59.210 --> 09:01.880
And then you see we have an error.

09:01.880 --> 09:06.200
So sometimes this is going to be explicit sometimes not okay.

09:06.230 --> 09:08.330
So sometimes it's easy to find the error.

09:08.330 --> 09:10.160
But it's not always obvious here.

09:10.160 --> 09:11.090
It's pretty obvious.

09:11.090 --> 09:12.980
You see we have a syntax error.

09:13.010 --> 09:15.290
The parentheses was never closed.

09:15.290 --> 09:17.570
So we know we have to close that parenthesis here.

09:17.600 --> 09:20.330
But let's say I forgot to close the quote.

09:20.330 --> 09:21.560
So I'm going to run again.

09:21.560 --> 09:23.570
And you see we have a different error here.

09:23.570 --> 09:25.460
So still on line one.

09:25.760 --> 09:26.990
Still a syntax error.

09:26.990 --> 09:31.040
But here you can see Unterminated string literal okay.

09:31.070 --> 09:33.560
So we need to finish with a quote here.

09:33.590 --> 09:40.010
Now let's say for example instead of print I just write print okay I forgot the T.

09:40.700 --> 09:43.610
If I run we have a different error here.

09:43.610 --> 09:44.840
And now you can see.

09:44.840 --> 09:47.360
So this was executed on line one.

09:47.360 --> 09:54.610
But then on line four here you can see in line four we have an error because the name print is not defined.

09:54.640 --> 09:54.850
Okay.

09:54.880 --> 10:01.600
We need to write print so you can see when you do a mistake here you will get different kinds of errors.

10:01.630 --> 10:01.840
Okay.

10:01.840 --> 10:03.250
When you run the program.

10:03.250 --> 10:06.640
And that can help you to fix the mistakes in your code.

10:06.670 --> 10:06.910
All right.

10:06.940 --> 10:08.170
Now if I run again.

10:08.380 --> 10:08.890
All good.

10:08.890 --> 10:09.220
Great.

10:09.220 --> 10:11.170
So with this, you've already seen a lot.

10:11.200 --> 10:13.000
And this is a really good first step.

10:13.000 --> 10:15.970
Now let's continue and discover variables.

10:16.870 --> 10:18.370
And just a quick parenthesis.

10:18.370 --> 10:25.030
If you have the same problem as me, if you go on file save as if you want to load and it's too big

10:25.120 --> 10:25.420
okay.

10:25.450 --> 10:26.620
Press escape.

10:26.620 --> 10:34.750
And what I found is well, it's a bit tricky, but if you quit here, the full screen, I mean, that's

10:34.750 --> 10:38.890
something that works for me if I go smaller.

10:41.530 --> 10:46.540
If I go smaller and I open again.

10:48.940 --> 10:52.480
And then I go to file save as.

10:53.740 --> 10:56.320
maybe I can guess.

10:56.320 --> 11:03.130
So somehow if you put a smaller, if you put the screen very small, you open Sony again, then you

11:03.160 --> 11:07.600
click on save as it's going to be too big and then you increase the size of the screen.

11:07.600 --> 11:10.900
Then you can get to the edges of that and that's what you want to do.

11:10.930 --> 11:14.320
You can reduce you can reduce this okay.

11:15.100 --> 11:20.290
So what I still it's still out but I can reduce it like this.

11:20.980 --> 11:23.140
And let's just click on cancel here.

11:23.140 --> 11:30.850
And now if I go to File Save as hopefully the size is the same as the one you have defined before okay.

11:30.880 --> 11:40.240
So I'm going to go back full screen F8 full screen I'm going to open Sony again okay.

11:40.270 --> 11:43.510
But whatever size I want I can put it full screen if I want to.

11:44.320 --> 11:48.220
And let's go to File Save as okay.

11:48.250 --> 11:52.720
And now I have a normal size window and the problem is solved.
