Sie sind auf Seite 1von 68

1

Introduction to computer
programming

Same logics, Computers (including in this


1.1 different languages term, in a broader sense,
smartphones, tablets and even
multimedia players and GPS navigators) are able to perform

se
a very high and varied number of tasks. This depends on the
fact that these devices can be instructed, for each of the tasks

lu
they are required to perform, through programs. A program is
na
a set of instructions that a computer must follow systemati-
tio
cally to perform a certain task, whether it is a simple or very
a

complex task: the complexity of the program will vary accord-


uc

ingly, but the logic remains the same. Apps for smartphones
ed

and tablets are no exception: they are nothing more than pro-
grams developed with a programming language, optimized for
or

mobile devices.
-f

In the following pages, we will focus on programming, al-


e

though the book is not meant for professional programmers.


pl
m

We will present the logic of programming and introduce Py-


Sa

thon, a very powerful language and widespread in the profes-


sional field, but also relatively easy and particularly suitable
for a first approach to coding. In fact, programming is not (any
more) an exclusive activity of professional programmers, but
it is increasingly present in many professions, especially those
that for various reasons include the collection and analysis of
data: from business intelligence to web marketing to social
networks or in the financial field, just to mention some top-
ical examples. Programming in fact allows to instruct a com-
puter, for example, to collect large amounts of data that would
be impossible to collect manually and process them effectively.
The same concepts of data science and big data would make lit-
tle sense if there were no sophisticated programs dedicated to
1
these tasks, but also simple scripts produced by non-program-
ming professionals. With a few lines of code, in Python or oth-
er languages, it is possible for example to launch a script that
collects all the Tweets or posts labeled with a certain hashtag
from the Internet and perform a sentiment analysis, or do
web scraping to look for trends and gather information in
massive manner.
Starting from the idea that programming is not just for pro-
grammers, the aim of this book is to make anyone (with a min-
imum of «computer culture») able to write simple programs in
Python, understand complex programs and above all assimilate
the logic of programming, which is common to all languages,
so as to be able to approach and understand any programming

se
language with a reasonable effort. Although the syntax of the

lu
various languages is different and requires a specific study, the
na
main logic and constructs are widely shared. Learning the fun-
damentals of programming with Python therefore enables ac-
tio

cess to the vast world of all-round software programming.


a
uc

Understanding the logic of programming and, more gen-


erally, computational thinking (the set of mental processes in-
ed

volved in the formulation of a problem and its solution, so that


or

a machine can actually execute them), are fundamental skills in


-f

the current scenario and very appealing in the job market. For
e

this reason, the study of programming with Python will be the


pl

key to opening many doors in present and future professions.


m

Before going into the logic and the programming methods,


Sa

it is important to understand how computers work and, in par-


ticular, how data is stored and managed. This allows us to pen-
etrate the very meaning of programming and the logic of high-
level languages.

Computer In our explanation, we will


1.2 and data recording take for granted that the read-
er knows the terminology and
the basic concepts concerning computers, starting from hard-
ware (the set of physical components of the computer) and
software (the set of programs that allows it to work and man-
age all operations).
2
To understand how computer «think», and therefore how
they can understand and manage the instructions given to it 1
by a programming language, we must step back and return to

Introduction to computer programming


their origins recalling the Von Neumann Architecture. This is
the reference architecture for any computer, based on what
was described in 1945 by the Hungarian mathematician and
computer scientist (naturalized US) John Von Neumann and
still valid today. The main components of Von Neumann Ar-
chitecture are:
XX CPU (Central Processing Unit): this is the processor that per-
forms the basic operations specified in the program (which
include arithmetic, logic, control and input/output opera-

se
tions);
memory: necessary to store both data and instructions. Com-

lu
XX
puters have different types of memories: ROM, RAM, mass
na
storage (hard disks, flash memories etc.);
tio
XX input/output devices: this category includes keyboards, dis-
a

plays/monitors, speakers etc.


uc

These components communicate with each other and exchange


ed

data through communication channels called BUS (Figure 1.1).


or

Computers, from the very beginning and up until today,


-f

have basically all the same architecture (whether desktop PCs,


laptops, tablets or smartphones). Over time, the computing
e
pl

power, size and sophistication of the components have ob-


m

viously changed, but the general architecture has always re-


Sa

mained the same.

FigurE 1.1  Representation of the Von Neumann Architecture

Input
&
Output CPU Memory
devices

BUS
3
Another thing that has remained unchanged is the lan-
guage used by the computer CPU. A fundamental concept is
that the CPU of any computer «speaks» and understands a sin-
gle language, called machine language. This language consists of
0 and 1 alone: any type of information is computed, managed
and stored by the CPU and computer memory in the only form
of binary digits 0 and 1 (each of which represents a bit of in-
formation). Therefore all programming languages, even those
more evolved (to be relatively easy, understandable and usable
by developers and users), always include a sort of «translation
system» into machine language, without the need for user in-
tervention.
In the 1950s, when computers were as big as a whole room

se
and coding was in the early stages, developers wrote the pro-

lu
gram instructions as a sequence of 0 and 1, and used punched
na
cards to physically memorize the instructions. Only simple
tio
instructions could be entered, such as reading from memory
a

areas, writing in memory areas and operations on numbers.


uc

The development of languages has made programming easi-


ed

er (making it less and less a job reserved for highly specialized


personnel) and allowed computers to perform increasingly re-
or

fined tasks.
-f

In essence, programming means learning a language to talk


e

to the machine. Every programming language is made to com-


pl

municate and give instructions to the machine, and despite the


m

different syntax and details, each one of them is built to give


Sa

the same kind of simple instructions to the machine. In fact,


the CPU is able to perform simple operations only. The set of
LEARNING PYTHON

many simple operations, performed by the CPU very quick-


ly, allows performing even very complex tasks. The syntax of
modern and sophisticated programming languages (which we
will learn to call «high-level languages») allows writing pro-
grams that instruct the machine so that it can also solve such
complex tasks.
Computer memory is divided into small memory units
called bytes. Each byte corresponds to a precise memory loca-
tion and contains exactly 8 bits, or the binary digits of which
the machine language is made. Each byte allows to store a single
4
datum, whether it is a number, a character, the color of a pixel,
the sample of a sound. Each type of data is stored in a different 1
way, following specific rules. In fact, each piece of information

Introduction to computer programming


must be broken up and transformed into bytes. For integers
greater than or equal to zero the conversion is simple, in fact
these numbers can be easily converted into binary numbers. An
information byte is used to represent numbers from 0 to 255,
for larger numbers more bytes are required. For negative num-
bers and real numbers, however, there are other more complex
coding systems. For characters, the most widespread encoding
system is the ASCII standard (American Standard Code for In-
formation Interchange). It is a table that contains 128 numer-
ic codes associated with as many letters of the English alpha-

se
bet, different punctuation marks and other characters; each of

lu
these characters corresponds to a number, and storage in bytes
na
takes place using this number converted into binary code. For
tio
example, the capital letter A corresponds to the code 65 in the
ASCII table, B to 66, C to 67 and so on. The ASCII coding sys-
a
uc

tem was developed in the 1960s, and has been widely used by
ed

computer manufacturers. However, it has limitations, provid-


ing a set of 128 characters. For this reason, the unicode coding
or

system was developed, which includes a much broader set of


-f

characters related to many world languages; unicode is com-


e

patible with ASCII and is gradually supplanting it as a standard.


pl

Even the pixels that illuminate the screen are converted


m

into a numeric code that represents their color according to a


Sa

predetermined table and which in turn is converted into bina-


ry code. Even the sounds of a piece of music are converted into
binary numbers: music or sounds are divided into tiny portions
(called samples) through the sampling process: the greater the
number of samples into which the sound is divided, the great-
er is the its fidelity to the original (this is why the «perfection»
of sound can only be reproduced with an analog support). For
example, a piece of music on a CD is divided into over 44,000
samples per second. Each sample is then converted into a bina-
ry number to be stored and managed by the CPU.
Clearly, to handle complex information, the memory units
of modern computers are able to store huge amounts of bytes
5
(in the order of Gigabytes, that is billions of bytes, or Terabytes,
that is trillions of bytes). At the same time, CPUs can process
information with great speed, in the order of billions of oper-
ations per second.

As mentioned, the CPU is


1.3 How a program works an electronic component
designed to perform spe-
cific tasks, which are limited to a few simple operations: read-
ing data from memory, adding, subtracting, multiplying or di-
viding numbers, moving data from one memory location to an-
other, compare values. The CPU, although often referred to as

se
the «brain» of the computer, is not equipped with any intelli-
gence: everything it does depends on the instructions commu-

lu
nicated to it by the programs. A program is nothing but an or-
na
dered set of instructions that tells the CPU what to do. These
tio
instructions must be very detailed, because in itself the CPU is
a

unable to do anything! A program could therefore contain from


uc

a few lines of code up to hundreds, thousands or even millions


ed

or billions of instructions (LOCs, Lines Of Code) to allow the


CPU to perform complex tasks. As an example, the code cor-
or

responding to the set of services offered by Google consists of


-f

approximately 2 billion instructions, and its complexity can be


e

compared to that of the human genome!


pl
m

Instructions can be divided into blocks or collected into


Sa

functions, as we will learn later.


Programs are usually stored in a mass memory of the com-
puter (i.e. a non-volatile memory), represented for example by
LEARNING PYTHON

the hard disk (in the case of a desktop computer or a laptop),


a flash ROM or a memory card (in the case of a smartphone or
a tablet). When the program is run, it is copied from the mass
memory to the central memory (RAM). The CPU processes the
program and executes the instructions one after the other in
sequence.
Programming, today, means writing the code of a program
through a language like Python, C, Java (just to name a few ex-
amples) or any other programming language. As we have al-
ready mentioned, in order to make the execution of the pro-
6
gram possible by the CPU, however, a translation must take
place from the source code, written in the specific language, to 1
the machine language that the CPU is able to understand. The

Introduction to computer programming


translation to the machine language can take place in two dif-
ferent ways: compilation or interpretation.
In the first case, the software, once developed, is compiled
through a specific command of the development environment
and produces an executable file (for example with the extension
.exe or .bat in Windows). From the moment of compilation on-
wards, it is no longer necessary to translate the software, thus
speeding up execution times. On the other hand, once the soft-
ware has been compiled in machine language, from the pro-
grammer’s point of view it becomes difficult to check if certain

se
errors have occurred: it will therefore be necessary to return to

lu
the code, correct it and then compile it again. An example of a
language to be compiled is C. na
The interpreted software, on the other hand, is translated
tio

and executed instruction by instruction from a software that acts


a
uc

as an interpreter, when it is launched. From this, it derives that


it is slower in the times of execution (although this is not the
ed

only discriminating factor in the performances of a language),


or

but also easier to analyze and correct in case of errors. Python


-f

is part of this category, which is why it is suitable for a first ap-


e

proach to coding. Interpreted languages are in fact more suit-


pl

able for approaching the world of coding for beginners, being


m

able to perform single operations without having to develop


Sa

and compile the entire code.


Using Python, we will learn to know and use its interpret-
er and we will better understand its logic.

If, in the beginning, pro-


