Hello World - Python Programming for Beginners (2015)

Python Programming for Beginners (2015)

Chapter Two: Hello World

In this chapter, you will learn:

· How to add comments to your code

· To write and run your first Python Program

The first program that you ever write when you are learning a new language is “Hello World”. We're not going to break that age-old programming book tradition here, so get ready to learn how to program Hello World in Python.

Adding Comments to Your Code

The first thing we are going to learn is how to comment out lines of text you type in your programming. When you comment out text you tell the interpreter to ignore whatever you type on the line.

“Why would you want to do that?” I hear you all ask.

For non-programmers, the idea of commenting out lines of text doesn't really make much sense, but it is vital that you get into the habit of commenting out text from the beginning. Years ago I developed a huge program without including any comments whatsoever and after 2 full weeks of development I found a bug in some of the early code I wrote. Can you imagine how difficult it was to go back through all of the code and find the one line which was causing the bug.

If you said “nearly impossible” you would be absolutely right.

Your comments tell you what the following piece of code is going to do, which makes it easier when you need to edit things or you need to find a bug.

In Python you comment out a line by typing a # at the beginning of the line. For the current program you are writing you would want to type:

#Displays the words “Hello World” on the screen

If you have more than one line you want to comment out then you can do it by typing three single quotation marks above the first and below the last line. You would do that in the current program in the following way:

'''

Displays the words

“Hello World”

On the screen

'''

Simple, right? Now let's write our program!

Writing Hello World

Hello World is the most simple program you will ever write, but it is also the first program you will ever write in Python (or ever, if you have never programmed before). So get excited!

All you need to do is type the following words into the editor:

#Displays the words “Hello World” on the screen
print (“Hello World”)

And your program is ready!

Now you need to click File > Save As and save your file as helloworld.py . Do not forget to add the .py extension! It will not do it automatically for you and if you don't add it then the file will not run.

You can then run the program by pressing F5 or clicking Run > Run Module. You will then see the words “Hello World” displayed on the Python Shell.

And We're Done...

And there we have it. You have now officially wrote your first Python program, give yourself a pat on the back.

But great programs aren't created by simply telling your computer to display words on the screen (unfortunately) you will also need to tell your program to hold data and process data. In the next chapter we will learn how to do that.