Building "Hello World" In Python - Learning Python

Now that we have Python installed and our tools setup, it’s time to start writing some actual Python code. But before we fire up our text editor and start diving in.



We’re going to use Python’s interpreted mode in the command line interface to see how easy it is to write and run Python.



So I started up my terminal program and you can do the same on your computer. Now I’m going to type the command python3 and just hit return. And remember depending on your installation, the command to invoke Python on your system might be a little different.

Execute Python



You might just need to type the word Python or Python 3.6 or some such. Refer to the docs for your installation on your system for the right way to execute Python. So this places me into Python’s interpreted mode and you can see right here is the version number. I’m running 3.6.3 and along with some help text. So I’m running the Python interpreter in command line mode so now I can just simply type some Python and it will run. So I’ll just type two plus two and you can see the answer comes back four. So Python, like JavaScript, is an interpreted language.

In other words, it’s not like Java or C or C++ or Objective-C. The application does not need to be compiled and built into an executable before you can run it. The Python interpreter simply takes each line of code as it comes across it and interprets and executes it. And in this case, it’s simply taking the numerical expression two plus two, evaluating it, and you can see it comes back with four. So let’s try something else so I’ll type in this line of code. Let me type print hello world and hit enter and now you can see I’ve executed this simple Python statement print which is a built-in function along with the string hello world.

Python Code

Now, this is not how you would normally write Python code. I mean, obviously real Python 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 simple things. All right, so now let’s type the word exit or you can use quit as well, but I’ll use exit, followed by an open and close print. So we’re actually calling the exit function and that will exit us back out of the Python interpreter and into the command line. Now that we’ve seen how to write and execute some basic Python statements, let’s start up Visual Studio Code and write our first Python program.




Visual Studio Code

So here we are in Visual Studio Code and I’ve closed the start page and over here in my project, I’m going to open up in chapter two the exercise file helloworld_start.py. So let’s start off by typing the word print and you can see that as I’m using VS Code, I’m getting statement completion right as I’m typing which is nice so I’ll just do that and then I’ll open parens and type in hello world and I’ll save.

Now, if you’re not using Visual Studio Code, then to run this app, you’ll need to start up a terminal window and go into the directory for the example file so let’s do that. So I’ll go into my exercise files folder and that’s in chapter two and then I’ll type the command python3 helloworld_start.py and you can see that the program runs and prints the output. If you’re using VS Code with the Python extension however, then you can run the app directly within the editor so let’s go back to VS Code and click on the debug icon right here. Then I’ll click on the debug console window to show the output if it’s not already visible.

Changing in Code

So let’s change a couple of things. So I’m going to add some code here so I’ll write def main and then I’ll indent the print line. Now, I don’t expect you to understand all of this right away. We will be going into this more deeply as the course goes on. But what I’m doing here is defining a function so line five defines a function called main and that function has only one statement. It just prints hello world. So I’m going to save this and I’m going to run it by clicking the little green arrow.

Setting for Vs Code

Now, if you get an error when you do this, you’re going to have to open the launch settings for VS Code by clicking the little gear icon right here. And again remember, make sure that this line, the CWD line is empty, all right, so let’s go back to the code. So the debugging toolbar appears and I’ll click on the little green run button and you can see that nothing happens. The reason nothing happens is because nothing is calling the main function. I’ve defined the function, but I haven’t called it from anywhere. I need to write some code that actually executes function main.

And before I do that, the other thing I want to point out is that Python syntactically is a little bit different than other languages you might be used to. For example in JavaScript, if I were to write something like function f and then I’d have these curly braces that define the function body. I can put some code in here and I can indent these however I want. It’s not going to matter to JavaScript because the white space isn’t important. Python doesn’t work like that. Python doesn’t have this notion of the scope block delimiters like these curly braces.

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 this indented by two spaces. That indicates to the Python interpreter that line six belongs to the function main and this little colon character right here is what starts that scope block. So let me get rid of this, all right. So now I’ll add some code to execute the main function. I’ll write if __name__ == main__ : and then I’ll just simply call the function main.

So what’s happening here is this line of code on eight is checking to see that when this module, when this Python code file is loaded and the Python interpreter has assigned the __name property the value of main, that means this Python program was executed as a main program. It was started up from the command line or invoked from the Python executable and therefore this function right here main should be called. I might have chosen to take this Python file and simply include it in another Python application and in that case. I don’t want the main function to be called.

Separate Library code

What I’m probably doing in that case is including this file as if it was a separate library of code. And if I had a whole bunch of Python code in here that provided some functionality. I wouldn’t want those functions to all just get executed. So lines eight and nine help distinguish between when a Python file is being included in another program or when that Python code is being executed as itself. So in our case, the file is being executed as a program and line eight will evaluate to true and the main function will be called. If I save this and I’ll run it again and now you can see that the main function is being executed. We’re getting the hello world output.




Hamza Arif
Follow us

Hamza Arif

Hey, i hamza arif student of telecommunication from BZU, i am good in Networking, Telecommunication and Web Development working on different projects and try my best to teach them to all of you.
Hamza Arif
Follow us

Leave a comment

Your email address will not be published. Required fields are marked *