0% found this document useful (0 votes)
3 views

Python

The document provides an introduction to Python and computer programming, explaining how computer programs work and the difference between natural and programming languages. It discusses the concepts of machine language versus high-level languages, as well as the processes of compilation and interpretation in programming. The document concludes by noting that Python is an interpreted language, highlighting its advantages and the need for the Python interpreter to run code.

Uploaded by

iftekharshah2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python

The document provides an introduction to Python and computer programming, explaining how computer programs work and the difference between natural and programming languages. It discusses the concepts of machine language versus high-level languages, as well as the processes of compilation and interpretation in programming. The document concludes by noting that Python is an interpreted language, highlighting its advantages and the need for the Python interpreter to run code.

Uploaded by

iftekharshah2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Essentials 1

Module 1: Introduction to Python and Computer Programming

Section 1: Introduction to Programming


Section 2: Introduction to Python
Section 3: Downloading and Installing Python

Section 1: Introduction to Programming

1.1 How does a computer program work?

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:

 accept a number representing the distance;


 accept a number representing the travel time;
 divide the former value by the latter and store the result in the memory;
 display the result (representing the average speed) in a readable format.

These four simple actions form a program.

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.

Language is the keyword.

1.2 Natural languages vs. programming languages

A language is a means (and a tool) for expressing and recording thoughts.

The language you use each day is your mother tongue.

Computers have their own language, too, called machine language, which is
very rudimentary.

A computer, even the most technically sophisticated, is devoid of even a trace of


intelligence and responds only to a predetermined set of known commands.

A complete set of known commands is called an instruction list, sometimes


abbreviated to IL.
Note: machine languages are developed by humans.

No computer is currently capable of creating a new language. However, that may


change soon.

1.3 What makes a language?

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

a set of rules (formal or informal, written or felt intuitively) used to determine if a


certain string of words forms a valid sentence (e.g., "I am a python" is a
syntactically correct phrase, while "I a python am" isn't)

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)

1.4 Machine language vs. high-level language

The IL is, in fact, the alphabet of a machine language.

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.

Both (computers and humans) need something else, a common language


for computers and humans, or a bridge between the two different worlds.

We need a language in which humans can write their programs and a


language that computers may use to execute the programs, one that is far
more complex than machine language and yet far simpler than natural
language.
Such languages are often called high-level programming languages. They
are at least somewhat similar to natural ones in that they use symbols,
words and conventions readable to humans. These languages enable
humans to express commands to computers that are much more complex
than those offered by ILs.

A program written in a high-level programming language is called a source


code (in contrast to the machine code executed by computers). Similarly,
the file containing the source code is called the source file.

1.5 Compilation vs. Interpretation

Computer programming is the act of composing the selected programming


language's elements in the order that will cause the desired effect. The
effect could be different in every specific case – it's up to the programmer's
imagination, knowledge and experience.

Of course, such a composition has to be correct in many senses:

 alphabetically – a program needs to be written in a recognizable script,


such as Roman, Cyrillic, etc.
 lexically – each programming language has its dictionary and you need
to master it; thankfully, it's much simpler and smaller than the dictionary
of any natural language;
 syntactically – each language has its rules and they must be obeyed;
 semantically – the program has to make sense.

Unfortunately, a programmer can also make mistakes with each of the


above four senses. Each of them can cause the program to become
completely useless.

Let's assume that you've successfully written a program. How do we


persuade the computer to execute it? You have to render your program into
machine language. Luckily, the translation can be done by a computer itself,
making the whole process fast and efficient.

There are two different ways of transforming a program from a high-level


programming language into machine language:

Compilation – the source program is translated once by getting a file


containing the machine code. However, this act must be repeated each time
you modify the source code. Now you can distribute the file worldwide. The
program that performs this translation is called a compiler.

Interpretation – the source program is translated each time it has to run.


The program performing this kind of transformation is called an interpreter,
as it interprets the code every time it is intended to be executed. It also
means that you cannot just distribute the source code as-is, because the
end user also need the interpreter to execute it.

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?

1.1.6 What does the interpreter do?

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).

It is also possible that a significant part of the code may be executed


successfully before the interpreter finds an error. This is normal behavior in
this execution model.

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.

1.7 Compilation vs. Interpretation – Advantages and Disadvantages

Compilation Interpretation

✓the execution of ✓you can run the code as


the translated code soon as you complete it –
is usually faster; there are no additional
✓only the user has phases of translation;
A
to have the compiler ✓the code is stored using
d
– the end-user may programming language, not
v
use the code machine language - this
a
without it; means that it can be run on
n
✓the translated computers using different
t
code is stored using machine languages; you
a
machine language – don't compile your code
g
as it is very hard to separately for each
e
understand it, your different architecture.
s
own inventions and
programming tricks
are likely to remain
your secret.

D ❌the compilation ❌don't expect interpretation


i itself may be a very to ramp up your code to
s time-consuming high speed - your code will
a process – you may share the computer's power
d not be able to run with the interpreter, so it
v your code can't be really fast;
immediately after ❌both you and the end user
a making an have to have the interpreter
n amendment; to run your code.
t ❌you have to have
a as many compilers
g as hardware
e platforms you want
s your code to be run
on.

What does this all mean for you?

 Python is an interpreted language. This means that it inherits all the


described advantages and disadvantages. Of course, it adds some of its
unique features to both sets.
 If you want to program in Python, you'll need the Python interpreter.
You won't be able to run your code without it. Fortunately, Python is
free. This is one of its most important advantages.

Due to historical reasons, languages designed to be utilized in the


interpretation manner are often called scripting languages, while the
source programs encoded using them are called scripts.

You might also like