1.4 Evolution of languages gramming in machine
language was an incred-
ibly long and complex operation, which required hyper spe-
cialized technicians, and nonetheless subject to a high rate of
errors (exchanging a 0 and a 1 could lead to the failure of the
whole program), over time there have been several improve-
ments. From machine language, extremely difficult to use, we
7
have arrived in time to advanced languages, whose instructions
resemble the natural language, in particular English. This has
allowed creating much more sophisticated programs and has
allowed many more people, less specialized, to approach this
discipline. Anyway, in order to program it is necessary to learn
principles that are not always simple and the specific syntax
of one or more programming languages: it is therefore always
necessary to have a longer or shorter learning period depend-
ing on the objectives.
The first evolutionary step occurred in the late 1950s, when
the Assembly language was introduced precisely to simplify the
programming process. Assembly was actually created in the
early days of programming, in the 1940s, but it began to be in-

se
creasingly used in correspondence with the diffusion of com-

lu
puters capable of storing programs (different from the previ-
na
ous computers programmed exclusively through switches and
tio
cables). Assembly for the first time introduced, in place of the
simple numerical sequences of 0 and 1, some mnemonic words,
a
uc

such as add (to add two numbers), mul (to multiply), mov (to
ed

move) and so on. Even programs written in Assembly need, as


for any other language than machine language, a translation to
or

be performed by the CPU. For this purpose, there is a specific


-f

program, called Assembler, which translates the Assembly code


e

into machine language. Assembly language is still difficult to


pl

use, requires a thorough knowledge of the CPU and produces


m

long programs even for simple operations. It is therefore con-


Sa

sidered a low-level language, still very close to machine language.


Today it is still used in some niche applications, such as in driv-
ers or firmware of operating systems.
LEARNING PYTHON

Since the 1950s, other languages with more advanced syn-


tax, called high-level languages, began to spread. High-level lan-
guages have developed since then into thousands of variants,
from the most spartan ones such as COBOL to the modern C,
Java or Python and all their similes widespread today. These
languages have a syntax similar to the English language and
allow to write complex programs without necessarily know-
ing the operation of the CPU and without having to write long
sequences of instructions to perform every simple operation.
8
Among the most popular languages there are Java, C, Python,
C++, Visual Basic .NET, C #, JavaScript, PHP, SQL and MATLAB 1
(for a more complete and up-to-date ranking of the most used

Introduction to computer programming


languages, to consult the TIOBE ranking at the URL https://
www.tiobe.com/tiobe-index/).
Programming languages can be classified in several ways,
depending on the paradigm used (i.e. the fundamental pro-
gramming style, which defines the way in which the program-
mer conceives the program itself). They are mainly divided into
declarative and procedural languages, where the former have the
characteristic of describing reality and declaring the required
objective, without implementing precise resolution algorithms,
while the latter on the contrary define algorithms that lead to

se
the goal step by step.

lu
Python is considered a multi-paradigm language (or «mixed
na
paradigm»), and was created with the aim of taking the best of
existing paradigms and languages, combining their qualities,
tio

overcoming their limits and correcting their defects, to simpli-


a
uc

fy the programming process. In fact Python can be considered


a procedural language, but also declarative (in the subgroup of
ed

functional languages) and finally it uses the paradigm of Object-


or

Oriented Programming (OOP), very much in vogue at present.


-f

This topic will be explored in Chapter 12, when the fundamen-


e

tal features and basic structures of Python will have been clar-
pl

ified and studied through various applications, and more so-


m

phisticated topics such as classes and objects can be introduced.


Sa

The program When a program is devel-


1.5 development cycle oped at a professional level,
its realization cannot be im-
provised and it cannot be created immediately, line by line. A
process divided into different steps is necessary, which starts
from the analysis of the requests or needs that the program
must satisfy, up to its writing and distribution. The develop-
ment cycle therefore represents the different steps that must
be completed to develop a complete program, which starts from
the analysis of the requirements and does not concern only the
actual coding step.
9
FigurE 1.2  The program development cycle

Requisites Analisys

Design

Implementation
Debugging

Test Execution

se
lu
na
It is possible to meet different representations of the program
tio
development cycle (or development process, or life cycle), but
a

most converge on the steps indicated in Figure 1.2, with slight


uc

variations.
ed

The requirements indicate the objectives to be achieved and,


in essence, the functions that the program will have to offer.
or

Requirements analysis is therefore a fundamental part of the


-f

program development cycle.


e

In the design phase, the essential lines of the program struc-


pl
m

ture are defined according to the requirements highlighted by


Sa

the analysis. Often in this phase algorithms (§ 1.6) are used to


represent the flow of statements that the program will have to
include to perform the tasks or solve the given problems.
LEARNING PYTHON

The implementation is the real programming phase of the pro-


gram, the one that creates the software solution through coding
(i.e. the writing of the code in a specific programming language).
The software is gradually tested and any errors are resolved
through the debugging phase, which can lead to corrections in
the syntax or code structure. Eventually the program is execut-
ed, or published, and used.
The development cycle does not vary depending on the com-
plexity of the objectives to be achieved: what varies is the com-
10

plexity of the algorithm and its implementation.


1
Algorithms are a key
1.6 Algorithms and flow charts element of the de-
sign and implemen-

Introduction to computer programming


tation steps of a program. An algorithm can be described as a
finite sequence of coded actions that leads to the solution of a
problem. During the design step, the problem is translated into
an algorithm that leads to its solution, and the implementation
step is in fact the translation of this algorithm into a program,
using a certain language.
Algorithms must have some crucial features:
XX the sequence of statements must be finite;
XX the procedure must lead to a result;
XX the statements must be materially executable;

se
XX the statements must be expressed unambiguously.

lu
The ability to split a task into its components is one of a pro-
na
grammer’s main operations. In practice, once an algorithm that
tio
leads to the solution of a problem has been developed, howev-
a

er complex it may be, it is possible to automate the process of


uc

solving the problem by translating the steps of the algorithm


ed

into statements.
Algorithms are commonly represented by flow charts, i.e.
or

a graphic notation used to describe intuitively the actions of


-f

which the algorithm itself consists. In flow charts, there are


e

several elements (called blocks), each of which is represented


pl
m

by a certain geometric shape:


Sa

XX oval boxes indicate beginning and end blocks of an algorithm;


XX rhomboids are input/output elements;
XX rectangles are execution elements and represent one or more
elementary actions;
XX diamonds are elements of decision or choice;
XX arrows represent the flow of execution of the algorithm, from
the beginning to the end.
A simple example of an algorithm, represented by the corre-
sponding flow chart, can be the calculation of the area of a rect-
angle (Figure 1.3).
In this book, attention is focused on programming, and not
on the process of engineering a software product (requirements
11
FigurE 1.3  E
 xample of a flow chart related to an algorithm:
calculating the area of a rectangle

Start

Read Length

Read Width

se
lu
Calculate Area
Length x Width na
a tio
uc

Show Area
ed
or
-f

End
e
pl

analysis, algorithm design, modeling etc.) but it is important to


m

understand the logic that leads from a problem to its represen-


Sa

tation in algorithm and its implementation in code. This will


facilitate the understanding of the code of simple and complex
programs, even by beginners.

In Chapter 2, we will get into Python


1.7 Why Python? programming. To conclude this in-
troductory chapter, let us try to un-
derstand why Python is gaining more and more admirers, both
among professional developers and non-professionals who
want to approach coding with the most varied objectives.
Python is undoubtedly a powerful language, but its suc-
cess is mainly due to other features. It was created by a Dutch
12
programmer, Guido Van Rossum, who at the end of 1989 de-
cided to write a language to correct the «flaws» present in the
1

Introduction to computer programming


other languages. Python was immediately successful in his re-
search center and in February 1991 it was published on Usenet
(the computer network still existing created in 1979 as an al-
ternative to ARPANET, the current Internet) and thus spread
throughout the world. About the starting point for his work
on Python, Van Rossum said, «In December 1989, I was look-
ing for a “hobby” programming project that should have kept
me busy over the Christmas week. My office would have been
closed, but I had a computer, and not much more. I decided
to write an interpreter for a new scripting language I would

se
have thought of later: a descendant of the ABC [another lan-

lu
guage previously devised by Van Rossum], which should have
belonged to the Unix hackers. I chose Python as the name of
na
the project, being slightly irreverent (and I’m a big fan of Mon-
tio
ty Python’s Flying Circus)». The name Python therefore derives
a

from the passion of Van Rossum for the well-known group of


uc

English comedians Monty Python.


ed

In 1999, Van Rossum presented a proposal to DARPA


(Defense Advanced Research Projects Agency, government
or

agency of the US Department of Defense in charge of devel-


-f

oping new technologies for military use, responsible among


e

other things for the creation of ARPANET) called Comput-


pl
m

er Programming for Everybody, in which he defined his goals


Sa

for Python:
XX a simple language, intuitive and powerful as its major ad-
versaries;
XX free and open source, so that anyone could participate in its
development;
XX an easily understandable code, such as spoken English;
XX great for everyday tasks, as it allows for short development
times.
One reason for the popularity of Python certainly lies in the
simplicity of the code, which facilitates understanding for be-
ginners. Python code is easy to read and easy to learn, while
still being a very powerful language.
13
Over the years Python has grown, evolved (under the direct
control of Van Rossum) and has won increasing popularity, first
with the Internet arena and, especially in recent years, with the
field of data science. Van Rossum worked from 2005 to 2012
in Google, and since then in Dropbox, not surprisingly two gi-
ants that make extensive use of Python in their web services.
Python is an open source language with a huge following
of volunteers who are constantly trying to improve it. This
allows the language to stay up to date with the latest trends.
The community is very large, and programmers can reuse ex-
isting code and features instead of developing them over and
over again.
Python is today one of the most popular languages (see the

se
TIOBE index mentioned in § 1.4) and measures against real «sa-

lu
cred monsters» like Java, C and C++. These are consolidated lan-
na
guages that have received great attention by the academic and
tio
business world, but which, being more complicated, are main-
a

ly used by professional developers. Python, compared to these


uc

three competitors, allows to do several things relatively easi-


ed

ly. As mentioned, it is in great demand in the field of data sci-


ence and big data, where business analysts familiar with this
or

language are becoming much sought after. There are also ad-
-f

ditional libraries that make Python very versatile (we will talk
e

about it in Chapter 11).


pl

In general, Python is used in the following areas:


m
Sa

XX data analysis;
XX machine learning;
XX web development;
LEARNING PYTHON

XX video game development;


XX web scraping;
XX script to automate repetitive actions;
XX development of graphical interfaces (GUIs).
There are several success stories about Python. For example,
it has been used for special effects and computer animation in
many of ILM’s recent films (Industrial Light & Magic, part of
LucasFilm, now owned by Disney): Star Wars, Thor, Transform-
14

ers, Iron Man, Star Trek, Pirates of the Caribbean and many others.
In the Web, it is employed by giants like Google, Mozilla Cor-
poration, Dropbox, and YouTube. It is used by NASA and it is 1
at the center of the ESA project that will bring a space probe to

Introduction to computer programming


Mars. Many games and smartphone apps are also developed
with Python.
In Chapter 2, we will begin to get to know Python closely
and discover its syntax and rules.

se
lu
na
a tio
uc
ed
or
-f
e
pl
m
Sa

