Hey welcome to this new lecture and in this lecture I'm going to show you how
to create and execute a Python program on a Mac computer, so if you are a
Windows user please skip this lecture. And so in the previous lecture I showed
you how to install Python 3 and also how to launch a Python 3 interactive
Session, so which is good for executing and testing out code so like that line.
Like my line, but if you quit the terminal you're going to lose that code, so you
want to find out a way to save that code in a file so that you can develop your
program step by step, so you can go back to your code, add the code, modify it, and
most importantly you can execute multiple lines at once instead of
executing line by line interactively. So let's go ahead and do that.
Normally you need to figure out, to create a directory where you want to
save your program, so I'm going to create this folder in here and this is empty
for now, so I'm going to create a file there. There may be different ways
of creating a Python file all but I prefer to do it via the terminal.
So currently you do PWD, that will print out the current directory of the terminal,
so the terminal is an app and it is opened in the current directory, in a certain
directory which is this on for the moment. If you do ls you will see the
directories and files or the current Directory, so users admin has this
directors, has these folders so if you want to go to the desktop folder,
you'd want to do desktop or you can use the Tab for auto
Completion, so the tab key and then Un Tab. That will take you to the
untitled folder. Execute and I'll see that your current folder is the untitled
folder and that means you can go ahead and create a file now. Let's say touch my
program dot py, so you can give it any name that you want. Execute and that
will create an empty file which for now you can go ahead and edit it with text
edit for example. Later on we're going to install Atom editor which is much
more advanced than this simple editor but for now this should be enough to
just get you started to get you the idea on how to execute a Python program
so let create code for the program. This program will simply
print out the hello text so you need, you use this print keyword and then you use
double quotes and then the text hello and then another double quote to close the
text and then a closing bracket.
CTRL S to save, exit text edit and then go back to the terminal and what you do
here now to execute your program, the program that you just created, you use the
Python 3 command and then my, you can type my program dot py or you can use
tab to autocomplete the name, enter to execute and you get the output of the program.
Print it out in the terminal so in this case the output is hello and yeah that's how
you create and execute a program on Mac and similarly you can go ahead and
create other Python programs in here just as we did with my program dot py. I can also
show you a quick way to actually open the terminal in this current directory
without having to do CD and try the path name, so let's do that.
You go here, system preferences, so this is a one time configuration. Go to keyboard.
Shortcuts and then in the shortcuts tab you go to services, scroll down and see
you probably have this option unchecked, so you want to make sure you
have checked the new terminal at folder option just like I have it here, so make
sure make sure that is checked. Close this and then what you do is to open the
terminal in a current folder, in a specific folder you right click on
folder and then go to New Terminal at Folder and now what you do is Python 3
Myprogram.py, execute and you get the Output, so that's a quick way to open
the terminal in a specific folder and yeah that's about this lecture.
I hope everything went smoothly and I'll talk to you later. Thanks.