WEBVTT

00:00.110 --> 00:04.400
In this lesson you will learn how to work with Python inside the terminal.

00:04.430 --> 00:07.430
We will start our Python programs directly from the terminal.

00:07.430 --> 00:09.500
We will also improve them.

00:09.500 --> 00:11.480
So let's start actually with the Python shell.

00:11.480 --> 00:14.690
This is something that we have seen at the very beginning of the course.

00:14.690 --> 00:20.990
So if I come back to thonny, while the shell is what you have on the bottom right, and if you remember

00:20.990 --> 00:23.690
we did something like print hello.

00:25.070 --> 00:25.220
Okay.

00:25.220 --> 00:27.980
You can run commands right here from the shell.

00:28.010 --> 00:32.960
And so this is something you can do in Thonny, but you can also do that directly from the terminal.

00:32.990 --> 00:36.500
How to do this you just type Python three.

00:36.530 --> 00:39.830
You press enter and you are in a Python shell.

00:39.830 --> 00:41.330
So I can do print.

00:42.350 --> 00:44.510
Hello for example.

00:44.510 --> 00:46.040
Well, I can do whatever I want.

00:46.070 --> 00:51.710
I can create a variable, I can print this variable.

00:51.710 --> 00:54.710
I can just run any Python comment that I want.

00:54.740 --> 00:59.930
And note here that if I press Ctrl C, you see I have a keyboard interrupt.

00:59.930 --> 01:01.880
We can come back to this at the end of this video.

01:01.880 --> 01:05.530
So we have a keyboard interrupt, but we are still in the shell.

01:05.560 --> 01:07.030
How to exit from the shell.

01:07.030 --> 01:13.840
We have the exit function with parentheses and then we are back to the terminal.

01:13.870 --> 01:14.170
All right.

01:14.200 --> 01:17.560
So if you want to use the shell you can see you just type Python three.

01:17.590 --> 01:21.910
Then you just know that you have to also write exit with parentheses to exit.

01:21.910 --> 01:23.890
And you can use the Python shell as you want.

01:23.920 --> 01:28.000
Now you can also run a Python program directly from the terminal.

01:28.000 --> 01:29.530
That's going to be super useful.

01:29.530 --> 01:30.070
Let's go.

01:30.070 --> 01:36.760
So with CD to documents and Python programs LZ what do we have here.

01:36.760 --> 01:39.460
So for example we have the Hello.py.

01:39.490 --> 01:43.690
So what we have done so far is for example we go on file, we open.

01:44.740 --> 01:46.180
Uh let's go back to this.

01:46.180 --> 01:46.690
Hello.

01:46.720 --> 01:49.000
Well you can just open any program okay.

01:49.000 --> 01:50.020
It doesn't matter here.

01:50.020 --> 01:52.420
You see that we can click on run.

01:52.420 --> 01:53.710
That's what we've done here.

01:53.740 --> 01:58.420
And so here well it's going to create a variable and then print the same variable three times.

01:58.420 --> 02:00.730
So we have 12 1212 as a result.

02:00.730 --> 02:04.200
So let's go back to the terminal and how to run this in the terminal.

02:04.200 --> 02:07.440
Well, you just use Python three, just like we did before.

02:07.440 --> 02:12.090
But then instead of just running Python three, you do space and you provide the file name.

02:12.090 --> 02:15.960
And this file name must be a Python file with the Python extension.

02:15.990 --> 02:16.410
Okay.

02:16.410 --> 02:18.120
So for example hello.

02:18.120 --> 02:21.120
And I can press tab for autocompletion okay.

02:21.150 --> 02:22.410
Hello.py.

02:22.410 --> 02:25.470
And you see we execute this from the terminal okay.

02:25.500 --> 02:27.390
So that's going to be very useful.

02:28.320 --> 02:32.460
And before we go further just a quick parenthesis if you run.

02:32.460 --> 02:38.940
So sometimes might happen if you run a program that uses the Gpio library.

02:38.940 --> 02:43.200
So for example if you use the push button, the LED, the Pi R sensor etc..

02:43.200 --> 02:48.660
So if you use a program that uses the Gpio zero library here and then you try to.

02:48.690 --> 02:52.530
So if you run this on Sony and then you try to run it on the terminal, maybe you're going to get an

