From the course: Learning Python

Building Hello World - Python Tutorial

From the course: Learning Python

Building Hello World

- [Instructor] Now that we have Python installed and our tools set up, it's time to start writing some actual Python code. But before we fire up our text editor and just start diving in, we're first going to use Python's interpreted mode in the command line interface to see how easy it is to write and run Python. So here I've started up my terminal program, and you can do the same on your computer, doesn't matter if it's Windows or Mac, and I'm going to type Python by itself and hit return. And remember on the Mac, you're going to have to do Python three, okay? So I'm going to type Python, and when I do this, you can see that this places me into Python's interpreted mode, right? So right here is the version number for Python, and then there's some help text. All right, and then there's this little three angle bracket prompt right here. Now in this mode, I can just simply type some Python code and it will run immediately. So I'm going to go ahead and type 2+2, and you can see that the answer comes back as four. Now, Python is an interpreted language, like some other common ones in use today, for example like JavaScript in the browser. In other words, it's not like Java or C or C++, the application does not need to be compiled and then built into an executable before you can run it. The Python interpreter simply takes each line of code as it comes across it, and then interprets and executes the code. And in this case, it's simply taking the numerical expression, two plus two, evaluating it, and it gets four back. All right, let's try a different line of code. I'm going to write print and then a parentheses and a quotes and type in Hello world and then quotes, and then a closed parentheses, right? I'll hit enter. And now you can see that I've executed these simple Python statement print, which is a built-in Python function, and we'll learn more about functions, along with the string of characters, Hello world. Don't worry about understanding all of this right now, it will become clearer as we go through the course. All right, let's try one more thing. I'm going to write name equals input, and I'm going to type, what is your name, question mark, and then close quote and parentheses. And when I hit enter, you can see that now Python is running the input function, which gives me a chance to enter a value. So I'll type Joe and hit enter. So now I've created a variable and that variable is called name, which contains the value that this input function asked for. So if I just type name by itself, you can see the value that the name variable contains, which is Joe. Now all of this is not how you would normally write and run your real Python programs. I mean, obviously real programs are going to be a lot more complex than this, but you can use the Python interpreter in command line mode to try out some simple things. Okay, now let's type the word exit and with an open and closed parentheses, so we're actually calling the exit function and that will exit us out of the Python interpreter and then back into the terminal command line. Okay, so now we've seen how to write and execute some basic Python statements, let's start up our code editor and write our first Python program. And I'm using Visual Studio Code, but again, you can use whatever editor you like. So here we are in VS Code, and I'm going to open up chapter two and here in chapter two, I'm going to open up the file named helloworld_start.py. So let's start off by printing out a hello world message, right, so I'm going to type print, and you can see that as I'm writing my code, right, visuals here in VS Code, I'm getting statement completion right as I'm typing, and that's because of the Python extension that we installed earlier. So I'm going to write print and then I'm going to write Hello World, and that's pretty much it for that line. And then let's also have the user enter their name. So I'm going to write name equals input. And I'll write, what is your name? And then I'll print, nice to meet you. And then outside the closing quotes, I'm going to put a comma and then name. All right. So let's go ahead and save this. All right, and we can start up a terminal window and go into the directory for the example file and run it like we did before, or if you're using VS Code and you've got the Python extension installed, you can also click this little icon up here, which it says you can see when I hover it, it says, run Python file in terminal, or I can right click in the source code and choose run Python file in terminal. So I'm going to go ahead and do that. And you can see that when I do that, the program is running in VS Code's built in terminal here, right, and you can see that Hello World is being printed, and here's the input for what is your name? So I'll type in Joe and it says, nice to meet you, Joe and then the program exits. So, so far so good. Let's change a couple of things. So I'm going to click on this little trashcan and get rid of this interpreter right here and what I'm going to do is define a function. We'll learn more about functions later in the chapter. So just bear with me for the moment. I'm going to write def, which defines a function, and I'm going to call the function main. And then I give it an open and closed parentheses and then a colon, right? Now, when I do that, you can see that this little red squiggle appears here. That means I'm getting an error. You can see it says expected indented block. So what I'm going to do is indent each of these lines under the function, main. So I'm going to hit the tab each time, okay? And it's important that all of these lines be indented using the same number of spaces. And you can see that I've got one, two, three, four spaces of intending. It doesn't matter if it's two or three or four, it doesn't matter. They all have to be the same. Now, again, I don't expect you to understand all of this right away, we're going to be going to this more deeply as the course goes on, but what I'm doing here is defining a function and that function contains our print and input statements. So I'm grouping all of these statements into one function that I can call elsewhere in my program. The Python syntax is a little bit different than other languages you might be used to. So Python actually uses the indentation level of the lines of code to indicate where that code belongs. So, because under the definition for the main function, I've got each of these lines indented by four spaces that indicates to the Python interpreter that these lines of code belong to the function name. Alright, so now I need to add some code to actually execute the main function. So what I'm going to do is I'm going to write if__name and then double underscore again, is equal to, and then inside quotes, double underscore main, and then double underscore and then a colon. And then once again, I have to indent. I'm going to call main. So why do we need to do this, and what does it do? So unlike in C Sharp or Java or other programming languages, Python does not automatically look for a specific function when the program starts. So what's happening here is this line of code right here, line 11, is checking to see that this module, this file of Python code is loaded and the interpreter has assigned the double underscore name variable, the value of main. That means that this Python program was executed as a main program. It was started from the command line or invoked from the Python executable somehow and therefore this function right here, the main function should be called. Now, the reason I need to do this is because Python code can be used multiple ways. My code in this file can be run as its own program, but I could also build a reusable module that can be included inside of other Python programs. So if I were using this code in another program, I wouldn't want all this code to just start running when it was imported into the other program, because that would cause problems. So lines 11 and 12 helped distinguish between when a Python file is being included in another program, or when that Python code is being executed as its own program. So in this case, the file is being executed as a program and this, if condition right here, will evaluate to true, and then the main function will be called. All right, so once again, let's save and I'm going to go ahead and right click and choose run Python file in the terminal. And you can see that we are getting the output just as we would expect.

Contents