Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Module 1 - Introduction to Python and Computer Programming

This document introduces Python programming, covering its fundamentals, the distinction between compilation and interpretation, and the language's history and characteristics. It explains how Python operates as an interpreted, high-level language, its ease of learning and use, and its popularity among programmers. Additionally, it highlights Python's creator, Guido van Rossum, and compares it to rival languages like Perl and Ruby.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 1 - Introduction to Python and Computer Programming

This document introduces Python programming, covering its fundamentals, the distinction between compilation and interpretation, and the language's history and characteristics. It explains how Python operates as an interpreted, high-level language, its ease of learning and use, and its popularity among programmers. Additionally, it highlights Python's creator, Guido van Rossum, and compares it to rival languages like Perl and Ruby.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Fundamentals 1:

Module 1
Introduction to Python and computer programming.

In this module, you will learn about:

• The fundamentals of computer programming, that is, how the computer works, how the program is executed, how the
programming language is defined and built.
• The difference between compilation and interpretation.
• What is Python, how it ranks among other programming languages, and what distinguishes the different versions of Python.

How does a computer program work?


This course aims to explain the Python language and what it is used for. Let's start from the basics.

A program makes a computer usable. Without a program, a computer, even the most powerful, is nothing more than an object.
Similarly, without a pianist, a piano is nothing more than a wooden box.
Computers can perform very complex tasks, but this ability is not innate. The nature of a computer is quite different.

A computer can perform only extremely simple operations, for example, a computer cannot understand the value of a complicated
mathematical function by itself, although this is not beyond the limits of possibility in the near future.

Contemporary computers can only evaluate the results of very fundamental operations, such as addition or division, but they can do so
very quickly and can repeat these actions virtually any number of times.

Imagine you want to know the average speed you have reached during a long trip. You know the distance, you know the time, you
need the speed.

Of course, the computer will be able to calculate this, but the computer is not aware of things like distance, speed or time. Therefore, it
is necessary to instruct the computer to:

• Accept a number that represents the distance.


• Accept a number that represents the travel time.
• Divide the above value by the second and store the result in 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 are far from what the computer can
understand, but they are good enough to translate into a language that the computer can accept.

The key word is language.

Natural Languages vs. Programming Languages


A language is a medium (and a tool) for expressing and recording thoughts. There are many languages around us. Some of them do not
require speaking or writing, such as body language. It is possible to express your deepest feelings very precisely without saying a
single word.

Another language that you use every day is your mother tongue, which you use to express your will and to think about reality.
Computers also have their own language, called machine language, which is very rudimentary.

A computer, even the most technically sophisticated, lacks even a trace of intelligence. You could say it's like a well-trained dog, it
responds only to a predetermined set of known commands.

The commands it recognizes are very simple. We can imagine the computer responding to commands like, "Take this number, divide
it by another number, and save the result."

A complete set of known commands is called an instruction list, sometimes abbreviated IL. Different types of computers can vary in
the size of their IL and instructions can be completely different on different models.

Note: Machine languages are developed by humans.


No computer is currently capable of creating a new language. However, that may soon change. On the other hand, people also use
several very different languages, but these languages were created by themselves. Plus, they're still evolving.

Every day new words are created and old ones disappear. These languages are called natural languages.

What makes up a language?