02:52.530 --> 02:54.420
error in the terminal.

02:54.420 --> 02:59.640
And if that happens, well what you have to do is to close first Tony okay.

02:59.670 --> 03:04.410
So you close Tony completely and then you run the code on the terminal.

03:04.440 --> 03:09.970
So just in case you have an issue with the Gpio zero, when you actually run it from one side and the

03:09.970 --> 03:10.480
other side.

03:10.480 --> 03:15.730
But as we will see later in this course, every time that we want to run a program with Gpio, it's

03:15.730 --> 03:17.920
going to be better to do it from the terminal.

03:17.920 --> 03:23.140
So now that you can use the Python shell and execute programs from the terminal, let's create a new

03:23.140 --> 03:25.960
file, let's edit it and let's run it.

03:25.960 --> 03:28.720
And so for example you can create a new file from Thonny.

03:28.720 --> 03:30.550
But you could also create it from the terminal.

03:30.550 --> 03:37.780
So I can do here I'm in my Python program I can do touch test dot pi for example.

03:37.780 --> 03:41.560
And then I could edit it with Nano or with any other program.

03:41.560 --> 03:43.690
But here, for example I can go back to Tony.

03:46.390 --> 03:52.750
And I can do file open and I can find this paste test dot pi.

03:52.780 --> 03:53.170
All right.

03:53.170 --> 03:56.710
So this way we actually don't create the file from Tony but from the terminal.

03:56.740 --> 03:57.970
Then where to edit the file.

03:57.970 --> 04:01.930
Well if you're going to create a Python program with at least a few lines.

04:01.960 --> 04:07.210
Actually Tony is better for this because you have the syntax highlighting, you have the Autocompletion.

04:07.240 --> 04:11.980
The interface is quite easy to use, so you can definitely write your code here.

04:11.980 --> 04:16.840
And then if you want to run it well for quick test, you can definitely run it from Thonny, but you

04:16.840 --> 04:18.790
can also run it directly from the terminal.

04:18.790 --> 04:21.550
So you could write the code here and run it from the terminal.

04:21.580 --> 04:26.560
For example, if you want to run the code later and you don't want to open Thonny when the code is already

04:26.560 --> 04:28.450
written, you can just do this from the terminal.

04:28.480 --> 04:34.150
Or if, let's say you don't have access to the desktop, if you don't have access to VNC or a screen,

04:34.150 --> 04:41.020
and if you only have an SSH access, then from SSH you can go on the terminal and you can execute this

04:41.020 --> 04:43.060
script with the command python3.

04:43.090 --> 04:48.430
Also, if you want to automate your Python program and run it from another program, well you will need

04:48.430 --> 04:50.890
to use the command from the terminal.

04:50.920 --> 04:51.490
All right.

04:51.490 --> 04:56.500
And now to finish this, I'm going to show you a way that you can improve your code.

04:56.500 --> 04:58.780
And this is something also related to the terminal.

04:58.780 --> 05:01.270
So let's write a simple code here.

05:01.270 --> 05:05.590
Print begin okay.

05:05.620 --> 05:10.850
And then print And so nothing special is going to print.

05:11.090 --> 05:13.910
Well, begin in a line and then end in another line.

05:14.000 --> 05:16.850
Now let's say I have a while loop.

05:17.000 --> 05:26.810
Let's say while true and I do print a and well let's not print A too fast.

05:26.810 --> 05:37.040
So I'm going to do import time and then I will do so here time.sleep.

05:37.250 --> 05:40.790
Let's say we print a every 0.5 seconds.

05:40.820 --> 05:41.090
All right.

05:41.120 --> 05:43.280
So you can see if I run it from here.

05:43.970 --> 05:46.910
Well you see begin a a a and then I stop.

05:46.940 --> 05:47.720
That's it.

05:47.750 --> 05:48.710
We don't see the end.

05:48.740 --> 05:50.750
That's kind of what you expected, right?

05:50.780 --> 05:52.790
I can also run it from the terminal.

05:52.820 --> 05:59.600
So Python three test.py begin a a a and then how to stop it?

05:59.630 --> 06:01.760
Well we're going to press control C.

06:01.880 --> 06:02.180
Okay.

06:02.210 --> 06:06.170
So if you want to stop a running script from the terminal you press control C.

