2.7. Hello, World!#

Traditionally, Hello World programs are used to illustrate how coding works, as well as to verify that the system is operating correctly. Greeting the user is considered the first step in learning to code, and it even has its own Wikipedia page! So, let’s do that.

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

2.7.1. Syntax, Semantics and Pragmatics of a programming language#

Before starting to code, there are some notions that we need to understand.

First of all, Python is regarded as a programming language. But what does that mean? How is a programming language different from a natural language?

Let’s start with what they have in common: language. A language is a system of communication, a means for individuals belonging to the same group to express themselves and understand each other. A language, in order to be successful in enabling communication and understanding, has to be based on conventions, rules that are agreed upon by the members of the group.

The three main categories we will compare here are: Syntax, Semantics and Pragmatics.

  • Syntax: grammatical structure of sentences. It defines the rules of what is considered to be grammatical in a language. For example, in a natural language such as English, syntax determines the order of the components of a sentence (Subject-Verb-Object). Programming languages (e.g., Python) have a highly non-flexible notion of grammaticality, meaning that a structure is either correctly implemented or it raises an error An example of a very common syntactic error raised in Python is wrong indentation.

  • Semantics: meaning of a sentence. Sometimes, the program we are writing is syntactically correct, but it raises an error for other reasons. So while the computer may understand the commands, carrying them out leads to errors. An example of this is division by zero, combining incompatible types of variables (Python is a weakly-typed language) or calling an undefined function.

  • Pragmatics: defines how context contributes to the meaning of a sentence. Humans are able to infer much information from the context, enabling them to infer the meaning from a set of shared assumptions, even though not all details are explicit. Computer programs are unable to do the same, and can only do what instructed. But this is not necessarily a bad attribute, since it means that there is no ambiguity: a piece of code can have one and only one interpretation, while natural language can often be ambiguous. If a program encounters potentially ambiguous statements, preference rules are employed to select a single interpretation. An example of this is given by mathematical operations.

2.7.1.1. Syntax#

# Example of a correct statement
for unit in ['metre', 'gram', 'second']:
    print(unit)
metre
gram
second
# Example of error
for unit in ['metre', 'gram', 'second']:
print(unit)
  Cell In[3], line 3
    print(unit)
    ^
IndentationError: expected an indented block after 'for' statement on line 2

2.7.1.2. Semantics#

# Division by zero
x = 5
y = 0
x / y
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18948/2436706066.py in <module>
      2 x = 5
      3 y = 0
----> 4 x / y

ZeroDivisionError: division by zero

Although the syntax of the above cell is correct, the code returns an error due to the fact that division by zero is not possible.

# Incompatible type
number = 1
letter = 'a'
number + letter
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18948/425070858.py in <module>
      2 number = 1
      3 letter = 'a'
----> 4 number + letter

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Here the error indicates that you cannot add a number and a letter because the first is cast as an integer (int) and the second as a string (str).

We will learn more about strings in week 2.

# Calling undefined function
['metre', 'gram', 'second'].my_function()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18948/1423563681.py in <module>
      1 # Calling undefined function
----> 2 ['metre', 'gram', 'second'].my_function()

AttributeError: 'list' object has no attribute 'my_function'

The error above indicates that my_function() is not defined.

2.7.1.3. Pragmatics#

Before running the following cell, can you guess what operation will be done first?

# Mathematical operations are handled by preference rules 
6 + 7 * 9
69

If you meant to first add and then multiply, you can use parenthesis!

(6 + 7) * 9

2.7.2. “A Foolish Consistency is the Hobgoblin of Little Minds”#

-Ralph Waldo Emerson, Self-Reliance

Programmers are very opinionated people, which means that they will probably have a strong preference for how to define variables, functions and classes, and so on.

Here you can find the Holy Grail of Python formatting style.

If you need a laugh after all this hard work, here is a video from the TV series Silicon Valley regarding Tabs vs Spaces:

from IPython.display import YouTubeVideo
YouTubeVideo('cowtgmZuai0', width=800, height=300)

Although we really like Richard, the PEP-8 guidelines say that “Spaces are the preferred indentation method.”