15
2
Introduction to Python

Looking through the FAQs on the


2.1 What is Python? official Python site (www.python.
org), you can find some interest-
ing definitions to frame the nature of this language. Python is

se
described as a high-level, interpreted, interactive, object-ori-
ented, simple programming language that uses dynamic typ-

lu
ing, strongly typed, incorporating high-level modules and data
na
types, and portable.
tio
We have already introduced the concepts of high-level lan-
a

guage (§ 1.3) and interpreted language (§ 1.4). To be an inter-


uc

preted language, Python is particularly fast and efficient.


ed

Python is interactive because it allows writing instructions


directly in its command prompt (the shell, § 2.3), without even
or

needing to create or modify a source file; this mode is quite spe-


-f

cial, and it can be convenient to start from scratch with pro-


e

gramming, to try some instructions or test small parts of pro-


pl

grams.
m
Sa

We will better understand what object oriented means in


Chapter 12, when we will talk about classes, attributes and
methods. At the moment, it is enough to say that the solution
LEARNING PYTHON

of a problem is not seen only as a sequence of statements but


in terms of objects, their attributes and the actions that these
objects can perform.
A fundamental peculiarity of Python is the clarity of its syn-
tax. As we will see, the statement blocks are delimited solely
by the indentation of text. There are no punctuation elements,
codes or particular syntaxes to indicate the beginning and end
of a block (which can represent a function, a cycle, a condition-
al or other construct), as it happens in other languages. We will
16

therefore learn that the correct use of indentation is fundamen-


tal in Python writing, and that this at the same time makes the
code particularly neat and readable. 2
Another feature of Python, which makes it more agile and

Introduction to Python
less «formal» than other languages, is the dynamic typing of vari-
ables. To understand this concept fully, it is necessary to have
become familiar with the concept of variable and with the types
of data. We will discover (Chapter 4) that variables are funda-
mental elements of a program because they contain the values
necessary or resulting from the calculations and operations that
the program performs. In many other languages, when intro-
ducing a variable it is necessary to use a more or less complex
syntax, which declares from the start which type of data can
be stored in that variable (in this case we talk about static typ-

se
ing). In Python, instead, it is sufficient to assign an initial val-

lu
ue to a variable to define its type, without further formalisms.
na
The data type of the variable can also change if another value
tio
is assigned with a different type. At the same time, however,
data typing is very important in Python and it is not possible to
a
uc

perform certain operations between different data types, while


ed

there are conversion functions (§ 4.6) that allow to change the


type of data when necessary (for example the value 10 can be
or

used as a number or as a string, as needed).


-f

Similarly to other languages, Python uses the most com-


e

mon elementary data types (§ 4.4) such as integers, strings,


pl

decimal numbers, Boolean values, but also provides other very


m

specialized ones such as lists, tuples and dictionaries (Chap-


Sa

ters 8 and 9).


As we have seen, Python was born many years ago from the
mind of Guido Van Rossum, and since then it has never stopped
evolving. The current version is 3.x, where 3 indicates the ma-
jor release and x the minor release, which varies more often and
introduces new features of lower impact. The transition from
major release 2.x to 3.x, which has progressively taken place
in recent years, has brought substantial innovations and you
may still find applications or modules developed with Python
2.x and not compatible with the new versions. For those who
approach Python from scratch, however, the reference version
is without a doubt 3.x.
17
A final key feature of Python is its portability. Python is com-
patible with all popular operating systems, so once you have
written a Python program on a computer with a Windows op-
erating system, it can be used without problems on Mac OS X,
Linux and even inside an iPod, PlayStation and several smart-
phones.
Once Python is installed, we will find that we automatical-
ly have several modules that provide additional functions and
commands. We will also learn to expand the potential of Python
with libraries developed by third parties, which will enable us
to perform virtually any task imaginable with Python. Experi-
enced programmers can also create custom modules, for their
own needs or to share them with the developer community.

se
Now, the question is: where do we start? The first thing to

lu
do to code in Python is to download and install the software,
na
in the latest version available. As we will see, a basic develop-
ment tool is included in the Python installation and, due to its
tio

simplicity and stability, it is particularly suitable for newbies:


a
uc

IDLE. This is not the only development tool dedicated to Py-


thon, there are several and more sophisticated ones. Among
ed

the most popular, we can mention Anaconda (www.anacon-


or

da.com), Pythonanywhere (pythonanywhere.com) and Atom


-f

