Python
Python
Imagine that you want to know the average speed you've reached during a long
journey. You know the distance, you know the time, you need the speed.
Naturally, the computer will be able to compute this, but the computer is not
aware of such things as distance, speed, or time. Therefore, it is necessary to
instruct the computer to:
Of course, these examples are not formalized, and they are very far from what
the computer can understand, but they are good enough to be translated into a
language the computer can accept.
Computers have their own language, too, called machine language, which is
very rudimentary.
We can say that each language (machine or natural, it doesn't matter) consists of
the following elements:
An alphabet
a set of symbols used to build words of a certain language (e.g., the Latin
alphabet for English, Kanji for Japanese, and so on)
A lexis
(aka a dictionary) a set of words the language offers its users (e.g., the word
"computer" comes from the English language dictionary, while "cmoptrue" doesn't;
the word "chat" is present both in English and French dictionaries, but their
meanings are different)
A syntax
Semantics
a set of rules determining if a certain phrase makes sense (e.g., "I ate a
doughnut" makes sense, but "A doughnut ate me" doesn't)
This is the simplest and most primary set of symbols we can use to give
commands to a computer. It's the computer's mother tongue.
Unfortunately, this mother tongue is a far cry from a human mother tongue.
A particular high-level programming language is designed to fall into one of these two
categories.
There are very few languages that can be both compiled and interpreted. Usually, a
programming language is projected with this factor in its constructors' minds – will it
be compiled or interpreted?
Let's assume once more that you have written a program. Now, it exists as a
computer file: a computer program is actually a piece of text, so the source
code is usually placed in text files.
Note: it has to be pure text, without any decorations like different fonts,
colors, embedded images or other media. Now you have to invoke the
interpreter and let it read your source file.
The interpreter reads the source code in a way that is common in Western
culture: from top to bottom and from left to right. There are some exceptions
- they'll be covered later in the course.
First of all, the interpreter checks if all subsequent lines are correct (using
the four aspects covered earlier).
If the compiler finds an error, it finishes its work immediately. The only result
in this case is an error message.
The interpreter will inform you where the error is located and what caused it.
However, these messages may be misleading, as the interpreter isn't able to
follow your exact intentions, and may detect errors at some distance from
their real causes.
For example, if you try to use an entity of an unknown name, it will cause an
error, but the error will be discovered in the place where it tries to use the
entity, not where the new entity's name was introduced.
In other words, the actual reason is usually located a little earlier in the code,
for example, in the place where you had to inform the interpreter that you
were going to use the entity of the name.
If the line looks good, the interpreter tries to execute it (note: each line is
usually executed separately, so the trio "read-check-execute" can be
repeated many times - more times than the actual number of lines in the
source file, as some parts of the code may be executed more than once).
You may ask now: which is better? The "compiling" model or the
"interpreting" model? There is no obvious answer. If there had been, one of
these models would have ceased to exist a long time ago. Both of them
have their advantages and their disadvantages.
Compilation Interpretation