Let's start with a very simple Python program.
First of all, well, where can you write and execute Python programs?
Here we will use a software that is already installed for you on the Raspberry Pi OS.
And this software is called Thonny.
So to find it you can just go under menu here and click on programming and you will find Thonny here.
So you just click on this and this is going to open Thonny here which is a Python IDE.
And IDE here means integrated development environment.
So it has everything for you to develop with Python.
And the first thing we are going to do here, this is a simplified mode that we have.
We are going to switch to regular mode.
So you click on this here you click on okay.
And now you need to restart it.
So let's restart programming and Thonny.
Okay.
Now we have the normal mode and you can keep it like that, but I'm going to increase the font size.
Okay.
Here you have this.
Or you have the shortcut so that you can actually see what I'm doing on my screen.
Okay.
So I use a bigger font size.
So this tool is a great tool where you can write Python code.
You can save Python programs into files.
And you can also run and debug your programs.
It is very handy because setting up Python with the environment can be quite tricky here.
Nothing to do, just open up thonny and you're good to go.
So we are first going to look at the bottom part here named shell.
Just if you ever close shell by mistake or if it's not here, you can simply open it again.
You just go on view and then shell okay.
And it's back here.
This shell part is an already configured Python environment where you can simply run any Python command
you want and it will be executed right away.
Let's go ahead and do a basic Hello World program.
So what we want to do here is simply to print some text.
You can use the already existing print function.
You can see syntax highlighting here okay.
And you need to open and close parentheses.
So print open close parentheses.
And inside we're going to use double quotes.
And inside those double quotes let's say hello Raspberry Pi okay so that's our Hello World program.
You can press enter.
And you see we have the result of that comment okay.
So we have run a Python command.
And the result of that command is we see hello Raspberry Pi okay.
We simply asked Python to print this piece of text.
So the Python interpreter.
That's how Python works is going to use a Python interpreter to execute each command one by one.
So here we just have one for now.
So it's going to execute this command and print the result or do whatever you ask it to do.
Okay.
Now we are back here.
You can use the up arrow if you want to go back to the previous command.
So you can use the up arrow and just press enter again.
And you can see that we have Hello Raspberry Pi again.
Now you could create entire programs like that on the shell.
But as soon as you close the shell, your program will be gone and it's not going to be very practical.
So what we're going to do from now on is to write the Python programs into a file, okay?
So you can save them, you can reopen them and also run those programs.
And to do that, well, you simply are going to use this text editor right here.
Okay.
So this is the shell but this is a text editor.
So I'm going to write here same thing I'm going to write the print function.
I'm going to open and close the parentheses and then put double quotes.
So one and two.
And inside this I'm going to say hello.
And I'm going to change to RPI.
Now You can know that when I press enter here.
Well, it doesn't do anything.
It actually goes to a new line.
You can see I have one and then I have two.
If I press enter again I go to a new line.
So this is simply what you would expect from a text editor.
So how to actually run this.
Well you have this play button here run current script.
So we're going to run that.
And you can see it's going to run here.
And you see hello RPI.
And if you want to run it again you just click here again.
Now you can save this file.
So as for now you can see it's untitled and not saved.
So to save it you just go on file save or save as okay.
And when you save it's going to open this.
And what it looks like with my configuration it's going a bit it's a bit oversized but it's okay I still
have space.
I'm just gonna well I'm going to go to documents here so you could save the file any way you want.
Okay.
Here we are in the, uh.
We're going to see this a bit more later.
We are in the home directory for your user Pi, but we're going to go to documents.
Okay.
And in documents you may create a new a new folder.
Let's name it Python underscore programs like this.
And you can click on create okay.
So we just save inside a folder.
It doesn't really matter which one.
And for the name I'm just going to name it hello Dot p y.
So the name of the file is hello.
And then I have the p y extension okay.
Pi extension is very important.
That's the extension for any Python program.
Then I can press enter and it is saved.
So then I can go to file um open and I can find the file again I click on escape here.
Great.
You can now create a Python file.
You can save it.
You can load it and you can run it.
And what we've seen is how to print something on the screen here.
Now, in this program where we only print one thing, but you could also add another print.
Okay.
For example test.
Okay.
So you write print, you open parentheses and then you write one double quotes.
And then you close the double quotes and you close the parentheses.
Let's play that.
And you can see we have hello API in test.
So we have the two pieces of text that are printed one after the other.
And you can see here that the order is very important.
Okay.
The program is going to be executed from line one and line two, line three etc..
Or you can see here I have a red dot.
If you click I'm just going to click again and remove it.
So the order for the code is very important.
And we're going to come back to this quite often.
You can also add as you can see you can add empty lines okay.
If you add empty lines it's simply not going to do anything.
You see the result is the same.
Okay.
So an empty line is not going to be executed.
So here we execute line one.
And then line two and three is nothing.
Then we execute line four and then five and six.
There is nothing.
One other functionality I want to show you right now is how to comment one line.
So let's use the hash sign here.
And you can see now it turns gray.
When you use the hash sign it's gonna comment the line, which means that this is not going to be executed
okay.
It's still here on the screen, but it's not going to be executed if I run again.
You can see we only have hello API.
Okay.
And this one is not executed.
Now, if I remove this and I run again, it is executed again.
Okay.
So you can add a comment in order to tell Python to ignore some lines of code.
And this is very useful.
You can use commands a lot with Python because for example, you have some code here and you want to
just ignore one part so that you can test your program without one part and then put it again.
Then you don't need to remove the code entirely.
You just put comment, you test the code and then you can remove the comment if needed.
Okay.
And you can also use comments not just for that, but just for explaining stuff in your code.
So for example the next line will print text.
Okay, so here it's pretty obvious, but sometimes you might want to explain a bit more what some parts
of the code are doing, or to give some more information about why you did something in that way.
Okay, so you can use some comments in this case.
And finally, to finish with this lesson, well here we have written everything perfectly.
Okay, so it works.
But let's say you forget a parenthesis here.
Well, first you might see some weird thing here.
Okay, you can see it's all gray.
But let's say I run the script, you can see that we're going to get an error because here you can see
in this error we have.
So the name of the file, which is this one?
The number for the line.
Okay, here it's line one.
And then you see we have an error.
So sometimes this is going to be explicit sometimes not okay.
So sometimes it's easy to find the error.
But it's not always obvious here.
It's pretty obvious.
You see we have a syntax error.
The parentheses was never closed.
So we know we have to close that parenthesis here.
But let's say I forgot to close the quote.
So I'm going to run again.
And you see we have a different error here.
So still on line one.
Still a syntax error.
But here you can see Unterminated string literal okay.
So we need to finish with a quote here.
Now let's say for example instead of print I just write print okay I forgot the T.
If I run we have a different error here.
And now you can see.
So this was executed on line one.
But then on line four here you can see in line four we have an error because the name print is not defined.
Okay.
We need to write print so you can see when you do a mistake here you will get different kinds of errors.
Okay.
When you run the program.
And that can help you to fix the mistakes in your code.
All right.
Now if I run again.
All good.
Great.
So with this, you've already seen a lot.
And this is a really good first step.
Now let's continue and discover variables.
And just a quick parenthesis.
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
okay.
Press escape.
And what I found is well, it's a bit tricky, but if you quit here, the full screen, I mean, that's
something that works for me if I go smaller.
If I go smaller and I open again.
And then I go to file save as.
maybe I can guess.
So somehow if you put a smaller, if you put the screen very small, you open Sony again, then you
click on save as it's going to be too big and then you increase the size of the screen.
Then you can get to the edges of that and that's what you want to do.
You can reduce you can reduce this okay.
So what I still it's still out but I can reduce it like this.
And let's just click on cancel here.
And now if I go to File Save as hopefully the size is the same as the one you have defined before okay.
So I'm going to go back full screen F8 full screen I'm going to open Sony again okay.
But whatever size I want I can put it full screen if I want to.
And let's go to File Save as okay.
And now I have a normal size window and the problem is solved.