(https://atom.io/). In this book, we will refer only to IDLE,


e

but the concepts related to the code do not vary in any case
pl

if you use another tool. Only the interface and the possibili-
m

ty of having additional libraries and tools for specific needs


Sa

change. For example, Anaconda presents a preconfigured en-


vironment with several additional modules and tools useful
for data analysis.
Let us now proceed to the installation of Python and the
first contact with IDLE.

To download Python we
2.2 How to install Python should connect to the of-
ficial website www.python.
org. You can also download from other websites, but here you
will always find the most up-to-date version of the software
as soon as it is released, along with the official documentation
18
FigurE 2.1  T
 he home page of the website www.python.org
with the link to download Python 2

Introduction to Python
se
lu
na
a tio
uc
ed

and information. Python is not localized, so it does not have an


Italian version. The language is exclusively English for installa-
or

tion, the menus and dialog boxes of the tools that are installed.
-f

In the home page of the python.org website (Figure 2.1) it is


e

possible to find, in the Download section at the bottom, a link


pl
m

containing the indication of the most updated version. Click-


Sa

ing on this link gives access to a page that contains all the in-
formation on the latest version and the variations of the instal-
lation file based on the operating system in use.
There is also a Downloads section, higher up (just below the
word pythonTM), which opens a panel with the download link
for the latest version. This immediately downloads the basic
version for the operating system in use, without proposing any
choice (suitable for beginners).
Upon clicking on the link, the installation file will be down-
loaded to your computer (it may be called python-3.x.x.exe or
similar, depending on the operating system). Starting the ex-
ecutable file, the installation procedure will start. Python will
show a dialog box like the one in Figure 2.2.
19
FigurE 2.2  The dialog to install Python

se
lu
na
a tio
uc

Before continuing, it is important to verify that the Add Py-


ed

thon 3.x to PATH check box is selected. Otherwise, Python will


not work properly (especially when trying to install new mod-
or

ules). If Python is already installed, but in a previous version,


-f

when the installation file is run, Python will be updated the


e

latest version.
pl

Once the Add to PATH item has been selected, you can click
m

Install Now. This will actually start the installation, which will
Sa

take a few minutes. When finished, you can close the dialog and
open Python from your computer’s menu. Along with the in-
terpreter and the development environment, the Python doc-
umentation and manuals will also be installed.

Once Python is installed,


2.3 IDLE: editor and shell IDLE (Python 3.x) will ap-
pear in the computer pro-
grams, where 3.x is the Python version installed. IDLE (Inte-
grated Development and Learning Environment) is the program-
ming environment included in the Python installation, which
20

includes two different components:


XX the shell, which is used in interactive mode (or «command
line»), that allows you to type and execute single statements 2
and immediately see the result;

Introduction to Python
XX the editor, which is used in script mode, i.e. allows you to cre-
ate the actual programs, which can be saved in the comput-
er’s memory and be executed whenever you wish.
IDLE is part of the programs called, in general, IDE (Inte-
grated Development Environment), that is programs that pro-
vide all the necessary tools to write, execute and test a pro-
gram. IDLE integrates some features specifically designed to
help write Python code. For example, the code is automati-
cally colored as it is written, so that keywords, functions and

se
other program elements are displayed in specific colors that
allow you to distinguish them at a glance (and also to imme-

lu
diately understand if the words are have been correctly writ-
na
ten, since otherwise they do not color). This makes it easier
tio
to write and read programs.
a

Opening IDLE from the operating system menu will open


uc

the shell window (Figure 2.3).


ed

Referring to what we have said about interpreted languag-


es, of which Python is a part, the shell is the Python interpreter,
or

which is the program able to read the instructions of the code


-f
e
pl

FigurE 2.3  The shell window


m
Sa

21
and execute them. For this reason, we often refer to the Python
shell as the «interpreter». The shell shows an opening message
that contains the indication of the installed version.
The symbols >>> represent the prompt, which indicates
that the interpreter waits for the insertion of a statement.
When you type a statement, directly after the prompt, this is
executed and the interpreter waits for a new statement (show-
ing the prompt again). For example, the following statement
displays the text contained in quotes:
>>> print('Python forever')

When you press the Enter key on the keyboard, the interpret-

se
er will execute the statement and re-propose the prompt, wait-
ing for a new statement.

lu
>>> print('Python forever')
na
tio
Python forever
>>>
a
uc

If a wrong statement is inserted, the interpreter shows an error


ed

message. This is very useful when you are learning to code, because
or

it allows you to verify immediately the correctness of a command


-f

before using it in a program. It is important to understand right


away that, to communicate with the machine through Python,
e
pl

we need to know and respect its rules. If we insert statements


m

that make sense, however simple, we will get a consistent result.


Sa

If we try instead to communicate in a way that makes no


sense to Python, we will get an error message. For example, if
we try to write simply  hello at the command prompt, we will
LEARNING PYTHON

get an error message (which will be unintelligible so far, but it


is the way Python has to tell us that we have not communicat-
ed according to its rules):
>>> hello
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
hello
NameError: name 'hello' is not defined
22

>>>
If we write, instead,  print('hello!')  we will get in re-
sponse  hello!. In this case, we wrote something that makes 2
sense to Python, and we got a consistent response:

Introduction to Python
>>> print('hello!')
hello!
>>>

We can try to write a simple arithmetic operation like  3+3, to


see that this is also a command that Python recognizes and to
which provides a response:
>>> 3+3
6

se
>>>

lu
In the shell, we can do all possible experiments. However, there
na
is no way to «clean up» the shell window, so if you want to start
tio
from a clean situation, the only way is to close it and reopen it
a

from scratch, or choose Shell → Restart Shell, or press Ctrl+F6


uc

on the keyboard.
ed

However, when you need to write a program, you have to use


the editor. The statements inserted in the shell, in fact, once
or

executed produce a result but they are not stored as a program.


-f

The editor allows you to save the statements in a file with a .py ex-
e

tension, which then can be run. Running the Python program


pl

will cause the interpreter to start in script mode (Figure 2.4): the
m
Sa

code will be read and run statement by statement, until the end
of the program. Several IDLE settings can be customized. The
command to access the settings is Options → Configure IDLE.
The Settings dialog box appears (Figure 2.5).
In the Fonts/Tabs tab, it is possible to configure, for exam-
ple, the font type and the font size used in the shell and in the
editor, or the width of the indentation (which is inserted in the
code by pressing the Tab key).
In the Keys tab, instead, it is possible to show (and eventu-
ally modify, if necessary) the key combinations (shortcuts) us-
able when programming in IDLE. It may be useful to take note
of some combinations, one out of all history-previous, which
23

corresponds to the Alt+P key combination and calls up the last


FigurE 2.4  The editor window

se
lu
na
tio

FigurE 2.5  The dialog box with the IDLE settings


a
uc
ed
or
-f
e
pl
m
Sa
LEARNING PYTHON
24
statements typed in the shell without having to rewrite it from
scratch (which will be very useful as soon as we get it a mini- 2
mum of confidence with Python).

Introduction to Python
In the Highlights tab, it is possible to display the colors as-
signed in IDLE to each element of the language:
XX built-in functions (i.e. Python’s default functions, such
as  print) are in purple;
XX strings are in green;
XX output is in blue;
XX errors are in red.
Moreover, among the elements that we will meet later, espe-
cially using the editor:

se
lu
XX Python keywords are in orange;
XX na
defined names, such as user-defined function names and
classes, are in blue;
tio

XX comments next to the lines of code are in red;


a
uc

XX comments outside the code lines are in green.


ed

Writing, saving When we want to open


or

2.4 the editor to write a new


-f

and running a program


program, we can use the
e

command File → New File from the shell, or alternatively press


pl
m

the key combination Ctrl+N.


Sa

The editor comes with a completely blank sheet, as if it were


a new Notepad file. The new file will have the default name «un-
titled» until it is saved with a name.
The two editor and shell windows remain open at the same
time (Figure 2.6). These are two different windows, and to use
one or the other we can for example show them side by side,
or bring to the fore only the one we need from time to time.
Now let us try to write our first simple program using the ed-
itor: a program made of a single statement that will return the
text «Hello World!». Once the editor is open, we can write the
source code of the program, which will consist of a single line:
25

print ('Hello World!')


FigurE 2.6  T
 he two windows of shell and editor open at the
same time

se
lu
na
tio
Once the code has been written, you can save the program on
a

a disc of your choice with the name hello_world.py, using the


uc

command File → Save As. When the file name contains several
ed

words, the underscore character (_) is commonly used to sep-


arate them. The command to save changes to a program, after
or

it has been saved for the first time, is the usual Save. In IDLE
-f

there is also the Save Copy As command, which works as Save As


e

but creates a second copy of the file leaving the original open.
pl
m

To run an open program in the editor, you can press the F5


Sa

key on the keyboard, or choose the Run → Run Module com-


mand. If the file had not been saved previously (both for the
first time and after any changes) IDLE will ask you to save it
LEARNING PYTHON

before continuing.
When a program is run, the output is displayed in the shell.
If, at the time of execution, Python detects a syntax error, a di-
alog box will appear with the  invalid syntax message. By
clicking OK in the dialog box, the editor will highlight the er-
ror location in the code. To check the syntax of a program be-
fore running it, in order to highlight any errors, you can choose
the Run → Check Module command.
If the saved program has been closed and you need to re-
26

open it for any changes, or to run it again, you need to locate it


in your computer’s resources, right-click on the file and choose
Edit with IDLE. N.B.: double-clicking on the file name does not 2
work, neither to open it nor to run it.

Introduction to Python
To open an existing program, the command (from the shell or
from the editor, indifferently) is File → Open.
It may happen, when writing a program, to write long
statements that become unreadable in the editor window. In
this case, it is possible to wrap within the code, for improved
readability. This will not produce any effect in the program
output. To do this, simply enter the character  \ (backslash)
in the code, where you want to wrap the text. The follow-
ing code, for example, is a single statement that will give
the result of the whole piece of text reported in the differ-

se
ent lines (the backslashes allow to break the code in more

lu
lines for greater legibility, where necessary, without affect-
ing the output): na
tio
print("I sing the body electric, \
a

the armies of those I love engirth me \


uc

and I engirth them, \


ed

they will not let me off till I go with them, \


respond to them, and discorrupt them, \
or

and charge them full \


-f

with the charge of the soul.")


e
pl

The result is the following:


m
Sa

I sing the body electric, the armies of those


I love engirth me and I engirth them, they
will not let me off till I go with them,
respond to them, and discorrupt them, and
charge them full with the charge of the soul.

First notions To learn a programming lan-


2.5 of Python syntax guage, you need to learn the
syntax and know the most im-
portant keywords and operators. The set of keywords, opera-
tors, punctuation and other elements allows writing the state-
ments of the language, which in the correct sequence lead to the
27

fulfillment of a specific task.


Each language has its own syntax, i.e. a set of rules that it
is necessary to know and follow strictly when writing a pro-
gram. Syntactic rules establish how language elements (key-
words, operators and various punctuation marks) can be used
to write a program.
For example, we have already mentioned the importance
of indentation in Python. This concept will become increasing-
ly clear as we will write more complex statements, grouped into
blocks, as functions, conditional constructs or loops. So far, we
just need to know that it is a topic that needs close attention:
the Python code indentations are never random and the or-
der and legibility deriving from this apparent rigidity are one
of Python’s winning features. For example, the following code,

se
although not fully intelligible (having not yet learned the  if-

lu
elif-else constructs and the variables) will still be readable
na
for an inexperienced eye, and this is due to the ease in recog-
tio
nizing each statement block, thanks to indentation:
a
uc

if age > 18:


print("Access denied")
ed

access = False
or

elif age > 80:


-f

print("Are you sure?")


access = False
e
pl

else:
m

access = True
Sa

One thing in which, instead, Python is not rigid is the man-


agement of spaces. Between keywords, operators, values and
brackets, as it will happen for the functions, it is possible to
LEARNING PYTHON

insert or not to insert a space. Python is able to interpret


the two scripts indifferently. Therefore, writing  3 + 3  is
equivalent to writing  3+3 ,  print ('John')  is the same
as  print('John') and the same is true in more complex sit-
uations. In general, however, it is preferable to use spaces for
better readability, even though it is not mandatory.
Each high-level language has its own set of predefined
words, called keywords or reserved words. Each keyword has
28

a specific meaning and cannot be used for other purposes


TABLE 2.1  Python keywords
2
and continue finally is raise

Introduction to Python
as def for lambda return
assert del from None True
async elif global nonlocal try
await else if not while
break except import or with
class False in pass yield

se
(for this reason it is called reserved). Table 2.1 shows all the
Python keywords: in the next chapters, we will discover the

lu
meaning of most of these keywords and use them in Python
na
statements.
tio
In addition to keywords, in programming languages there
a

are operators that perform various operations on data. For ex-


uc

ample, they can be mathematical operators like  +,  -,  *,  /, or


ed

other types. In Chapter 4, we will devote our attention to math-


ematical operators and calculations, but we already know that
or

Python is able to perform simple arithmetic operations with a


-f

very intuitive syntax. For example:


e
pl

>>> 37*6
m

222
Sa

>>> 24/4
6.0

It is important to note that in Python numbers follow the An-


glo-Saxon notation, so they use the dot as a decimal separator.
In Chapter 3, dedicated to calculations, we will focus on why
the result of division  24/4 is  6.0 and not just 6.
In addition to numbers, it is important to understand how
to handle text strings. We will deal with string features more
precisely in Chapter 3, when we will analyze the different types
of data managed by Python. Now, it is essential to learn how to
use strings in the most common cases. In Python it is necessary
29

to enclose strings with quotes (' ') or quotation marks (" ").
>>> print('John Snow')
John Snow
>>> print("John Snow")
John Snow

If it is necessary for a string to contain quotes or an apostro-


phe, it must be enclosed in quotation marks:
>>> print("The ship has weather'd every rack")
The ship has weather'd every rack

In the case, less common but still possible, in which the string
contains in turn quotation marks, it is necessary to enclose it
in quotes:

se
>>> print('You have to read "Macbeth" and

lu
write a summary by tomorrow!')
na
You have to read "Macbeth" and write a
tio
summary by tomorrow!
a
uc

Three quotes or three quotation marks can be used to enclose


strings that span multiple lines, something that cannot be done
ed

with single quotes or quotation marks. This is often used for


or

the documentation string of a program, that is a string placed at


-f

the beginning of the code that describes the objectives and/or


e

the general operation, by way of documentation (we will dis-


pl

cuss it in § 2.6).
m

It is not possible to perform calculations using strings, even


Sa

if their content is or appears numeric:


>>> '3' - '2'
LEARNING PYTHON

Traceback (most recent call last):


File "<pyshell#3>", line 1, in <module>
'3' - '2'
TypeError: unsupported operand type(s) for -:
'str' and 'str'

However, there are two operators that work with strings, al-
though their meaning changes:
XX the  + operator, used on strings, performs a concatenation;
30

XX the  * operator, used on strings, performs a repetition.


Example:
>>> print('super' + 'man')
2

Introduction to Python
superman
>>> print('Python' * 4)
PythonPythonPythonPython

In the examples seen so far we have encountered Python error


messages several times. The related specifications and how to
handle errors will be the subject of Chapter 10, but it is necessary
to understand something about the messages that Python will
always return to us when we write something wrong. The main
cases we may meet in our first programming approaches are two:
a syntax error during writing or an error during program execution.

se
The syntax error occurs when we do not respect one of Py-

lu
thon syntax rules, and shows the error message SyntaxEr-
ror: invalid syntax. For example, if writing a  print func-
na
tion we inadvertently add a second closed or open parenthe-
tio
sis, it is enough for Python to return a syntax error message:
a
uc

>>> print('Mickey Mouse'))


ed

SyntaxError: invalid syntax


or

Even if an instruction or expression is syntactically correct, it


-f

may cause an error when trying to run it. Errors detected dur-
ing execution are called exceptions and are not always fatal: in
e
pl

Chapter 10 we will learn how to manage them in Python pro-


m

grams. Most exceptions, however, cause error messages, like the


Sa

ones we have already seen (starting with  Traceback (most


recent call last)). Example:
>>> 3/0
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
3/0
ZeroDivisionError: division by zero

The last line of the error message indicates what happened. Ex-
ceptions are of different types, and the type is shown as part
of the message (for example  ZeroDivisionError,  NameEr-
ror or  TypeError) along with a brief explanation (in the ex-
ample:  division by zero).
31
Comments When the program grows in size and
2.6 and docstring becomes more complex it is a good
habit to add comments, which serve
to document the various steps of the program.
Comments are short notes, which can be inserted at differ-
ent points in a program and explain how the statements work
or what objectives they have. Comments can be entered in two
ways:
XX when they follow a line of code, only by preceding them with
the symbol  #;
XX when they are between several lines of code or at the begin-
ning of the program, in addition to the # symbol, insert-

se
ing them between triple quotation marks (""" """) or triple

lu
quotes (''' '''). In this case, the comments can also go over
several lines. In the case of a comment made in this way, po-
na
sitioned at the beginning of the program or at the beginning
tio
of a custom function we speaks (as we will see in Chapter 7)
a

of a documentation string.
uc
ed

It is important to get used to writing comments because they


are a great help to modify the program later or in case of mal-
or

function and consequent debugging.


-f
e
pl

Escape codes (also known as escape


2.7 Escape codes
m

sequences or escape characters) are


Sa

special commands consisting of a


character preceded by a backslash (\), placed inside a string.
When the string is displayed on the screen, these commands
LEARNING PYTHON

perform a specific action: for example, wrapping the characters


of the string in precise points, or aligning different parts of the
string to tabulations.
The most common escape code is  \n, which is used to wrap
text in the output:
>>> print('Ready\nSteady\nGo!')
Ready
Steady
32

Go!
The code  \t, instead, aligns the text to the tabulation:
>>> print('String \t with tabulator')
2

Introduction to Python
String with tabulator

This can be useful, in a program, when you want to align ele-


ments of different strings to the same point.
There are other escape codes, which are also used to display
particular characters such as backslashes, quotes or quotation
marks in the output of the  print function. Table 2.2 shows a
list of the most common escape codes.

TABLE 2.2  The most common escape codes

se
Escape Description Example Result

lu
code

\\ Backslash (\) print("\\")


na \
atio

\' Quote (‘) print('\'') '


uc

Double quote
ed

\" print("\"") "


