And let's now write our first Python program, this
time using a text editor and not directly inside
the Python Shell. So you have just installed PyCharm,
and I'm going to just explain to you very
quickly the different sections and what you need
to get started. This is very little, okay, we are
not going to use all the options of PyCharm, the
goal is not here to make you a PyCharm expert. The
goal is to learn Python. So let's keep that in
mind. And let's keep things simple. So, on the PyCharm
IDE here, the main section is of course,
here, the Python text editor. So this is a text
editor, you can write any text you want. We have
an example here, okay. And you can see, you can
zoom in and zoom out, okay, with ctrl, or command,
and the scroll of your mouse, on the left, you
have a navigation tree, where you can see your
different files, or you can navigate between the
different files of your project. So for now, we
just have one file, which is named main.py, and as
you can see, we have the .py extension, that is
going to be the extension for all your Python
files. So here, you can toggle that if you want,
okay, you can, but is shorter. Then on the top here,
you can choose which file you're going to execute.
And with the play button, you can execute that
file, okay, you can execute the code that is in
that program. Okay, we are going to see that just
a bit later. And then one important section you
have on the bottom. You can see here Python
Console. If you open this, you have a Python
Shell. So you have a Python Shell directly inside
your Python IDE. So this is going to be very
convenient. If you just want to test one line of
code, one small instruction, just open the Python
Shell here with Python Console, write the
instruction, test your thing and then come back to
the text editor.
Alright, and now let's write our first real Python
program. I'm going to remove all of that, okay.
This is automatically generated for you, but we're
not going to use it. So back to our file, and now
this file is empty, as you can see here. So you
have the lines also on the left, which is very
convenient. And so you can write many lines of
code if you want. And let's start with the very
basic Hello world. So the same thing I did before
in the Python Shell, I'm just using Hello world
here, the string Hello world. Okay, and to run
that program, so to run the code inside, I'm going
to click here. On the play button, and this is going
to open a new tab here, tab named Run, and you can
see, so the first line will be here you can see
this is the Python executable. And this is the
main.py. So this is basically the command that is
executed to run that file. And then you have the
output of your file, then you will have also at
the end, Process finished with exit code, and a
number. So zero means success. If you have
anything other than zero, then you have an error
in your code, and you will also see the error
here. So now, well, we have Hello world. But why
don't we see Hello world here. I'm going to open
the Python Shell again do hello world, that is, and
you can see hello world is printed here. So that's
the big difference between the Python Shell and
when you execute a Python program here from Python
file. In the Python Shell, everything you write will
be evaluated and printed. In a Python program, well,
this is not the case. If you want to print
something, you actually have to explicitly say
that you want to print that thing. And to do that
it is very simple, you use the print function. So
print parenthesis and then the string you want.
Okay, I'm going to make it a bit bigger. So print
Hello world, and now I'm going to run it again and
you can see Hello world. So I can go to a newline
so we're going to write each, so let's write Test,
we are going to write each instruction in a new
line okay. So now if I run this, you can see Hello
world, Test. So as you can see, and this is very
basic, each line is going to be executed one by
one. So first the line one and then the line two,
etc. So the order is very important. Okay. This
line is going to be executed before that one. Now
one nice functionality you get with the IDE is the
auto completion. So what is the auto completion?
So when you want to write print, for example,
you can see here, on the bottom, we have some
help, okay. You have some suggestions of what you
could want to do, okay. And you can see here we
have print. So the first suggestion, if you wants
to write this, you can simply here, press on tab,
and this is going to auto complete the function,
whatever you need to write, okay. So this is the
auto completion, and you're going to use that a
lot. Alright, and now what I'm going to do, I'm
going to simply remove that, and I'm going to
rewrite one of the exercise we had to do in the
last section. So we simply asked the user for the
name, for the age, and then we'll print a nice
string to say, for example, Hello, Bob, you are
25. So now instead of doing that in the Python
Shell and write each instruction one by one, I'm
going to write all the instructions in that file,
okay with the text editor, and then run the Python
program. Okay, so user_name is equal to input,
let's say, What is your name? And then the user_age is
equal to input("How old are you?"). Okay, so I put
each instruction in a separate line, and then I
can do print. So let's say I want to say Hello,
and then I'm going to put plus user_name,
plus comma you are, and then plus user_age. As you
can see, I also have the auto completion for the
variables that I have created before. So I can
just press tab here, and I have user_age.
Alright, one thing to note here, if you don't have
to write on every line, okay. Here, I just kind of
group those two instructions together again, then
I put a new space, and that instruction. This is
just for clarity. Okay, so it is easier to read.
So now, I'm going to press here on Run. And you
can see What is your name? So now I need to say,
say, Bob. How old are you? 25. Hello Bob, you are
25. Okay, so now as you can see, this is better
than the Python Shell, okay, because here, we will
be able to easily modify the program, okay. We can
add new line, modify existing line, etc. And we
can just save that and run this. Okay, if I want,
I can run it again. What is the name? John. How
old are you? 29. Hello John, you are 29. Okay. So
you don't need to write the program again to
execute it again. Okay, and what you can do to
save the files. Normally, this is going to save
automatically, you can just click here, Save All
with or press Ctrl S. And if you want to reopen
that project, so let's say you go on File, Closed
Project, you can just open PyCharm, and it's
going to open the project for you, or you can go
on the project and just click on the project you
have, and this is going to come back to your
current project. Okay, if you click here, you can
also see the exact path where your project is
saved. Okay, so in Users, so you username, and
then PyCharm Projects, my_first_project and then
you have another folder here and the main.py. So
that is where you can find your file. Alright, and
one last thing I want to show you is, let's say you
have an error. So let's say forget this, you can
see if we have an error, then you're going to see
something in red. And we did it there, we have an SyntaxError,
EOL means an offline and well this is
just it's going to show you where you have the
error and an explanation that can be explicit or
not depends. And you will see here exit code
number one. Also you are going to see a red
underline here, so you can easily spot the errors
and once you correct the error, that's going to work
as expected. Okay, great. Now you know how to
write a Python program in a text editor, how to
save it, how to run it, and also how to use the
auto completion and a few features of a Python IDE.