06:06.170 --> 06:11.810
And here you can see we have a keyboard Interrupts and the program just ends like this.

06:11.840 --> 06:14.060
Of course, we don't execute the end.

06:14.090 --> 06:15.890
Actually, we could also.

06:15.890 --> 06:23.090
So if I run the script from Tony, we can also click here and press control C you see directly from

06:23.090 --> 06:23.390
the shell.

06:23.390 --> 06:27.380
So you have to click on the shell and then press Ctrl C and you see we have the keyboard interrupt.

06:27.380 --> 06:29.420
So that's something you can also do on Tony.

06:29.450 --> 06:31.100
Now this can be fine.

06:31.100 --> 06:37.970
But if you want to end your scripts in a bit more clean way, or if you want, for example, to be able

06:37.970 --> 06:44.390
to execute the last line or a few more instructions after you decide to exit from the script, then

06:44.390 --> 06:45.110
how can you do that?

06:45.140 --> 06:48.830
Well, you can use a try catch block.

06:48.830 --> 06:50.120
So let me write this.

06:50.120 --> 06:56.180
You will use try like this but a colon and then everything that you want to run here.

06:56.180 --> 07:02.390
For example this while loop you put it in the try like that with an indentation okay.

07:02.420 --> 07:05.360
So you see now this is indented inside the try.

07:05.360 --> 07:07.550
And then you go back to a new line.

07:07.550 --> 07:13.650
You come back here and you write accept because you accept because actually we're going to catch an

07:13.680 --> 07:14.640
exception.

07:14.640 --> 07:17.280
And what is the exception we want to catch?

07:17.280 --> 07:19.740
This is a keyboard interrupt.

07:19.770 --> 07:24.570
So you're going to write keyboard like that interrupt.

07:24.570 --> 07:28.410
And as you see try and accept a recognized keyword.

07:28.440 --> 07:31.680
We also have syntax highlighting for keyboard interrupt.

07:32.100 --> 07:33.450
You put a colon here.

07:33.450 --> 07:39.810
You go back to new line and let's say print uh quotes exception.

07:41.100 --> 07:42.750
So what this is going to do.

07:42.750 --> 07:45.900
Well the try is just going to run this normally.

07:45.900 --> 07:51.570
But then if you press Ctrl C because this is going to be a keyboard interrupt, instead of exiting from

07:51.570 --> 07:57.390
the program you will kind of catch the exception here, catch the control C and then be able to print

07:57.390 --> 07:59.580
that and then finish the program.

07:59.580 --> 08:04.950
I'm going to save with Ctrl s and well let's run it from the terminal again.

08:05.670 --> 08:09.270
Now I press Ctrl C and you see what we have the control C here.

08:09.270 --> 08:11.400
But you see we have code exception.

08:11.400 --> 08:14.010
That's the log here and then end.

08:14.040 --> 08:17.520
So we don't finish the script with an error.

08:17.550 --> 08:17.970
Okay.

08:18.000 --> 08:20.670
And we can also execute the last line of the program.

08:20.670 --> 08:22.140
And one more thing here.

08:22.140 --> 08:24.510
Well, we could decide to do nothing.

08:24.540 --> 08:30.960
Actually, when we catch this exception, we could decide to do nothing and just print n.

08:30.960 --> 08:33.540
But if we run that, we're going to get an error.

08:33.600 --> 08:39.690
Okay, that's the indentation error because, well, you cannot have a block of code that contains nothing.

08:39.690 --> 08:41.580
So what if you want to actually do nothing.

08:41.580 --> 08:42.660
Well you have a keyword.

08:42.660 --> 08:43.770
It's called pass.

08:43.770 --> 08:48.990
So this way you say, well pass is just I don't want to execute anything, but because I have to write

08:48.990 --> 08:51.900
something then I just write pass, I'm going to save.

08:51.960 --> 08:58.590
And now it's going to work so I can run the program again from what Tony or the terminal and control

08:58.620 --> 09:02.550
C you see we have end and this way it's a bit more clean.

09:02.580 --> 09:02.970
All right.

09:02.970 --> 09:08.160
So in this lesson you have seen how to work with Python from the terminal and also how to improve your

09:08.160 --> 09:14.610
programs so that when you press control C on the terminal you can actually catch this keyboard interrupt.