(“)
or

Hello
-f

\n Wrap text print("Hello\nWorld!")


World!
e
pl

Hello
print("Hello\tWorld!")
m

\t Tabulation World!
print("Bye\tbye!")
Sa

Bye bye!

A function is a piece of pre-pack-


2.8 Built-in functions aged code that performs a cer-
tain operation. Python has sev-
eral built-in functions that perform various operations. The first
and most common of the built-in functions is the  print func-
tion, which we have already encountered in several examples.
This function displays an output on the screen. In the shell, en-
tering this instruction and pressing the Enter key, the value en-
33

tered between the brackets is displayed:


>>> print('hello!')
hello!

The name of the function in IDLE is displayed in purple, while


the text string between quotes (or in quotation marks) in green
and the output in blue, according to the color codes described
in § 2.3.
We can see that function names are always written in low-
er case. In fact, if we tried to write Print or  PRINT instead
of  print, we would get an error message:
>>> Print('hello!')
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>

se
Print('hello!')

lu
NameError: name 'Print' is not defined
na
The flexible management of Python spaces allows instead writing
tio
the first bracket attached or detached from the  print function.
a

We can notice (Figure 2.7) that typing  print( the call tip


uc

appears with the syntax of the function (which will be discussed


ed

in detail in Chapter 7, entirely dedicated to functions). If they


or

are not visible, call tips can be activated with the Edit → Show
-f

Call Tip command.


The syntax shows the different parameters of the function,
e
pl

separated by a comma (which in Python is the character of sep-


m

aration between the parameters of the functions). The first is


Sa

the  value parameter, which is the value that will be returned


as an output. For our first examples, we are using only sim-
ple strings, but continuing we will use the  print function in
LEARNING PYTHON

more sophisticated ways, inserting among its parameters cal-


culations, variables and other functions. One or several values
(even of different types) can be inserted separated by a com-
ma: the  print function converts them into strings, and shows
them in outputs separated by a space and followed by a new-
line character:
>>> print('I', 'am', 'me')
I am me
34

>>>
FigurE 2.7  T
 he print function with the call tip that
describes its syntax 2

Each parameter, if it is a string, must be included in quotes


or quotation marks. In fact, the following function produc-

se
es a different output (not three different texts but a single

lu
text string):
na
>>> print('I, am, me')
tio
I, am, me
a

>>>
uc
ed

Among the parameters of the  print function is the  sep pa-


rameter, which indicates which separator must be returned
or

between the various texts in output. The default is space


-f

(sep=' '), but it could be any other character. The  sep pa-


e

rameter must be indicated, always after a comma, at the end


pl

of the  value parameters. For example, we can test it with


m

the hyphen:
Sa

>>> print('I', 'am', 'me', sep='-')


I-am-me
>>>

In addition to the  print function, there are many other func-


tions immediately available in Python. Table 2.3 shows all Py-
thon’s built-in functions. Many of them will be described in the
next chapters: in Chapter 3, for instance, we will see different
functions related to mathematical calculations; in Chapter 4
we will explain another very important and commonly
used function: input. In § 2.9 we will introduce the  help 
35

function.
TABLE 2.3  Python’s built-in functions

abs() issubclass()
all() iter()
any() len()
ascii() list()
bin() locals()
bool() map()
breakpoint() max()
bytearray() memoryview()
bytes() min()
callable() next()

se
chr() object()

lu
classmethod() oct()
compile() open()
complex()
na
ord()
tio
delattr() pow()
a

dict() print()
uc

dir() property()
ed

divmod() range()
or

enumerate() repr()
-f

eval() reversed()
exec() round()
e
pl

filter() set()
m

float() setattr()
Sa

format() slice()
frozenset() sorted()
getattr() staticmethod()
LEARNING PYTHON

globals() str()
hasattr() sum()
hash() super()
help() tuple()
hex() type()
id() vars()
input() zip()
int() __import__()
36

isinstance()
2.9
Help and other
kinds of support
Python provides some support
tools for experienced and non-ex- 2
pert programmers. Among these

Introduction to Python
is the  help function, which is used in interactive mode (in the
shell) and shows information about a function, a data type or a
module. It can be used whenever we have a doubt about one of
these elements and we want to learn more. We can try to use
it on the  print function: we will get information on its syn-
tax and its parameters:
>>> help(print)
Help on built-in function print in module
builtins:

se
print(...)

lu
print(value, ..., sep=' ', end='\n',
file=sys.stdout, flush=False) na
tio
Prints the values to a stream, or to sys.
a

stdout by default.
uc

Optional keyword arguments:


ed

file: a file-like object (stream);


defaults to the current sys.stdout.
or

sep: string inserted between values,


-f

default a space.
e

end: string appended after the last


pl

value, default a newline.


m

flush: whether to forcibly flush the


Sa

stream.

The same obviously applies to any built-in Python function


and, as we will see in Chapter 7, also for custom functions cre-
ated by the user.
We can also try out  help on itself:
>>> help(help)
Help on _Helper in module _sitebuiltins
object:

class _Helper(builtins.object)
| Define the builtin 'help'.
37

|
| This is a wrapper around pydoc.help that
provides a helpful message
| when 'help' is typed at the Python
interactive prompt.
|
| Calling help() at the Python prompt
starts an interactive help session.
| Calling help(thing) prints help for the
python object 'thing'.

The penultimate line informs us that it is also possible to acti-


vate an interactive help in the shell by typing  help() and then
choosing between the various topics and sub-topics available:

se
>>> help()

lu
Welcome to Python 3.7's help utility!
na
If this is your first time using Python, you
should definitely check out
tio
the tutorial on the Internet at https://docs.
a

python.org/3.7/tutorial/.
uc
ed

Enter the name of any module, keyword, or


topic to get help on writing
or

Python programs and using Python modules. To


-f

quit this help utility and


e

return to the interpreter, just type "quit".


pl
m

To get a list of available modules, keywords,


Sa

symbols, or topics, type


"modules", "keywords", "symbols", or
"topics". Each module also comes
with a one-line summary of what it does; to
LEARNING PYTHON

list the modules whose name


or summary contain a given string such as
"spam", type "modules spam".

help>

The  help function allows, among other things, to have a list of


Python keywords, with the  help ('keywords') statement.
In addition to the  help function, Python provides exten-
38

sive documentation, which is installed along with the program


FigurE 2.8  T
 he documentation program that is installed
with Python 2

se
lu
na
a tio
uc

itself. In particular, there is a program called Python 3.x Manu-


ed

als (Figure 2.8) which includes a navigable version of the com-


or

plete documentation on using Python.


-f

In addition to this, on the Web you can find all kinds of in-
formation, including parts of code ready for use. For informa-
e
pl

tion on Python, it is always advisable to refer to the official web-


m

site www.python.org.
Sa

39
3
Calculations and data output

Math operators Most of the algorithms that come


3.1 and calculations from real-world problems require
you to perform calculations: to ap-
proach computer programming it is therefore necessary to intro-
duce some rules concerning numbers and mathematical opera-

se
tors that allow to perform calculations in Python.

lu
In calculations, numbers (called operands) and mathematical
na
operators are involved. Python provides a series of mathemati-
tio
cal operators that allow to perform the most common arithme-
a

tic calculations. Table 3.1 contains a list of them.


uc
ed

TABLE 3.1  Python math operators


or

Symbol Operation Description


-f

+ Addition Adds two numbers


e
pl

- Subtraction Subtracts one number from another


m

* Multiplication Multiplies one number by another


Sa

Divides one number by another and returns


/ Division
the result as a floating-point number
Divides one number by another and returns
LEARNING PYTHON

// Integer division the result as whole number (without


remainder)
Divides one number by another and returns
% Remainder
the remainder
** Exponent Raises a number to a power

As we said in Chapter 2, in Python numbers follow the An-


glo-Saxon notation, so they use the dot as a decimal separa-
tor. Here are some simple examples of calculations, performed
40

in the shell:
>>> 8-4
4 3
>>> 7*6

Calculations and data output


42
>>> 30/6
5.0

As you can see, the result of  30/6 is  5.0 and not 5, as the di-
vision operator always returns a decimal number of the float-
ing-point type (data types will be deepened in the Chapter 4).
If we want to get from two integers an integer result (without
decimals, but also without remainder) we have to use the inte-
ger division operator  //.

se
>>> 30//6

lu
5
>>> 30//7 na
tio
4
a

Let us see another example, which allows us to consider how


uc

Python uses rounding:


ed
or

>>> 40/6
6.666666666666667
-f

>>> 40//6
e

6
pl
m

Using the  //  operator, when the result is a positive num-


Sa

ber it is truncated to the lower integer, i.e. the decimal part is


simply eliminated. When the result is a negative number, it is
rounded down to the nearest whole number. If you use the di-
vision operator (/) and the result is a floating-point number,
the maximum number of decimals shown is 16, even in the
case of a periodic decimal. For instance, 1/3 is equal to 3.3 pe-
riodic, but in Python  3.3333333333333333 (with 16 deci-
mals) is returned while 40/6 is equal to 6.6 periodic, but in Py-
thon  6.666666666666667 is returned, with 15 decimal dig-
its and final rounding. Rounding in Python is difficult to inter-
pret, since it refers to a binary approximation system that does
41

not always give predictable results.


The remainder operator (%) returns the remainder of the di-
vision operation between two integers. You can use it to check
if an integer is divisible by another, that is if the division be-
tween the two numbers gives 0 as the remainder:
>>> 5 % 2
1
>>> 4 % 2
0

Parentheses and A mathematical expression


3.2 operator precedence is a sequence of numbers,
mathematical operators

se
and parentheses that performs a calculation and returns a

lu
value. An expression can contain both numbers and vari-
na
ables that represent numbers (a concept that we will explore
in Chapter 4). Therefore, all the following can be considered
tio

expressions:
a
uc

12 + 2
ed

(7+3)**2*20.5
or

worked_hours * pay_rate
-f

Where  worked_hours and  pay_rate are variables that rep-


e

resent numbers (for instance the number of hours worked and


pl

the hourly pay of an employee, whose salary is to be calculated).


m

In Python’s mathematical expressions, some rules concern-


Sa

ing the precedence of operators and brackets are respected. In


particular:
LEARNING PYTHON

XX the operations enclosed in brackets are performed first;


XX subsequently, when two operators share the same operand,
the operator with the highest precedence is applied first in
the following order:
—— exponentiation: **;

—— multiplication and division: *, /, // and % (remainder);

—— addition and subtraction: +, -.

The multiplication and division operators, as well as addi-


42

tion and subtraction ones, have the same level of precedence


among them. When two operators with the same precedence
level share the same operand, the operations are performed 3
from left to right. To remember the order it is possible to use

Calculations and data output


