In this lesson you will learn how to work with Python inside the terminal.
We will start our Python programs directly from the terminal.
We will also improve them.
So let's start actually with the Python shell.
This is something that we have seen at the very beginning of the course.
So if I come back to thonny, while the shell is what you have on the bottom right, and if you remember
we did something like print hello.
Okay.
You can run commands right here from the shell.
And so this is something you can do in Thonny, but you can also do that directly from the terminal.
How to do this you just type Python three.
You press enter and you are in a Python shell.
So I can do print.
Hello for example.
Well, I can do whatever I want.
I can create a variable, I can print this variable.
I can just run any Python comment that I want.
And note here that if I press Ctrl C, you see I have a keyboard interrupt.
We can come back to this at the end of this video.
So we have a keyboard interrupt, but we are still in the shell.
How to exit from the shell.
We have the exit function with parentheses and then we are back to the terminal.
All right.
So if you want to use the shell you can see you just type Python three.
Then you just know that you have to also write exit with parentheses to exit.
And you can use the Python shell as you want.
Now you can also run a Python program directly from the terminal.
That's going to be super useful.
Let's go.
So with CD to documents and Python programs LZ what do we have here.
So for example we have the Hello.py.
So what we have done so far is for example we go on file, we open.
Uh let's go back to this.
Hello.
Well you can just open any program okay.
It doesn't matter here.
You see that we can click on run.
That's what we've done here.
And so here well it's going to create a variable and then print the same variable three times.
So we have 12 1212 as a result.
So let's go back to the terminal and how to run this in the terminal.
Well, you just use Python three, just like we did before.
But then instead of just running Python three, you do space and you provide the file name.
And this file name must be a Python file with the Python extension.
Okay.
So for example hello.
And I can press tab for autocompletion okay.
Hello.py.
And you see we execute this from the terminal okay.
So that's going to be very useful.
And before we go further just a quick parenthesis if you run.
So sometimes might happen if you run a program that uses the Gpio library.
So for example if you use the push button, the LED, the Pi R sensor etc..
So if you use a program that uses the Gpio zero library here and then you try to.
So if you run this on Sony and then you try to run it on the terminal, maybe you're going to get an
error in the terminal.
And if that happens, well what you have to do is to close first Tony okay.
So you close Tony completely and then you run the code on the terminal.
So just in case you have an issue with the Gpio zero, when you actually run it from one side and the
other side.
But as we will see later in this course, every time that we want to run a program with Gpio, it's
going to be better to do it from the terminal.
So now that you can use the Python shell and execute programs from the terminal, let's create a new
file, let's edit it and let's run it.
And so for example you can create a new file from Thonny.
But you could also create it from the terminal.
So I can do here I'm in my Python program I can do touch test dot pi for example.
And then I could edit it with Nano or with any other program.
But here, for example I can go back to Tony.
And I can do file open and I can find this paste test dot pi.
All right.
So this way we actually don't create the file from Tony but from the terminal.
Then where to edit the file.
Well if you're going to create a Python program with at least a few lines.
Actually Tony is better for this because you have the syntax highlighting, you have the Autocompletion.
The interface is quite easy to use, so you can definitely write your code here.
And then if you want to run it well for quick test, you can definitely run it from Thonny, but you
can also run it directly from the terminal.
So you could write the code here and run it from the terminal.
For example, if you want to run the code later and you don't want to open Thonny when the code is already
written, you can just do this from the terminal.
Or if, let's say you don't have access to the desktop, if you don't have access to VNC or a screen,
and if you only have an SSH access, then from SSH you can go on the terminal and you can execute this
script with the command python3.
Also, if you want to automate your Python program and run it from another program, well you will need
to use the command from the terminal.
All right.
And now to finish this, I'm going to show you a way that you can improve your code.
And this is something also related to the terminal.
So let's write a simple code here.
Print begin okay.
And then print And so nothing special is going to print.
Well, begin in a line and then end in another line.
Now let's say I have a while loop.
Let's say while true and I do print a and well let's not print A too fast.
So I'm going to do import time and then I will do so here time.sleep.
Let's say we print a every 0.5 seconds.
All right.
So you can see if I run it from here.
Well you see begin a a a and then I stop.
That's it.
We don't see the end.
That's kind of what you expected, right?
I can also run it from the terminal.
So Python three test.py begin a a a and then how to stop it?
Well we're going to press control C.
Okay.
So if you want to stop a running script from the terminal you press control C.
And here you can see we have a keyboard Interrupts and the program just ends like this.
Of course, we don't execute the end.
Actually, we could also.
So if I run the script from Tony, we can also click here and press control C you see directly from
the shell.
So you have to click on the shell and then press Ctrl C and you see we have the keyboard interrupt.
So that's something you can also do on Tony.
Now this can be fine.
But if you want to end your scripts in a bit more clean way, or if you want, for example, to be able
to execute the last line or a few more instructions after you decide to exit from the script, then
how can you do that?
Well, you can use a try catch block.
So let me write this.
You will use try like this but a colon and then everything that you want to run here.
For example this while loop you put it in the try like that with an indentation okay.
So you see now this is indented inside the try.
And then you go back to a new line.
You come back here and you write accept because you accept because actually we're going to catch an
exception.
And what is the exception we want to catch?
This is a keyboard interrupt.
So you're going to write keyboard like that interrupt.
And as you see try and accept a recognized keyword.
We also have syntax highlighting for keyboard interrupt.
You put a colon here.
You go back to new line and let's say print uh quotes exception.
So what this is going to do.
Well the try is just going to run this normally.
But then if you press Ctrl C because this is going to be a keyboard interrupt, instead of exiting from
the program you will kind of catch the exception here, catch the control C and then be able to print
that and then finish the program.
I'm going to save with Ctrl s and well let's run it from the terminal again.
Now I press Ctrl C and you see what we have the control C here.
But you see we have code exception.
That's the log here and then end.
So we don't finish the script with an error.
Okay.
And we can also execute the last line of the program.
And one more thing here.
Well, we could decide to do nothing.
Actually, when we catch this exception, we could decide to do nothing and just print n.
But if we run that, we're going to get an error.
Okay, that's the indentation error because, well, you cannot have a block of code that contains nothing.
So what if you want to actually do nothing.
Well you have a keyword.
It's called pass.
So this way you say, well pass is just I don't want to execute anything, but because I have to write
something then I just write pass, I'm going to save.
And now it's going to work so I can run the program again from what Tony or the terminal and control
C you see we have end and this way it's a bit more clean.
All right.
So in this lesson you have seen how to work with Python from the terminal and also how to improve your
programs so that when you press control C on the terminal you can actually catch this keyboard interrupt.