Hi. Welcome back.
In this video, I'll show you how to build
a Python program and how to execute that Python
program so that you can get the output.
Obviously, this program will be very simple because
the focus here is to show you the
process of creating a program and executing it.
To build a program in Python, you need two things.
One is Python and the other is an IDE or a text editor.
An IDE is just an advanced text editor.
So I'll use Visual Studio Code as the
IDE, which is this one in here.
And then I will create a project here.
A project is nothing more than
just creating an empty directory.
So first you want to go to your
file system, Windows Explorer or Mac Finder and
make sure you have an empty folder.
So First Project is the name of
this empty directory on Windows Explorer.
And then you want to go back to Visual Studio Code.
If you are on another IDE,
the process is basically the same.
You go to File and then you go to Open Folder. On Mac,
it could be written just Open.
So click on that, and then you want to
browse to that empty folder, double click it, and
then select folder, and Visual Studio code,
what it will do is, it will open that
folder as the root directory of the project.
You have this warning here.
It's fine to say, yes, I trust the authors.
You can see now that FIRST PROJECT, the name of
the folder is the name of the project as well.
So what I'll do now is
write a program, a Python program,
and this consists of creating a text
file where you write that actual program.
To create that text file, you can go
to your folder again and create it here.
Or normally what we do is we
create that file from within the IDE.
So if you click here, then write a
name for the file, such as my_program.
It's not recommended to use spaces in
the names all over the files.
So please always use underscores instead of a space.
And then let's dot, try txt, and see if this will work.
The actual extension is .py, but
that is not a requirement.
Of course we will use .py always, always.
But for now, let's keep it at .txt so
that you understand a very crucial concept about Python.
So I'll press Enter, and here I
will do print(3+4).
And that's our program.
So this program simply outputs the
sum of 3 and 4.
How can I execute this program now to get the results?
Well, you see this Terminal menu item here?
You press that and go to New Terminal.
And that should open a new terminal in here.
So that's like the command line
where you interact with your computer.
And here, now we can write py -3 if you are on Windows.
If you are on Mac, you should write python3.
So I'm on Windows here, so I'll use
py -3, and then my_program, actually I can use the tab.
Now, if I press the tab,
that will autocomplete the file name.
This part here is not necessary.
The dot and the backslash, you can leave
it, it's fine, but even if you remove
it like that, it will still work.
So now if you press Enter here, what we
get is we go to the next line.
Perhaps you were waiting for the
output to show in here.
It didn't show up because the file is still empty.
We haven't saved the changes.
So after I wrote this line
of code, I didn't save anything.
So what you want to do is you want
to go to File, Save or Control s and
then press the upper arrow key in here to
get that previously executed command and press Enter.
And then we get the output 7.
So as you can see, a Python program is nothing
more, but just a text file that you write,
[No audio]
and then you execute it using the Python command.
But there is a nicer way to execute
Python programs, at least on Visual Studio code,
and that is by using a run button.
Currently there is no run button here because
we need to install a Python extension.
So if you go over to this icon here, extensions, and
here you want to search for python, press Enter,
[No audio]
and this first result is the extension we want to install.
So press on the Install button,
[No audio]
and that will install the Python extension.
But what is the Python extension?
Well, I told you that this will
show the Run button somewhere in here.
It didn't show it because my
file doesn't have a .py extension.
So the Python extension searches for .py files.
So what you want to do is right click here,
rename to .py and now we see that button.
[No audio]
We also get this message that we
need to select a Python interpreter.
So to select a Python interpreter, you either press that button
there or if you didn't get that window, what you want
to do is, you want to go to this icon here,
this curly bracket, and then go to Select Interpreter.
And then you want to select the python 3.10.3
interpreter, which we installed in the previous video.
Perhaps in your case it's another version.
So whatever Python interpreter you're using, select
it, and then you're ready to run
this program using that button.
So press that button
[No audio]
and that's the output 7 down here.
So as you can see, there are
two ways to run your Python programs.
Either you use the python command, as we did
with py -3, and then you mention the
name of the file, or you use the easier
way, which is by using this button.
But as you so to be able to use that button,
you need to name your files with .py at the end.
And that's actually the best practice.
Everyone names their files .py.
It's like a convention.
It's not a requirement, but it is a hard convention.
And as you saw, things like extensions,
IDE extensions, also will work better if
you have your files named with .py.
The other advantage of using this button is that
if you had more code, such as 4 plus
9, you don't have to save the code, you
just run it and you'll get the result.
7 and 13.
So the sum of that line, and
the sum of that line as well.
If you want to create another program, what you want
to do is you want to go to File again,
Open Folder and make sure you have an empty folder,
or just create one on the fly here.
So Folder, Second Project,
[No audio]
double click it, select folder,
and that will close that current window, current project,
and it will open the SECOND PROJECT.
So this one in here.
Again, you create a file.
my_program.py, using .py always, select an
interpreter using this curly brackets and go there.
Choose always the same interpreter that you used
previously, unless you want to select another interpreter,
if you have many interpreters and
[No audio]
you write some code, run it and you should see the output
here on the command line. With that,
I thank you for watching this video, and
I'll talk to you in the next one.