the acronym PEMDAS: Parentheses, Exponentiation, Multi-
plication, Division, Addition, and Subtraction. Here are some
examples:
XX parentheses:
>>> 5 * (3 - 1)
10
first,  3-1 is computed and then the result (2) is multiplied
by  5;
XX exponentiation:

se
>>> 3 * 2 ** 2

lu
12
na
first,  2**2 is computed and then the result (4) is multi-
tio
plied by  3;
a

XX multiplication and division, addition and subtraction:


uc

>>> 1 + 2 * 3
ed

7
or

first,  2*3 is computed and then the result (6) is added to  1.
-f

Operators with the same priority are computed from left to


e
pl

right, except for exponentiation, which is performed from


m

right to left:
Sa

>>> 2**3**2 → first computes 32 = 9, and then


51229 = 512

>>> (2**3)**2 → first computes 23 = 8, and then


6482 = 64

In the first case, in absence of parentheses, we note that the


result,  512 , corresponds to  2**9 , or  2**(3**2) . On the
other hand, if we want to get  8**2, that is  64, we must en-
close  2**3 in brackets.
43
When using mathemati-
3.3 Mathematical formulas cal notation in a calcula-
tion, it is not always nec-
essary to make operators explicit. For example, writing 3xy im-
plies the multiplication operator, but it is clear that the mean-
ing corresponds to 3*x*y. In Python, as in many other pro-
gramming languages, it is always necessary to make operators
explicit for calculations. Moreover, sometimes in algebraic ex-
pressions brackets are omitted in writing, while in Python it is
necessary to make them explicit. For example, consider the fol-
lowing expression:
2a + b
x=

se
c

lu
To convert it into a Python statement it is necessary to make
na
the  * operator explicit and add the brackets to enclose the nu-
tio
merator:
a
uc

x = (2*a+b)/c
ed

In Chapter 2, we saw how to break down a particularly long


or

statement by wrapping the code. This is always valid, even with


-f

mathematical expression. For instance:


e

tot = val1 * 2 + val2 *3 + val3 * 4 \


pl

+ val 5 * 6 + val 6**2 + val7 * val8 \


m

+ (val9 + val10) / 2
Sa

So far, we have only encountered very


3.4 Data output basic ways to visualize data, but often
we will need to be able to have more
subtle control over how data are displayed on the screen. Now
we will look at some simple output formatting techniques.
We are not always satisfied with the appearance of the num-
bers displayed on the screen, especially in the case of floating-
point numbers, as can be seen from the example:
>>> 1000/7
44

142.85714285714286
We can round the output to the second decimal, using the  for-
mat function: 3

Calculations and data output


>>> format(1000/7, '.2f')
'142.86'

As you can see, the  format function has two arguments:


XX the number to be formatted, in this case the output of a di-
vision;
XX the format specifier, in this case  '.2f', where  .2 indicates
that you want to round the number to the second decimal,
while  f specifies that the format we want to obtain is that
of floating-point numbers (float).

se
The fact that the result of the  format function appears in

lu
quotes indicates that it is a string. If we try to insert a  for-
na
mat function inside a  print, the result will be displayed with-
tio
out quotes, but it is only a difference in display (the  print func-
a

tion produces an output classified as «noType», regardless of


uc

the type of data contained in the arguments of the function):


ed

>>> print(format(1000/7, '.2f'))


or

142.86
-f

Instead of the  f  letter, as a format specifier you can use


e

the  % symbol, which is used to format a floating-point number


pl
m

as a percentage. The  % symbol multiplies the number by 100


Sa

and displays a percentage symbol immediately after it. Here


are some examples:
>>> print(format(5/10, '%'))
50.000000%
>>> print(format(5/10, '.2%'))
50.00%
>>> print(format(5/10, '.0%'))
50%

In all three examples, the output of the division (0.5) is format-


ted as a percentage: in the first case the number of decimals is
not specified, in the second case the output is limited to two
45

decimals and in the third case to zero decimals.


To format a number with commas as thousands separa-
tor, it is possible to insert a comma in the format specifier, as
shown in the example:
>>> print(format(123456.789, ',.2f'))
123,456.79

In this case the  format function inserts the thousands sepa-


rator (,) and simultaneously formats the number to two deci-
mal places.
It is possible to insert the thousands separator, with
the  format function, also in an integer. In this case, the syn-
tax includes the letter  d instead of  f.

se
>>> print(format(123456, ',d'))

lu
123,456
na
tio
In Python there are
3.5 Functions for calculations
a

several built-in func-


uc

tions dedicated to
ed

mathematical calculations. Here are some examples.


or

XX sum function: it calculates the sum of a list of elements in-


-f

dicated in square or round brackets separated by a comma:


e
pl

>>> sum([1, 2, 3, 4, 5])


m

15
Sa

>>> sum((1, 2, 3, 4, 5))


15
As we will see, in Python a comma-separated list of elements
LEARNING PYTHON

enclosed in square brackets is a very specific object called list


(§ 8.4), while a list enclosed in round brackets is called tuple
(§ 9.1). Both these objects are sequences and are part of the
objects called iterable. We will deepen these concepts in the
following chapters;
XX pow function: calculates the elevation of a number to a pow-
er. For example, 25 will be:
>>> pow(2, 5)
46

32
The result is equivalent to the use of the exponent opera-
tor already seen: 3

Calculations and data output


>>> 2**5
32
XX abs function: calculates the absolute value of a number:
>>> abs(-7)
7
XX round function: rounds a floating-point number to the dec-
imal digit indicated as the second parameter:
>>> round(3.376, 0)
3.0

se
>>> round(3.376, 1)

lu
3.4
>>> round(3.376, 2) na
3.38
tio
>>> round(3.376, 3)
a

3.376
uc

XX max and min functions: return the maximum and minimum


ed

value of a series of single parameters (separated by commas)


or

or a list or tuple of values (therefore between square or round


-f

brackets, separated by commas), or a string:


e

>>> max(30, 10, 20)


pl

30
m

>>>min(30, 10, 20)


Sa

10
>>> max([12, 58, 87, 43, 21])
87

However, among the built-in functions, only a few are dedi-


cated to basic calculations. If we wanted to do a more complex
calculation, e.g. to calculate the average of a list of values, we
would not find a dedicated function among built-in Python
functions, which only allow to sum the values and divide them
by the number of values:
>>> (12 + 35 + 27 + 16) / 4
47

22.5
In addition to built-in functions, we will discover that there are
other types of functions: those that are part of the standard Py-
thon library (they are installed with Python, but must be im-
ported in order to be used), those that are part of third-par-
ty libraries (which must be installed and then imported before
they can be used) and the custom functions created by the user.
We will deepen the theme of custom functions in Chapter 7,
while libraries will be the subject of Chapter 11. However, we
can mention the functions of the standard library, since some
of them are widely used and may be useful in future examples.
The standard library is a set of additional modules that are
installed together with Python to perform specific tasks, but are
not immediately active. This means that when Python is load-

se
ed into memory (when the program starts) only the most com-

lu
mon functions are loaded, so as not to overload the computer’s
na
memory with functions that are not needed. When a particular
tio
function is needed, which is part of a module of the standard
library, it is necessary to import that module into the current
a
uc

session, to make its functions available. For instance, one of the


ed

additional modules of the commonly used standard library is


statistics, which contains numerous functions intended for sta-
or

tistical analysis (the  mean function for calculating the average


-f

of a list of values,  median for the median,  mode for the mode


e

etc.). To import a module into the current session you need


pl

to use the  import command, followed by the module name.


m

Once imported, you can use its functions in the current shell
Sa

session (until it is closed) or in the entire current program, by


typing  module_name.function_name. The calculation of the
average can therefore be carried out as follows.
LEARNING PYTHON

>>> import statistics


>>> statistics.mean([12, 35, 27, 16])
22.5

Another module of the standard library is math, which includes


several mathematical functions otherwise not available in Py-
thon. For example, the function  sqrt of the math module re-
turns the square root of a number, while the  pi function sup-
48

plies the value of the mathematical constant π:


>>> import math
>>> math.sqrt(4) 3
2.0

Calculations and data output


>>> math.pi
3.141592653589793

The math module also includes the  sin,  cos, and  tan func-
tions that calculate the sine, cosine and tangent values, re-
spectively.
Another commonly used module, in the context of calcu-
lations, is random, which allows generating random numbers.
The  random() function of the random module generates a ran-
dom floating-point number between 0 and 1.

se
>>> import random

lu
>>> random.random()
0.5078906679277326 na
tio
The  randint(a,b) function generates an integer random
a

number between a and b, extremes included.


uc
ed

>>> random.randint(0,5)
4
or
-f

For further examples and insights on the standard library mod-


e

ules, see Chapter 11 where we will also learn about some third-
pl

party libraries, which further expand the capabilities of Python.


m

In Chapter 7 we will learn to build custom functions, which al-


Sa

low to perform any calculation in Python, even extremely com-


plex, thanks to a function built specifically by the user.
To conclude this chapter we can remember that the  help 
function, already seen in § 2.9, also provides information on
the modules, so it can be useful to know what functions are
contained in the modules of Python standard library. To use
the  help function on a module, you must first import it. Look
at the following example:
>>> import math
>>> help(math)
Help on built-in module math:
49

NAME
math
DESCRIPTION
This module is always available. It
provides access to the
mathematical functions defined by the C
standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in
radians) of x.
acosh(x, /)
Return the inverse hyperbolic cosine
of x.

se
If we want to have only a list of the functions contained in a

lu
module, we could use the function  dir (useful, as  help, most-
na
ly in the shell) instead. This function will also be taken up and
tio
examined in Chapter 11.
a
uc

>>> import math


ed

>>> dir(math)
['__doc__', '__loader__', '__name__', '__
or

package__', '__spec__', 'acos', 'acosh',


-f

'asin', 'asinh', 'atan', 'atan2', 'atanh',


'ceil', 'copysign', 'cos', 'cosh', 'degrees',
e
pl

'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs',


m

'factorial', 'floor', 'fmod', 'frexp', 'fsum',


Sa

'gamma', 'gcd', 'hypot', 'inf', 'isclose',


'isfinite', 'isinf', 'isnan', 'ldexp',
'lgamma', 'log', 'log10', 'log1p', 'log2',
'modf', 'nan', 'pi', 'pow', 'radians',
LEARNING PYTHON

'remainder', 'sin', 'sinh', 'sqrt', 'tan',


'tanh', 'tau', 'trunc']

The  dir function can also be used to list Python’s built-in func-


tions, using the syntax  dir(__ builtins__), with two un-
derscores both before and after  builtins.
50
4
Variables and data types

Variables and assignment Programs store data in


4.1 statements the computer’s memo-
ry and perform opera-
tions on them. To access and use data stored in memory, pro-

se
grams use variables. A variable is a noun that represents a val-
ue in the computer’s memory (in such cases, it is said that the

lu
variable references such value): it is like a container, with the
na
value’s name labelled on it, in which a value is stored. To create
tio
a variable and make it reference specific values, an assignment
a

statement is used. The assignment of a value to a new variable


uc

is also called initialization. The following statement creates a


ed

variable called «width» and assigns it a value of 10 (we can say


that the variable «is initialized» to value 10). The equals sign
or

(=) is called assignment operator.


-f
e

>>> width = 10
pl
m

Variables can be used in the programming statements to repre-


Sa