We can say that every language (machine or natural, it doesn't matter) consists of the following elements:

• An alphabet: a set of symbols used to form words in a given language (for example, the Latin alphabet for English, the
Cyrillic alphabet for Russian, kanji for Japanese, and so on).
• A lexicon: (also known as a dictionary) a set of words that a language offers to its users (for example, the word "computer"
comes from the English dictionary, while "cmoptrue" does not; the word "chat" is present in both English and French
dictionaries, but their meanings are different.
• A syntax: a set of rules (formal or informal, written or intuitively interpreted) used to determine whether a given string of
words forms a valid sentence (for example, "I am a snake" is a syntactically correct sentence, while "I am a snake" is not).
• A semantic: a set of rules that determine whether a sentence makes sense (for example, "I ate a donut" makes sense, but "A
donut ate me" doesn't).

IL is, in fact, the alphabet of a machine language. This is the simplest and most basic set of symbols that we can use to give
commands to a computer. It is the computer's native language.

Unfortunately, this language is far from being a human mother tongue. Both of us (computers and humans) need something more, 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 can use to execute the programs, which
is much more complex than machine language and simpler than natural language.

Such languages are often called high-level programming languages. They are somewhat similar to natural ones in that they use
symbols, words, and conventions readable by humans. These languages allow humans to express commands to computers that are
much more complex than the instructions offered by IL.

A program written in a high-level programming language is called source code (in contrast to the machine code executed by
computers). Similarly, the file containing the source code is called a source file.

Compilation versus interpretation


Computer programming is the act of establishing a sequence of instructions that will cause the desired effect. The effect could be
different in each specific case: it depends on the imagination, knowledge and experience of the programmer.

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

• Alphabetically: A program must be written in a recognizable script, for example, Roman, Cyrillic, etc.
• Lexically: every programming language has its dictionary and you need to master it; fortunately, it is 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 in each of the above four ways. Any of them can render the program completely
useless.

Suppose you have successfully written a program. How do we persuade the computer to execute it? You have to convert your program
into machine language. Fortunately, translation can be done by computer, making the whole process quick and efficient.

There are two different ways to transform a program from a high-level programming language to a machine language:

COMPILATION - the source program is translated once (however, this law must be repeated every time the source code is modified)
obtaining a file (for example, an .exe file if the code is designed to run on MS Windows) containing the machine
code; you can now distribute the file all over the world; the program that performs this translation is called a compiler or translator.

INTERPRETATION - You (or any user of the code) can translate the source program each time it is run; the program that performs
this kind of transformation is called an interpreter, since it interprets the code each time it is intended to be run;
it also means that you cannot distribute the source code as is, because the end user needs the interpreter to run it as well.

Due to some very fundamental reasons, 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. Typically, a programming language is designed with this
factor in mind for its builders: Will it be compiled or interpreted?

What does the interpreter actually do?


Suppose once again 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 must be pure text, without any decoration, such as different fonts, colors, embedded images or other media. Now you need to
invoke the interpreter and let it read the source file.

The interpreter reads the source code in a manner that is common in Western culture: from top to bottom and from left to right. There
are some exceptions: these will be covered later in the course.

First, the interpreter checks whether all subsequent lines are correct (using the four aspects discussed above).

If the compiler finds an error, it terminates its work immediately. The only result in this case is an error message.
The interpreter will tell you where the error is and what caused it. However, these messages can be misleading, as the interpreter
cannot follow your exact intentions and may detect errors at some distance from their real causes.

For example, if you try to use an entity with an unknown name, it will cause an error, but the error will be discovered at the place
where you try to use the entity, not where you entered the new entity name.

In other words, the real reason is usually located a little earlier in the code, for example, in the place where you had to inform the
interpreter that you would use the entity name.

0010 1101 0101


0011 1100 1101
1101 lily 0000

If the line looks OK, the interpreter attempts to execute it (note: each line is usually executed separately, so the "Read - Check -
Execute" trio may be repeated many times, more times than the actual number of lines in the source file, because some parts of the
code may be executed more than once).

It is also possible that a significant portion of the code will execute successfully before the interpreter encounters an error. This is
normal behavior in this execution model.

You may now ask: Which one is better? The "compilation" model or the "interpretation" model? There is no obvious answer. If there
were, one of these models would have ceased to exist long ago. Both have their advantages and disadvantages.

Compilation vs. interpretation - advantages and disadvantages

COMPILATION INTERPRETATION
• Translated code is usually executed faster. •
• Only the programmer should have the You can run the code as soon as you complete it;
compiler; the end user can • there are no additional translation phases.
The code is stored using the programming language,
use the code without it. not the machine language; this means that it can be
ADVANTAG
ES
• Translated code is stored in machine language, run on computers that use different machine
since it is very difficult to understand, your languages; the code is not compiled separately for
own inventions and programming tricks are each different architecture.
likely to remain a secret.
• The compilation itself can take a long time; it
is possible that •
you cannot run your code immediately after Don't expect interpretation to speed up your code:
any modification. your code will share computer power with the
DISADVANTAG
• You have to have so many compilers • interpreter, so it can't be really fast.
as hardware platforms you want your code to Both you and the end user must have the interpreter
ES run on. to run the code.

What does all this mean to you?


• Python is an interpreted language. This means that it inherits all the advantages and disadvantages described. Of course, it
adds some of its unique features to both sets.
• If you want to program in Python, you will 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 used in an interpreted manner are often called scripting languages, while source
programs coded using them are called scripts.

What is Python?
Python is a high-level, interpreted, object-oriented, general-purpose programming language with dynamic semantics, used for general-
purpose programming.

Although you may know the python as a large snake, the name of the Python programming language comes from an old BBC comedy
series called Monty Python's Flying Circus.

At the height of their success, the Monty Python team was performing their skits live for audiences around the world, including at the
Hollywood Bowl.

Since Monty Python is considered one of the two essential nutrients for a programmer (the other being pizza), Python's creator named
the language after the television show.

Who created Python?


One of the amazing features of Python is the fact that it is actually the work of one person. Typically, major programming languages
are developed and published by large companies that employ many professionals, and due to copyright rules, it is very difficult to
name any of the people involved in the project. Python is an exception.

There are not many programming languages whose authors are known by name. Python was created by Guido van Rossum, born in
1956 in Haarlem, Netherlands. Of course, Guido van Rossum did not develop and evolve every component of Python.

The speed with which Python has spread throughout the world is the result of the continuous work of thousands of (very often
anonymous) programmers, testers, users (many of whom are not IT specialists) and enthusiasts, but it must be said that the first idea
(the seed from which Python sprouted) came to one head: Guido's.

A hobby programming project


The circumstances under which Python was created are a little puzzling. According to Guido van Rossum:

In December 1989, I was looking for a "hobby" programming project that would keep me busy during Christmas week. My
office (...) would be closed, but I had a computer at home and not much else on my hands. I decided to write an interpreter
for the new scripting language I'd been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I
chose Python as the working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying
Circus).Guido van Rossum

Python's goals
In 1999, Guido van Rossum defined his goals for Python:

• An easy and intuitive language as powerful as those of the main competitors.


• Open source, so anyone can contribute to its development.
• Code that is as understandable as plain English.
• Suitable for everyday tasks, allowing short development times.

Some 20 years later, it is clear that all these intentions have been fulfilled. Some sources say Python is the most popular programming
language in the world, while others claim it is the third or fifth.

Either way, it still ranks high in the top ten of both the PYPL Popularity of Programming Language and the TIOBE Programming
Community Index.

Python is not a young language. He is mature and trustworthy. It's not a one-hit wonder. It is a shining star in the programming
firmament, and the time spent learning Python is a very good investment.

What makes Python special?


Why do programmers, young and old, experienced and novice, want to use it? How did big companies adopt Python and implement
their products using it?

There are many reasons. We have already listed some of them, but let's list them in a more practical way:

• It's easy to learn: The time required to learn Python is shorter than many other languages, which means you can get started
with real programming faster.
• It is easy to teach: the teaching workload is lower than that required for other languages; this means that the teacher can put
more emphasis on general (language-independent) programming techniques, not wasting energy on exotic tricks, strange
exceptions and incomprehensible rules.
• It's easy to use: When writing new software, you can often write code faster when using Python.
• It's easy to understand: It's often easier to understand someone else's code faster if it's written in Python.

• It's easy to get, install, and deploy: Python is free, open, and cross-platform; not every language can boast that.

Of course, Python also has its drawbacks:

• Not a speed demon: Python does not offer exceptional performance.


• In some cases it can be resistant to some simple testing techniques, which can mean that debugging Python code can be more
difficult than with other languages. Fortunately, making mistakes is harder in Python.
It should also be noted that Python is not the only such solution available on the IT market.

It has many followers, but there are many who prefer other languages and do not even consider Python for their projects.

Python rivals?
Python has two direct competitors, with comparable properties and predispositions. These are:

• Perl: A scripting language originally written by Larry Wall.


• Ruby: A scripting language originally written by Yukihiro Matsumoto.

The first is more traditional, more conservative than Python, and resembles some of the good old languages derived from the classic C
programming language.

In contrast, the latter is more innovative and full of new ideas. Python lies somewhere between these two creations.

The Internet is full of forums with endless discussions about the superiority of one of these three over the others, if you want to learn
more about each of them.

Where can we see Python in action?


We see it every day and almost everywhere. It is widely used to implement complex Internet services like search engines, cloud
storage and tools, social networking, etc. When you use any of these services, you are actually very close to Python.

Many development tools are implemented in Python. More and more everyday applications are being written in Python. Many
scientists have abandoned expensive proprietary tools and switched to Python.
Many IT project testers have started using Python to perform repeatable testing procedures. The list is long.

Why not Python?


Despite Python's growing popularity, there are still some niches where Python is absent or rarely seen:
• Low-level programming (sometimes called "near-the-metal" programming): If you want to implement an extremely
powerful graphics engine or driver, Python would not be used.
• Mobile applications: This territory is still waiting to be conquered by Python, most likely it will happen someday.

There is more than one Python


There are two main types of Python, called Python 2 and Python 3.

Python 2 is an older version of the original Python. Its development has been intentionally stagnated, although that doesn't mean there
are no updates. On the contrary, updates are issued regularly, but they are not intended to modify the language in any significant way.
They prefer to fix any newly discovered bugs and security holes. The Python 2 development path has already reached a dead end, but
Python 2 itself is still very much alive.

Python 3 is the newest (to be precise, the current) version of the language. It is going through its own path of evolution,
creating its own standards and habits.

These two versions of Python are not compatible with each other. Python 2 scripts will not run in a Python 3 environment and vice
versa, so if you want a Python 3 interpreter to run your old Python 2 code, the only possible solution is to rewrite it, not from scratch,
of course. Large parts of the code may remain intact, but you have to review the entire code to find all possible incompatibilities.
Unfortunately, this process cannot be fully automated.

It is too difficult, time-consuming, expensive, and risky to port an old Python 2 application to a new platform. Rewriting your code
may introduce new bugs. It is easier and more sensible to leave these systems alone and improve the existing interpreter, rather than
trying to work within the source code that already works.

Python 3

Python 2

Python 3 is not just an improved version of Python 2, it is a completely different language, although it is very similar to its
predecessor. When viewed from a distance, they appear to be the same, but when viewed up close, many differences are noticeable.

If you are modifying an existing Python solution, then it is most likely coded in Python 2. This is why Python 2 is still in use. There
are too many existing Python 2 applications to dismiss it entirely.

NOTE

If you are starting a new Python project, you should use Python 3, this is the version of Python that will be used during this
course.

It is important to remember that there may be major or minor differences between subsequent versions of Python 3 (p. E.g., Python 3.6
introduced sorted dictionary keys by default in the CPython implementation.) The good news is that all newer versions of Python 3 are
backward compatible with older versions of Python 3. Whenever significant and important, we will try to highlight those differences
in the course.

All code examples you will find throughout the course have been tested with Python 3.4, Python 3.6, Python 3.7, and Python 3.8.

Python aka CPython


In addition to Python 2 and Python 3, there is more than one version of each.
First, there are the Pythons that are maintained by the people gathered around PSF (Python Software Foundation), a community that
aims to develop, improve, expand and popularize Python and its environment. The president of the PSF is Guido van Rossum himself,
and for this reason, these Pythons are called canonical. They are also considered reference Pythons, as any other implementation of
the language must follow all the standards set by the PSF.

Guido van Rossum used the "C" programming language to implement the first version of his language and this decision is still in
force. All Pythons coming from the PSF are written in the "C" language. There are many reasons for this approach. One of them
(probably the most important) is that thanks to it, Python can be easily ported and migrated to all platforms with the ability to compile
and run programs in "C" language (virtually all platforms have this feature, which opens up a lot of expansion and opportunities for
Python).

This is why the PSF implementation is often referred to as CPython. This is the most influential Python among all the Pythons in the
world.

Cython
Another member of the Python family is Cython.

Cython is one of the possible solutions to Python's most painful trait: its lack of efficiency. Large and complex mathematical
calculations can be easily coded in Python (much easier than in "C" or any other traditional language), but the resulting code can be
very time consuming to execute.

How are these two contradictions reconciled? One solution is to write your mathematical ideas using Python, and when you are
absolutely sure that your code is correct and produces valid results, you can translate it into "C". Certainly, "C" will run much faster
than pure Python.

This is what Cython aims to do: automatically translate Python code (clean and clear, but not too fast) into "C" code (complicated and
chatty, but snappy).

Jython
Another version of Python is called Jython.

"J" is for "Java". Imagine a Python written in Java instead of C. This is useful, for example, if you develop large, complex systems
written entirely in Java and want to add some of the flexibility of Python to them. Traditional CPython can be difficult to integrate into
such an environment, since C and Java live in completely different worlds and don't share many common ideas.

Jython can communicate with existing Java infrastructure more effectively. This is why some projects find it useful and necessary.

Note: The current implementation of Jython follows Python 2 standards. As of now, there is no Python 3 compliant Jython.
PyPy and RPython
Check out the logo below. Can you solve it?

It's the logo for PyPy - a Python within a Python. In other words, it represents a Python environment written in a Python-like language
called RPython (Restricted Python). It is actually a subset of Python.

PyPy source code is not executed in an interpretive manner, but is translated into the C programming language and then
executed separately.

This is useful because if you want to test any new features that may or may not be introduced in the Python implementation, it is easier
to verify them with PyPy than with CPython. This is why PyPy is more of a tool for Python developers than for everyone else.

This does not make PyPy any less important or any less serious than CPython.

Additionally, PyPy supports the Python 3 language.

There are many more different Pythons in the world. You'll find them if you look for them, but this course will focus on CPython.

You might also like