Lesson A1 – Hello, World!

Hello, World!

We will start this tutorial writing a “Hello, World!” program, i.e a program that outputs “Hello, World!” on the monitor.

Info: The “Hello, World!” program is a little tradition in starting to learn a programming language. You can find examples of its implementation in all kinds of languages here.

In Python, we can do this with the following line. We open a Jupyter notebook session and execute this statement in a code cell:

[1]:
print("Hello, World!")
Hello, World!

Although this is fairly simple, it includes already a lot of the elements, we need to understand to learn programming in Python. First of all, let’s consider the expression "Hello, World!" in double quotes itself. By putting a sequence of alpha-numerical characters in quotes, Python recognises this as a string object. We will learn more on strings later. Second, we have used the print directive. This is considered a function object. Functions are used to do specific things. While we will look at functions in Python in more detail later, note only so much now: A function can be called, that is told to execute its purpose, by using parentheses: print(). This function takes a string, "Hello, World!", as an argument. As a consequence of our function call, the string passed to the function, is displayed on the screen.

Note: Python knows different objects. The text "Hello, World!" is a string object. The command print is a function object.

In a Jupyter notebook (or any other interactive Python session), you can also just type in "Hello, World!" without using the print function.

[2]:
"Hello, World!"
[2]:
'Hello, World!'

Advanced: If you wonder, why the result looks different, this is because a fundamentally different thing happens here. Simply using an object as input, will give you the largely unmodified object back as output. Using the print function on the other hand, processes its argument, prints it to the screen, and returns an empty object (None) as output.

Comments

Now that you have written your first program in Python, let’s take a moment to discuss one of the most important topics for writing code, which is documenting and commenting what you write. Comments are parts of your code that are ignored by the interpreter and are not executed. One way to do this, is to put a number sign (#) in your code, after which everything is regarded a comment.

[3]:
print("Hello, World!")  # Comment in-line
Hello, World!
[4]:
print("Hello, World!")
# Comment after the commented statement
Hello, World!

Alternatively you can introduce comments to your code as strings. By enclosing the text of a comment between three quotation marks at the beginning and the end, you can use multi-line strings as comments. It is considered good practice to use a comment like this at the very beginning of programs or program parts you write its usage. Others who will read your code and especially your future self will be grateful to you.

[5]:
"""This is a little program that prints "Hello, World!" to the screen.

A comment like this can span over multiple lines.
Everything inside the three quotes will be ignored.""";

print("Hello, World!")
Hello, World!

Note: In an interactive Python session, typing in a bare string like this will give you the string back as output. To surpress this you can use a semicolon (;) after the string.

Arguably, the above line of code is quite self-explanatory. Always try to make your code as comprehensible as possible by itself and avoid redundant comments. For instance, don’t do this (except for making a point, of course):

[6]:
print("Hello, World!")  # prints: "Hello, World!"
Hello, World!

Errors

One of the typical things you will encounter while programming, is that your programs are not doing what you intended or what you have might expected. You are likely to produce errors when running your code. Errors are a very good thing. The worst case scenario that can happen, is that your program does something unexpected and does not throw an error at you. Errors are your friends, because they tell you what has gone wrong. Here, we will provoke an error by calling something Python does not know (more on defining objects and making Python know things in the next exercise).

[7]:
print(x)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-fc17d851ef81> in <module>
----> 1 print(x)

NameError: name 'x' is not defined

Error messages can be quite confusing and long, depending on the situation. For a start, you might want to look at the very end of the printed message. In the last line of the error message, Python will tell you the type of error that has been raised. The NameError means, that Python does not know an object named ‘x’, yet. It simply does not exist. After the error type, you will get a short explanation of the error.

Note: For different kinds of situations, Python raises different kinds of errors.

To resolve the error in your code, you may want to read the message from the top. What is printed there is called a Traceback, which can be considered a special log of the most recent events in your program. An arrow (---->) pointing to a line number (1) indicates the position in your code where the error occurred. We will learn more on how to handle errors later. Here is just another type of error, that is very common.

[8]:
print(x
  File "<ipython-input-8-688950385000>", line 1
    print(x
           ^
SyntaxError: unexpected EOF while parsing

A SyntaxError occurs if you have violated Python’s grammar in a way that the interpreter does not understand what you mean. Often it is just that parenthesis have been forgotten or misplaced. Luckily those are easy to solve most of the time.