sent the value they reference, and such value may change dur-
ing the execution of the program depending on various factors,
as we will see later.
Variables are extremely useful when used as arguments
of functions. In the example below, the value of the vari-
able  width is passed to the  print function.
>>> print(width)
10

Obviously, the  print function will be able to return a result


only if the variable has been introduced into the code previ-
ously, assigning it a value. If we try to use the name of a vari-
51
able that has not been previously initialized, we will obtain an
error in return.
>>> print(height)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
print(height)
NameError: name 'height' is not defined

Python’s error message indicates that the problem is due to the


fact that the name  height has not been defined yet, therefore
it cannot be used.
We are now creating three variables: teacher_sur-
name,  num_presents and  temperature, assigning each of

se
them a value:

lu
>>> teacher_surname = 'Smith' na
>>> num_presents = 86
tio
>>> temperature = 22.5
a
uc

In the first case, a string is assigned to the variable  teacher_


ed

surname; in the second case, the integer number  86 is assigned


to the variable  num_presents, and in the third case the deci-
or

mal number  22.5 is assigned to the variable  temperature.


-f

When a variable is used, its name must not be put in quotes


e
pl

or quotation marks, otherwise it will be considered as a string


m

and it will not show the corresponding value:


Sa

>>> print(teacher_surname)
Smith
>>> print('teacher_surname')
LEARNING PYTHON

teacher_surname

On the other hand, if we write  Teacher_surname  (with-


out quotes or quotation marks, but with a capital T) with-
in the  print function, an error would result since the vari-
able  Teacher_surname does not exist.
>>> print(Teacher_surname)
Traceback (most recent call last):
52

File "<pyshell#3>", line 1, in <module>


print(Teacher_surname)
NameError: name 'Teacher_surname' is not 4
defined

Variables and data types


Python is indeed a case-sensitive language, distinguishing between
upper and lower case characters:  JOHN,  john, and  John all
are different names. It must be noted that by convention only
lower case letters should be used for the names of variables.
In an assignment statement, the variable that receives the
assignment must be inserted on the left of the assignment op-
erator, i.e. the equals sign; if the element on the left of the op-
erator  = is not the name of a variable, an error occurs:
>>> 22.5 = temperature

se
SyntaxError: can't assign to literal

lu
na
As we will examine shortly, it is not possible to use 22.5 as the
name of a variable and therefore an error occurs.
tio
It is possible to insert into the arguments of functions vari-
a

ables, strings, numbers or other data types, compatibly with


uc

the data types accepted by different arguments, as in the ex-


ed

ample below:
or

>>> print('Teacher surname:', teacher_


-f

surname)
e

Teacher surname: Smith


pl
m

Both arguments of the  print function are returned one after


Sa

the other, single-spaced.


As we will see later, variables are commonly used as argu-
ment of functions, from the simplest to the most complex ones.

Variables naming When defining the name of a


4.2 conventions variable, it is important to con-
sider some conventions that
stem both from common sense and from Python’s syntactic rules.
First, it is important that variables be assigned significant
names, in order to record what they are and what they are used
for. For example, a variable that must memorize velocity could
53

be called  vel, whereas another variable that measures temper-


ature could be called  temperature. Names such as x, y, a or b
do not give information about the nature and scope of the vari-
able and could make the code difficult to read, in a basic pro-
gram; also, every time one needs to use a specific variable, it
could be complicated to retrieve the correct name. At the same
time it is advisable that short names are used, and not too com-
plex, to avoid (or reduce) the risk of misspelling.
Moreover, names of variables must follow some fundamen-
tal rules:
XX must begin with a letter or the underscore symbol (_), fol-
lowed by letters, numbers or underscores;
XX must not contain spaces nor non-standard characters (for

se
example @, &, $, €, § etc.);

lu
XX must not be among Python keywords.
na
It is preferable that they do not contain accented characters.
tio
Keeping in mind that in assignment statements the name
a

of the variable is on the left, here are some examples of syn-


uc

tax errors:
ed

>>> 4weddings = 'film'


or

SyntaxError: invalid syntax


-f

4weddings is not admitted because it does not begin with a


e
pl

letter or an underscore.
m
Sa

>>> john.snow@outlook.com = 'email'


SyntaxError: can't assign to operator

john.snow@outlook.com is not admitted because it contains


LEARNING PYTHON

a non-standard character (@).


>>> break = 5
SyntaxError: can't assign to keyword

break is not admitted because it is one of Python keywords


(we have seen the complete list in Table 2.1).
break is a keyword, but Break is not, keeping in mind that
Python is case-sensitive. This means that, in theory we could
54

use Break (with a capital B) as name of a variable:


>>> Break = 3
>>> print(Break) 4
3

Variables and data types


Even though it is technically possible to use a word that is so
similar to a keyword, it is not advisable since it could make the
comprehension of the code much more difficult, causing mis-
understanding and mistakes. We also remind that by conven-
tion all the names of variables are lower case.
Despite the possibility to name a variable with a name of a
function, this operation must be avoided. From that moment,
the function loses its properties. See the example below:
>>> print(5)

se
5

lu
>>> print = 6
na
This latter instruction creates a variable called  print, assign-
tio
ing it a value of 6. From now on, every time the program is run
a

or in the current session of the shell, the name «print» will no


uc

longer refer to the function we have used so far, but to the val-
ed

ue 6. Indeed, if we type the name of the variable, the assigned


or

value will be returned, but if we try to use the  print function,


-f

an error will be returned instead.


e

>>> print
pl

6
m

>>> print(5)
Sa

Traceback (most recent call last):


File "<pyshell#3>", line 1, in <module>
print(5)
TypeError: 'int' object is not callable

As we can notice, after creating and assigning a value to


the  print variable,  print cannot be used as a function any-
more. To make the function work again, it is necessary to close
and reopen the shell, or choose Shell → Restart Shell, or press
Ctrl+F6 on the keyboard.
It is possible to use names of variables made up of more than
one word and, keeping in mind that it is not possible to insert
55

spaces in the names of variables, we could:


XX space words with an underscore;
XX merge the words together (but in this case they may diffi-
cult to read);
XX merge words capitalizing the first letter of each word, start-
ing from the second (but, as already mentioned, it is advis-
able not to use upper-case letters for names of variables).
For example, the name of a variable that must contain the to-
tal value can be written in three styles:  total_value or  to-
talvalue or  totalValue. The first style is advisable, since
it is the clearest one (and the most common).

Variables are called this way because

se
4.3 Reassignment they may reference different values

lu
during the execution of a program.
na
When a variable is assigned a value, it references such value un-
til another value is assigned:
a tio

>>> price = 100


uc

>>> price
ed

100
>>> price = 90
or

>>> price
-f

90
e
pl

In this example, a  price variable is created and assigned ini-


m

tial value of  100. Subsequently another value (90) is assigned


Sa

to the same  price variable. The instruction  price = 90 is


a reassignment operation: the variable, assigned a certain val-
ue (100), is reassigned a value 90. The old value no longer ref-
LEARNING PYTHON

erenced by any variable is removed from the computer’s mem-


ory through a process called garbage collection.
In addition to the replacement of the value we have just seen
(value 100 is replaced by 90), update is one of the most common
types of reassignment, where the new value of the variable de-
pends on the old one.
>>> a = 1
>>> a
56

1
>>> a = a + 1
>>> a 4
2

Variables and data types


The variable is initialized to value 1; subsequently it is increased
by 1 (a = a + 1), therefore the new value of  a is 2.
Before updating a variable, its initialization (i.e. assigning
it a value at least once) is fundamental, since updating a non-
defined variable generates an error, as in the example below:
>>> b = b + 1
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
b = b + 1

se
NameError: name 'b' is not defined

lu
Python admits assigning variables multiple values (also called
na
unpacking), which allows to initialize or reassign more than one
tio
variable with a single code line. In the following example, three
variables called  alpha,  beta and  gamma, are initialized to the
a
uc

values  17,  22 and  34 respectively:


ed

>>> alpha, beta, gamma = 17, 22, 34


or

>>> alpha
17
-f

>>> beta
e

22
pl

>>> gamma
m

34
Sa

When we communicate in the real


4.4 Data types world, we use a mass of data but usual-
ly we do not need to think about what
data type we are using. However, in computer science this is
fundamental. Let us now consider the following sentence: «On
12/10/2018 student John Smith paid the first university tu-
ition fee, for a total of 1,234 euros». This sentence includes at
least three data types:
XX a date (12/10/2018);
XX a text string (John Smith);
57

XX a number (1,234).
Usually we do not think about the fact that a name consists of
characters, the date and the amount of money consist of num-
bers, and so on. When we use data in a programming language,
we must consider the data type we are using in order to know
what we can do: data management implies rules that depend on
the specific language we are using, even though some of these
rules and logic are common to most high-level languages. For
this reason, it is important to understand data types and the re-
lated rules and usage: what we are going to learn in Python will
allow us to perform all the fundamental operations with data
and will constitute the starting point for programming. Most
of the definitions and logic we are going to learn can be applied
to all programming languages, for instance VBA, Java, R, Sta-

se
ta and Matlab, but also will be useful for the management of

lu
database and other IT tools related to data (including big data
management tools). na
tio
In § 1.2, we saw the way computers memorize data, and talk-
a

ed about conversion processes from various data types into bi-


uc

nary code. Starting from the conversion process, each data type
ed

(whole or integer number, decimal number, text character or


symbol etc.) is managed differently. This principle especially
or

applies to a programming language in which each data type is


-f

used according to specific rules. The data type used in the pro-
e

gramming statements is fundamental, since some operations


pl

are performed differently according to the data type, whereas


m

other can only be performed on values of a specific data type.


Sa

This is the reason why, in programming languages, typing ex-


ists, i.e. the management of data that varies according to their
type. In some languages, data must be indicated explicitly with
LEARNING PYTHON

the data type category they belong to, while in others (such as
Python) this happens automatically (in this case we talk about
dynamic typing).
The concept of variables, introduced at the beginning of this
chapter, is closely connected with data typing, which will now
be taken into account. In programming languages, data typing
can be either static or dynamic. In the first case, the program-
mer must declare the variable type before using it, while in dy-
58

namic typing the interpreter (or compiler) is the one who de-
cides the variable type, according to the value assigned to it.
Python uses dynamic typing, therefore specifying the type of 4
the variables is not necessary: variables assume it automatical-

Variables and data types


ly depending on the value assigned. The following code is per-
fectly valid in Python: in each line, a different value is assigned
to the variable  x. The first statement initializes  x to value 10,
the others reassign the value  'Python' and  [1, 2, 3] to
the same variable. Every time the data type of the variable is
changed automatically.
x = 10
x = 'Python'
x = [1, 2, 3]

se
The first value assigned to  x is obviously a whole number (or

lu
integer), the second is a string, and the third is a complex data
na
type (called list), which we will analyze in Chapter 8.
tio
Python’s elementary data types are listed in Table 4.1.
When we insert an integer in Python, this is classified
a
uc

as  int and follows specific rules. When a decimal number is


ed

inserted, though, it is classified as float and the rules are


different.
or
-f

TABLE 4.1  Python’s elementary data types


e
pl

Data type Name Description Examples


m
Sa

