PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo1
Programming in Python
1
Mattia Monga
Dip. di Informatica
Universit`
a degli Studi di Milano, Italia
[email protected]
Academic year 2020/21, II semester
1cba 2020 M. Monga. Creative Commons Attribuzione — Condividi allo stesso modo 4.0
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo2
Lecture I: Programming in Python for
quantitative biologists
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo3
Programming in Python (for quantitative
biologists)
The course introduces imperative programming by referring to
the Python language.
1
Python3 and its object-oriented
features;
2
Python3 libraries that can be useful
in scientific computation and data
analysis, in particular NumPy and
pandas.
Everything will be available on:
mattiamon.ga/pyqb
(a.k.a.
https://mameli.docenti.di.unimi.it/pyqb
)
Please: fill in the survey, subscribe to Zulip.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo4
Course schedule
Mondays, Fridays 9:30–11:30 (am)
Lectures: 40h (online), Labs: 16h (face-to-face if possible)
Labs always on Friday, 12/3 19/3 9/4 23/4 7/5 21/5 4/6
11/6
We will explore different setups: (1) a “scaffolded” one for
the first steps, (2) the plain python interpreter, and finally
(3) the notebooks popular in scientific practice
Tutor: dott. Davide Paolillo (computer science master
student)
Text: every Python3 reference/book/tutorial is ok, you
can access freely to the book linked on the website
Final test: write (small) python programs without help
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo5
Why Python?
Programming can be approached in many “languages”, the
fundamental skills are general. . . but you cannot learn without
referring to a specific language.
A precise requirement of the teaching committee
Very popular in the scientific landscape
Easy to learn, many useful libraries, free software
Alternatives: Fortran, C, Matlab, Mathematica, R, Julia,
. . .
Python is slower, but it is considered easier to understand
and manage
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo6
Which Python?
We will use Python3 (current version is 3.9): be careful when
looking around, Python2 is still very common (but deprecated)
and incompatible. Python supports different “paradigms”, we
will focus on:
Imperative programming: programs describe
changes
in
registers and the executing environment;
Object-oriented: complex (imperative) programs are
organized around
objects
in order to hide and isolate
complexity.
This is a
programming course
: I will try to propose example
that I believe could be useful in your daily practice, but I’m not
a biologist.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo7
Programming
Programming in science can serve two (almost opposite) goals:
1Understanding every detail of a computational process;
2Compose computational process by assembling powerful
build blocks of which you understand very little.
Most of the current popularity of programming is related to
goal 2. . . with many sorcerer’s apprentices. But this course will
focus mainly on goal 1. In the last part of the course we will
bend towards 2, hopefully with a solid background.
Programming can be both hard and addictive:
Teach Yourself
Programming in Ten Years
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo8
Fundamental concepts of Python
The programmer describes computational processes in terms of:
objects
: all the entities manipulated by the program, each has
an
identity
(can be distinguished) and a
value
, that is
an element in a specific
type
(a set of values together
with the operations that make sense on them)
basic types
: integers (
int
), floats, strings (
str
), functions; they
can be composed in more complex types
variables
:
names
used to refer to objects; the same name can
refer to different objects during the same process
special commands
: the only way to change the execution
environment (i.e., the “virtual machine” provided by
the operating system) is to use
system calls
; syscalls
change from system to system (e.g., Linux vs.
Windows), but Python wraps them and they appear
like the functions written by the programmers (e.g.,
), even if they could not be programmed in
Python.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo9
The onion model
Hardware
Operating System
Applications
Software
Operating System: it
is the only program
interpreted directly by
the hardware; other
pieces of software get
interpreted by the
virtual machine
provided by it.
Applications:
programs (e.g., the
python interpreter or
python programs)
executed within the
protected environment
created by the
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo10
Let’s try!
https://python.di.unimi.it/
You can use it without any personal account, but if you want
support you must create one, putting me as the
“guru”: mmonga
This platform will be used for the first lessons, since it requires
no setup at all: everything happens in the browser (and the
server).
(Thanks to the University of Waterloo, Canada for providing
the CS Circles)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo11
Assignment
This is the fundamental statement for imperative programming:
A
name
, known as
variable
, is needed to refer to objects.
professor
=
"Mattia"
=
is not symmetrical
, read it as
becomes
: Left-hand-side is
always a variable, right-hand-side is an object, that can be
either a
literal
or anything referred by another variable.
A variable can change its value with another, following,
assignment. Thus, the same variable may refer to different
objects.
professor
=
"Violetta"
Basic objects (numbers, strings, Boolean values) are
immutable
(the variable change, not the object; different
objects have always different identity)
Tracking
a program means to track the values of all the
variables of a program during its execution.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo12
Basic operations
Binary operators
:
5 + 2
, they compute a new object by
using the two objects on which they apply;
Unary operators
:
-
(
-5
);
Functions
:
max
, they compute a new object by using an
arbitrary number of objects (in general 0–. . . ,
max
takes at
least 1)
passed
as
parameters
(or
arguments
) when the
function is
called
(
max
(
3
,
6
, something_else));
sometimes the object computed is
None
;
Syntactically appear as functions, but commands like
(
"Hello!"
) are actually used to request
side effects
in the executing environment.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo13
Different approaches
Problem: exchange the name of two objects (Chapter 1, last
exercise).
Know the basic syntax of
variables
and
assignment
=
Know the semantics of what you write: assigning an
object to a variable delete any previous assignment;
Natural strategy: use a temporary name to “save” the
value during the exchange;
“Fox” strategy: know language or library tricks For
example Python has a “multiple assignment” construct x,
y
=
y, x, or a special library function swap(x, y) could
exist;
“Hedgehog” strategy: study the problem in depth, e.g., if
objects are numbers you can exploit arithmetic.
x
=
x
+
y
y
=
x
-
y
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo13
Different approaches
Problem: exchange the name of two objects (Chapter 1, last
exercise).
Know the basic syntax of
variables
and
assignment
=
Know the semantics of what you write: assigning an
object to a variable delete any previous assignment;
Natural strategy: use a temporary name to “save” the
value during the exchange;
“Fox” strategy: know language or library tricks For
example Python has a “multiple assignment” construct x,
y
=
y, x, or a special library function swap(x, y) could
exist;
“Hedgehog” strategy: study the problem in depth, e.g., if
objects are numbers you can exploit arithmetic.
x
=
x
+
y
y
=
x
-
y
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo13
Different approaches
Problem: exchange the name of two objects (Chapter 1, last
exercise).
Know the basic syntax of
variables
and
assignment
=
Know the semantics of what you write: assigning an
object to a variable delete any previous assignment;
Natural strategy: use a temporary name to “save” the
value during the exchange;
“Fox” strategy: know language or library tricks For
example Python has a “multiple assignment” construct x,
y
=
y, x, or a special library function swap(x, y) could
exist;
“Hedgehog” strategy: study the problem in depth, e.g., if
objects are numbers you can exploit arithmetic.
x
=
x
+
y
y
=
x
-
y
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo13
Different approaches
Problem: exchange the name of two objects (Chapter 1, last
exercise).
Know the basic syntax of
variables
and
assignment
=
Know the semantics of what you write: assigning an
object to a variable delete any previous assignment;
Natural strategy: use a temporary name to “save” the
value during the exchange;
“Fox” strategy: know language or library tricks For
example Python has a “multiple assignment” construct x,
y
=
y, x, or a special library function swap(x, y) could
exist;
“Hedgehog” strategy: study the problem in depth, e.g., if
objects are numbers you can exploit arithmetic.
x
=
x
+
y
y
=
x
-
y
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo14
Homework
Finish chapters 1, 1E, 2, 2X, 3, 4.
It shouldn’t take more than three hours (one hour per day. . . ),
but exercising is
crucial
.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo15
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo16
Polls
To answer the questions I will rise please connect to:
sli.do
Event#: 57146
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo17
Summary
Programming means to instruct an (automatic) interpret
with a precise description of a computational process.
(In fact, the only way to make a description precise is to
specify exactly the interpreter)
We use a software interpreter, itself a program interpreted
by the operating system (the stack of interpreters can be
much deeper).
Our interpret (Python3) manipulates objects taken from
types (that define which manipulations are possible),
referred by variables, with special commands to ask the
services provided by the operating system.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo18
Basic types
bool False
,
True
Logical operations
int
1
,
-33
,
1_000_000_000
. . . Arithmetic
operations, no upper or lower limit
float
1.0
,
.1
,
1.2e34
. . . Arithmetic operations,
limited but you have
float
(
'infinity'
) (and
float
(
'nan'
))
sys
.
float_info(
max
=1.7976931348623157e+308
c, max_exp
=1024
, max_10_exp
=308
,
min
=2.2250738585072014e-308
,
min_exp
=-1021
, min_10_exp
=-307
,
dig
=15
, mant_dig
=53
,
epsilon
=2.220446049250313e-16
,
radix
=2
, rounds
=1
)
,→ ,→ ,→ ,→ ,→ ,→str
'aaaa\nthis is on a new line'
,
"bbb'b\"b"
. . . Concatenation, alphabetical
ordering, replication, . . .
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo19
Sequence of operations
1x
= 1 + 2 * 3
2x
=
x
+ 1
The 2 lines of code translate to at least 5 “logical” instructions
(maybe more, for example adding two big numbers require
multiple instructions):
12 * 3
21 + 6
3x
= 7
47 + 1
5x
= 8
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo20
Flow of control
It is normally not very useful to write programs that do just
one single computation.
You wouldn’t teach a kid how to multiply
32 × 43, but the
general algorithm
of multiplication (the level of
generality can vary).
To write programs that address a family of problems we need
to be able to
select
instructions to execute according to
conditions
.
if
x
< 0
:
x
= -
x
y
= 2 *
x
if
x
== -1
:
x
=
x
+ 1
else
:
x
= 3 *
x
y
= 2 *
x
In Python the indentation is part of the syntax and it is
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo21
Repetitions
It is also useful to be able to
repeat
instructions: it is very
convenient, but it also opens a deep Pandora’s box. . .
There are two ways of
looping
in Python:
Repeat by iterating on the
elements of a collection (similar
to math notation
P
i ∈{a,b,c}
f (i ))
for
i
in range
(
0
,
5
):
# 0 1 2 3 4
(i)
Repeat while a (variable)
condition is true
i
= 0
while
i
< 5
:
(i)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo22
Loops can be difficult to understand
When you have loops, understanding the code can be a difficult
task and the only general strategy is to track the execution.
# This is known as Collatz's procedure
n
= ...
while
n
> 1
:
if
n
% 2 == 0
:
# if the remainder of division by 2 is 0, i.e. n
is even
,→
n
=
n
/ 2
else
:
n
= 3*
n
+ 1
We know (by empirical evidence) that it ends for all
n < 2
68
≈ 10
20
, nobody is able to predict the number of
iterations given any n.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo23
Learn to write loops can be hard
When you write a loop, you should have in mind two related
goals:
1
the loop must terminate
: this is normally easy with
for
loops (when the finite collection ends, the loop ends also),
but it can be tricky with
while
s (remember to change
something in the condition);
2
the loop repeats something
: the programmer should be
able to write the “repeating thing” in a way that makes it
equal in its form (but probably different in what it does).
The second part (technically known as
loop invariant
) is the
hardest to learn, since it requires experience, creativity, and
ingenuity.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo24
Homework
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo25
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo26
International Women’s Day
Ada Byron
(Lady King, Countess of
Lovelace, 1815–1852) wrote the first ever
program for an automatic machine, the
“analytical engine” designed (but never
built) by Charles Babbage.
In 1840, C. Babbage gave a seminar in
Turin, Luigi Menabrea (future Italian prime
minister) transcribed it into French, Ada
translated it to English. . . with many original
notes, and a program to compute Bernoulli
numbers (1843).
An important programming language was
named after her: Ada.
Daguerreotype by
Antoine Claudet,
public domain
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo27
Summary
In Python3
Variables are names to refer to objects;
Objects are elements of types, which define the operations
that make sense on them;
Therefore, the basic instructions are the
assignment
(bind
a name to an object),
the proper operations for each
object
, and the
commands
to ask the services of the
operating system;
One can alter the otherwise strictly sequential execution of
instruction with control flow statements:
if
,
for
,
while
.
Remember that in python3, indentation matters (it is part of
the syntax).
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo28
Proper operations
On objects one can apply
binary
and
unary
operators:
2 *
3 -
(
-5.0
)
not True
'foo'
+
'bar'
. . .
There also
built-in
functions like
max
(
8
,
5
,
6
), the full list
is here:
https:
//docs.python.org/3/library/functions.html
(syntactically, commands like
or
input
cannot be
distinguished from other built-in functions)
Every object has methods that can be applied with the so
called
dot notation
: (
3.2
)
.
is_integer()
'foo'.
upper()
'xxx'.
startswith(
'z'
); the list of
which methods an object has is given by
dir
(
object
).
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo29
Definition of functions
As variables are names for objects, one can also name
fragments of code:
def
cube
(x:
int
)
->
int
:
square
=
x
*
x
return
square
*
x
Now we have a new operation cube, acting on
int
s: cube(
3
).
Type hints are optional (and ignored, you can call cube(
3.2
)
or cube(
'foo'
)), but
very useful
for humans (and tools like
mypy).
# Equivalent
def
cube
(x):
square
=
x
*
x
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo30
A function computes a result
Returns a useful result
def
concat_with_a_space
(string1, string2):
return
string1
+
' '
+
string2
# string1 is the _formal_ parameter
# 'foo' is the _actual_ parameter (like an assigment string1 =
'foo')
,→
(concat_with_a_space(
'foo'
,
'bar'
))
Return
None
def
repeated_print
(string, repetitions):
for
i
in range
(
0
, repetitions):
(string)
repeatedPrint(
'Hello, world!'
,
3
)
Recursive call
:
def
repeatedPrint
(string, repetitions):
if
repetitions
> 0
:
(string)
repeatedPrint(string, repetitions
- 1
)
repeatedPrint(
'Hello, world!'
,
3
)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo31
Functions are objects too
One can assign functions to variables:
def
cube
(x:
int
)
->
int
:
square
=
x
*
x
return
square
*
x
mycube
=
cube
(mycube(
3
))
(
type
(mycube))
And short functions can even be expressed as literal expressions
(
lambda expressions
)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo32
Naming helps solving
The tower of Hanoi
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo33
Describe the moves for a solution
Recursive thinking is a powerful problem solving technique and
it can be translated to Python thanks to recursive calls.
Hanoi moves A → C :
In A there is just one disk: move it to C
Otherwise in A there are n disks (> 1):
leap of faith!
I suppose to know the moves needed to
move n − 1 disk; then
apply this (supposed) solution to move n − 1 disks from A
to B (leveraging on C , empty, as the third pole)
move the last disk from A to C
apply the (supposed) solution to move n − 1 disks from B
to C (leveraging on A, now empty, as the third pole)
This implicit description solve the problem! Finding a
non-recursive solution is possible but not that easy.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo34
In Python
def
hanoi
(n:
int
, a_from:
str
, c_to:
str
,
b_intermediate:
str
):
,→
if
n
== 1
:
(
'Move 1 disk from '
+
a_from
+
' to '
+
c_to)
return
hanoi(n
- 1
, a_from, b_intermediate, c_to)
(
'Move 1 disk from '
+
a_from
+
' to '
+
c_to)
hanoi(n
- 1
, b_intermediate, c_to, a_from)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo35
Homework
Chapters 7A, 10, 11A, 11B, 11C, 12
Create an account on
https://github.com/
(if you
don’t have one) and send me the name (Zulip preferred,
use a private message if you don’t want to make it known
to the other students).
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo36
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo37
The pieces of software
Python 3.9.2, with pip and the IDLE editor (on MS
Windows they are bundled together):
https://www.python.org/downloads/
Git 2.30.2:
https://git-scm.com/downloads
(optional, Win and Mac only) Github desktop
https://desktop.github.com/
Homework assigments will be available via Github Classroom
(you will need a Github account). For example:
https://classroom.github.com/a/TpWhD1zt
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo38
Software Configuration Management
Software Configuration Management like git are tools designed
to track all the
revisions
of some set of software
artifacts
(files).
q
0
q
1
q
2
. . .
q
n
r
0
r
1
configuration
r
2
. . .
r
n
The system configuration itself evolves in different
versions
.
One can have multiple
branches
of evolution.
A motivating talk on why you should use tools like these in
your scientific work
.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo39
Git
git is a powerful tool to manage all this complexity in a very
efficient (and distributed) way. It is not an easy tool, however.
A good tutorial is
here
. But for this course we use a very
simplistic workflow:
1
Clone (copy) on your machine a repository git clone
...;
2
Work on the artifacts
3
Add the modified artifacts to the changeset you want to
“publish” git add ...
4
Commit the changeset git commit -m"message"
providing a comment about what have you done
5Push the changeset on Github git push
6
(If someone else is working on the same artifacts you can
sync with git pull)
All these steps are very easy (almost hidden, especially
authentication) if you use Github desktop.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo40
IDLE
Programs are data!
File extension is conventionally .py
To edit Python programs you need a
text editor
:
something like Notepad, not Word (a word processor)
IDLE is the “standard” one provided by the Python
distribution itself: it is easy to use and it provides an easy
way for executing programs without getting to the
command line
Other good choices: VS Code Atom Notepad++ or any
other universal text editor like EMACS or vi
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo41
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo42
Homework status
Students list has 26 names. . .
. . . but only 18 accepted the assignment on GitHub
Classroom
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo43
Simple and composite objects
int
s
float
s
bool
s are simple objects: they have no
“parts”
Strings are an example of composite objects since it is
possible to consider also the characters: a
str
is a
sequence of single characters; an important (simplifying)
property: they are
immutable
Generic
immutable
sequences (with elements of any type)
are called
tuples
(
tuple
): (
1
,
2
,
'foo'
) (
1
,)
Generic
mutable
sequences (with elements of any type) are
called
lists
(
list
): [
1
,
2
,
'foo'
] [
1
]
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo44
Mutability
Immutable objects are simpler to use:
x
=
(
1
,
2
,
3
)
y
=
x
x
=
(
10
,
20
,
30
)
# x refers to a new object, since the
old cannot be changed
,→
(x, y)
Mutable ones require some caution:
x
=
[
1
,
2
,
3
]
y
=
x
x[
0
]
= 10
# both x and y refer to a changed object
(x, y)
x
=
[
100
,
200
,
300
]
(x, y)
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo45
Exercises
Write a function middle(L) which takes a list L as its
argument, and returns the item in the middle position of
L. (In order that the middle is well-defined, you should
assume that L has odd length.) For example, calling
middle([
8
,
0
,
100
,
12
,
1
]) should return 100, since
it is positioned exactly in the middle of the list.
(
assert
is a useful tool to check assumptions — known
as
preconditions
— are indeed true)
Define a function prod(L) which returns the product of
the elements in a list L.
(for type hinting it is sometimes useful
from
typing
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo46
Homework
https://classroom.github.com/a/gd_s1ybQ
https://classroom.github.com/a/6AI0xnot
https://classroom.github.com/a/exi-O3Kl
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo47
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo48
Homework status
Students: 27
One triangle: tried by 22, 16 correct solutions
Triangle kinds: tried by 20, 9 correct solutions
DNA Hamming: tried by 19, 12 correct solutions
Newton Sqrt: tried by 17, 13 correct solutions
7 students did all the exercises correctly
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo49
Dictionaries
A composite type
dict
that implements a
mapping
between
immutable
keys
and
values
.
d
=
{
'key'
:
'foo'
,
3
:
'bar'
}
(d[
'key'
])
# 'foo'
(d[
3
])
# 'bar'
(d[
2
])
# error!
Notation is similar to lists/tuples, but
dict
s are not sequences
indexed by numbers, you must use only the existing keys
(d
.
keys()).
if
x
in
d
.
keys():
(d[x])
A sequence of values can be obtained with d
.
values. A
sequence of 2-tuples (key, value) with d
.
items().
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo50
Sets
A
set
is a composite object with no duplicate (non mutable)
elements. Common set operations are possible.
Set literals: {
1
,
2
,
3
}
set
()
{
1
,
2
,
3
}
.
union({
3
,
5
,
6
})
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo51
Comprehensions
Comprehensions
are a concise way to create lists, sets,
maps. . . It resembles the mathematical notation used for sets
A = {a
2
|a ∈ N}.
squares
=
[x
**2
for
x
in range
(
10
)]
# equivalent to:
squares
=
[]
for
x
in range
(
10
):
squares
.
append(x
**2
)
# filtering is possible
odds
=
[x
for
x
in range
(
100
)
if
x
% 2 != 0
]
# with a set
s
=
{x
for
x
in range
(
50+1
)
if
x
% 5 == 0
}
# with a dict
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo52
Files
A
file
is an abstraction the operating system uses to preserve
data among the execution of programs. Data must be accessed
sequentially
.
We need commands to ask to the OS to give access to a
file (
open
).
It is easy to read or write data
sequentially
, otherwise you
need special commands (seek) to move the file “cursor”
The number of open files is limited (≈ thousands), thus it
is better to close files when they are not in use
Files contain bits (normally considered by group of bytes, 8
bits), the interpretation (“format”) is given by the programs
which manipulate them. However, “lines of printable
characters” (
plain text
) is a rather universal/predefined
interpretation, normally the easiest to program.
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework
ndarr
cay
Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo53
File read access
f
=
open
(
'filename.txt'
,
'r'
)
# read only
# iterating on a file reads (all) the lines
for
i
in
f:
(i)
# End of file already reached, result is ''
f
.
readline()
f
.
close()
# File closed, error!
f
.
readline()
To avoid remembering to close explicitly, Python provides the
context manager
syntax.
with open
(
'filename.txt'
,
'r'
)
as
f:
for
i
in
f:
PyQB Monga Why Python Python fundamentals Assignment Basic operations Homework Summary Flow of control Selections Repetitions Functions Software git IDLE Composite objects
Tuples and lists
Homework Dictionaries Sets Comprehensions Files Exercises Types, docstrings, doctests Abstracting similarities Procedural encapsulation OO encapsulation Homework Random numbers Monte Carlo Simulations Third-party libraries NumPy Homework