Whole number,
Integer int 112, 0, -158
positive or negative
Floating-point
Real
float number, positive 2.14, -159.1234
number or negative
Logical value:
Boolean bool True, False
true or false
String
of alphanumeric
characters, 'SEDIN', 'Python',
String str representing 'Python3', 'Web
a text. It may contain 2.0'
letters, numbers
and symbols
59
To identify the data type each value belongs to, we can use
the  type function:
>>> type(10)
<class 'int'>
>>> type(7.3)
<class 'float'>
>>> type('Python')
<class 'str'>

The  type function can also be used with variables, and it will


return the type of the value they are referencing in that pre-
cise moment:

se
>>> value = 10

lu
>>> type(value)
<class 'int'> na
>>> value = 'very high'
tio
>>> type(value)
a

<class 'str'>
uc

As already mentioned, strings can contain letters, but also num-


ed

bers and symbols. If any kind of value is put in quotes or quo-


or

tation marks (included numerical values), the corresponding


-f

data type will always be considered as a string:


e
pl

>>> type('Python3')
m

<class 'str'>
Sa

>>> type('10')
<class 'str'>

In Python a data type corresponding to «Currency» or «Ac-


LEARNING PYTHON

counting» does not exist, and currency symbols cannot be


written in numerical values (the same is true for spaces or
commas):
>>> total_amount = $5,227.04
SyntaxError: invalid syntax

Numbers must always be written in the simplest possible way,


only with the decimal point (if necessary). Other optional el-
60

ements, such as thousands separators and currency symbols,


can be managed from an output point of view using the  for-
mat function and the print function or string concatenation. 4

Variables and data types


>>> total_amount = 5227.04
>>> print('Total amount: $', format(total_
amount, ',.2f'))
Total amount: $ 5,227.04

Or:
>>> print('Total amount: $' + format(total_
amount, ',.2f'))
Total amount: $5,227.04

In this case, the only difference between the use of a comma

se
(argument separator in the  print function) and the opera-

lu
tor  + (which allows string concatenation) is in the manage-
na
ment of spaces. The different arguments of the  print function
tio
are returned divided by one space automatically, while string
a

concatenation does not insert spaces automatically and they


uc

must be managed manually in the text. To get the same result


ed

in the string concatenation as in the argument separator, it is


therefore necessary to add a space in the  print function af-
or

ter the  $ symbol, before the second quote:


-f
e

>>> print('Total amount: $ ' + format(total_


pl

amount, ',.2f'))
m

Total amount: $ 5,227.04


Sa

The only additional difference is in the fact that the concatena-


tion operator (+) only works with strings, therefore if the  to-
tal_amount variable is not a  format function (which returns
a string as output), concatenating it to the  'Total amount:
$' string will not be possible. There will not be problems if
the string and the variable are simply used as arguments of
the  print function.
In addition to the data types we have just seen, Python also
offers other data types, extremely specialized: lists, tuples, sets,
dictionaries. We will analyze some of these specialized data
types in the following chapters. At the moment, knowing of
their existence is sufficient.
61
As already mentioned, after a variable is assigned a value
of a certain data type, it is possible to reassign it another data
type. For example:
>>> price = 100
>>> type(price)
<class 'int'>
>>> print(price)
100
>>> price = 'Depends on the applied discount
percentage'
>>> type(price)
<class 'str'>
>>> print(price)

se
Depends on the applied discount percentage

lu
The instruction in the first line creates a variable called  price,
na
and assign it value  100, an integer. The instruction of line 6 as-
tio
sign to the same variable  price a string; at this point the vari-
a

able  price does not refer to the integer value  100 anymore,


uc

but to the string value  'Depends on the applied dis-


ed

count percentage'. Python’s dynamic typing allows this op-


eration to be performed transparently (it means that the pro-
or

grammer must do nothing but reassign the variable). In many


-f

static typing languages the data type must be explicitly declared


e

in the assignment statement (often referred to as variable dec-


pl
m

laration) and the data type cannot be changed through a sub-


Sa

sequent reassignment operation.

Mixed data types When performing a calcula-


4.5 expressions tion between two operands,
the data type of the result
depends on the data type of the operands. In this case, Python
follows some specific rules:
XX when the two operands are int, the result will be int;
XX when the two operands are float, the result will be float;
XX when the operation is between an int and a float, the result
will always be a float. In this case, we talk about a mixed data
62

type expression.
For example:
>>> 17 * 2.0
4

Variables and data types


34.0

In § 4.6, we will see when and how it is possible to convert a


value from one data type to another.

Type conversion Converting a value from one data


4.6 functions type to another can be useful in
some cases, and necessary in oth-
ers. This operation is not always possible and sometimes it does
not make any sense, but often it is fundamental for the correct

se
management of data. For example, some functions return as

lu
result values in string format (even when they are numbers),
while converting them to integers or floats may be necessary
na
to be able to use them for later calculations. The  format func-
tio
tion, already seen, and the  input function, which we will see
a

in § 4.7, are examples of this behavior.


uc

To convert the data type of a variable, you can use


ed

the three built-in Python functions useful for this pur-


or

pose:  int,  float and  str. The  int function converts to an


-f

integer; the  float function converts to a floating-point num-


ber; the  str function converts to a string. Now we are going to
e
pl

see them in more detail.


m
Sa

■ 4.6.1  The int function


The  int function takes a certain value and converts it to in-
teger, if possible.
>>> price = '100'
>>> int(price)
100
>>> type(price)
<class 'str'>

However, in this case the  price variable still is of type  str:


what Python displays is simply the value corresponding to the
63

conversion of the variable into integer.


On the other hand, it is different if we create a new vari-
able that is the result of the conversion of the previous one.
In the example below, we create the  price_int variable, to
which we assign the value of the  price variable converted
to an integer:
>>> price = '100'
>>> price_int = int(price)
>>> price_int
100
>>> type(price_int)
<class 'int'>

In all those cases in which conversion is impossible (because it

se
makes no sense or it is not technically feasible), an error mes-

lu
sage appears:
>>> int('Python')
na
tio
Traceback (most recent call last):
a

File "<pyshell#58>", line 1, in <module>


uc

int('Python')
ed

ValueError: invalid literal for int() with


base 10: 'Python'
or
-f

The  int function can also be used for converting float values


into integers, but we must notice that the decimal part is not
e
pl

rounded, but truncated:


m
Sa

>>> int(9.9999)
9
LEARNING PYTHON

■ 4.6.2  The float function


The  float function converts integers and strings (when pos-
sible) into floating point numbers:
>>> float(10)
10.0
>>> float('10.123456789')
10.123456789
64
■ 4.6.3  The str function
The  str function converts the value of its argument into a
4

Variables and data types


string. It could be useful, for example, when we want to show a
result consisting of numbers and strings. Suppose the  prof-
it variable refers to a decimal number, which represents an
amount of money in euros. We want to construct a string that
represents the output value with the  € symbol and the value
of the  profit variable. However, the concatenation between
a string and a numeric value is not allowed in Python, as we
have already seen:
>>> profit = 1227.64
>>> '€' + profit

se
Traceback (most recent call last):

lu
File "<pyshell#20>", line 1, in <module>
'€' + profit na
TypeError: can only concatenate str (not
tio
"float") to str
a
uc

To solve this problem, we can use the  str function, which con-


ed

verts the value of the profit variable into a string, thus allow-
ing its concatenation with the currency symbol:
or
-f

>>> '€ ' + str(profit)


e

'€ 1227.64'
pl
m

As we have learned, Python is a strongly typed language: the


Sa

interpreter checks the data types of all operands before execut-


ing an operation. If the type of an operand is not appropriate,
the interpreter interrupts the execution with an error message.
This control prevents the program from doing something not
allowed. For this reason, the conversion functions are funda-
mental in the management of the data types of the operands.

The  input  function is very


4.7 The input function important and widely used in
Python: it is used to allow the
user to enter data from the keyboard, which will then be used
65

by the program. In fact, most programs must read input data


and perform operations on such data in order to provide an out-
put. A very common input operation is the input of keyboard
data. When a program receives data from the keyboard, it usu-
ally stores them in a variable, so that they can be used later.
The  input function reads a piece of data entered from the
keyboard and returns it to the program as a string:  input ac-
cepts one argument only, called prompt. It is a string that is dis-
played on screen before being input by the user. It is often used
within an assignment statement, as follows:
variable = input(prompt)

For example:

se
>>> name = input('What is your name? ')

lu
What is your name?
na
tio
Once the user has typed a value and pressed the Enter key, the
function memorizes the piece of data in the  name variable.
a
uc

What is your name? Robert


ed

>>> print(name)
or

Robert
-f

In the previous example we can notice the space after the ques-
e

tion mark in  'What is your name? ': if the space was not
pl
m

inserted, the user’s answer would not be divided from the ques-
Sa

tion mark, thus affecting text readability:


>>> name = input('What is your name?')
What is your name?Robert
LEARNING PYTHON

As already mentioned,  input  returns the value as a string


even if the user inputs numerical data, as in the example below:
>>> value = input('Insert the desired value: ')
Insert the desired value: 242
>>> value
'242'
>>> type(value)
66

<class 'str'>
This does not change if we decide to display the value
with  print, even if the lack of quotes in the output may de- 4
ceive us. See the following example:

Variables and data types


>>> print(value)
242

Once again, if we asked Python what the data type of the  val-
ue variable is, we would get the same answer:

>>> type(value)
<class 'str'>

Moreover, if we try to increase the  value variable by 1, we

se
will get an error:

lu
>>> value = value + 1
Traceback (most recent call last): na
tio
File "<pyshell#16>", line 1, in <module>
value = value + 1
a
uc

TypeError: must be str, not int


ed

This can be a problem, especially if we want to perform a math-


or

ematical operation using the value referenced by the variable.


-f

It is therefore necessary to convert the string returned by


the  input function using the conversion functions seen in
e
pl

§ 4.6:  int and  float.


m

With reference to the previous example, we can solve the


Sa

problem using the  int function. The function can be used to


nest input, so that the output is already an  int value.
>>> value = int(input('Insert the desired
value: '))
Insert the desired value: 127
>>> value
127
>>> value = value + 1
>>> value
128

The next example shows the use of the  float function, which


67

allows to calculate the area and perimeter of a square by asking


the user for the length of the side. If we use the  input function
without correctly handling the data type, we will incur an error:
>>> side = input('What is the length of the
side ? ')
What is the length of the side? 3.5
>>> print("The area of the square is:",
side**2)
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
print("The area of the square is:", lato**2)
TypeError: unsupported operand type(s) for **
or pow(): 'str' and 'int'

se
Therefore, it is necessary to manage the output data of the  in-

lu
put function, converting it into a float:
na
>>> side = float(input('What is the length of
tio
the side? '))
What is the length of the side? 3.5
a
uc

>>> print("The area of the square is:",


side**2)
ed

The area of the square is: 12.25


or

>>> print('The perimeter of the square is:',


-f

side * 4)
The perimeter of the square is: 14.0
e
pl

In addition, it is possible to insert the length of the side into


m

the final  print function:


Sa

>>> print('The perimeter of the square whose


side is', side, 'is:', side * 4)
LEARNING PYTHON

The perimeter of the square whose side is 3.5


is: 14.0
68

Das könnte Ihnen auch gefallen