• No results found

Programming in Python 1

N/A
N/A
Protected

Academic year: 2021

Share "Programming in Python 1"

Copied!
165
0
0

Loading.... (view fulltext now)

Full text

(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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

1

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

(2)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

2

Lecture I: Programming in Python for

quantitative biologists

(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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

3

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.

(4)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

4

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

(5)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

5

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

(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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

6

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.

(7)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

7

Programming

Programming in science can serve two (almost opposite) goals:

1

Understanding every detail of a computational process;

2

Compose 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

(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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

8

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

print

), even if they could not be programmed in

Python.

(9)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

9

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

(10)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

10

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)

(11)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

11

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.

(12)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

12

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

print

(

"Hello!"

) are actually used to request

side effects

in the executing environment.

(13)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

13

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

(14)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

13

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

(15)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

13

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

(16)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

13

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

(17)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

14

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

.

(18)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

15

(19)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

16

Polls

To answer the questions I will rise please connect to:

sli.do

Event#: 57146

(20)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

17

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.

(21)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

18

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

(22)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

19

Sequence of operations

1

x

= 1 + 2 * 3

2

x

=

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

1

2 * 3

2

1 + 6

3

x

= 7

4

7 + 1

5

x

= 8

(23)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

20

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

(24)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

21

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

print

(i)

Repeat while a (variable)

condition is true

i

= 0

while

i

< 5

:

print

(i)

(25)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

22

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.

(26)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

23

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.

(27)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

24

Homework

(28)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

25

(29)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

26

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

(30)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

27

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

(31)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

28

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

print

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

).

(32)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

29

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

(33)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

30

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

,→

print

(concat_with_a_space(

'foo'

,

'bar'

))

Return

None

def

repeated_print

(string, repetitions):

for

i

in range

(

0

, repetitions):

print

(string)

repeatedPrint(

'Hello, world!'

,

3

)

Recursive call

:

def

repeatedPrint

(string, repetitions):

if

repetitions

> 0

:

print

(string)

repeatedPrint(string, repetitions

- 1

)

repeatedPrint(

'Hello, world!'

,

3

)

(34)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

31

Functions are objects too

One can assign functions to variables:

def

cube

(x:

int

)

->

int

:

square

=

x

*

x

return

square

*

x

mycube

=

cube

print

(mycube(

3

))

print

(

type

(mycube))

And short functions can even be expressed as literal expressions

(

lambda expressions

)

(35)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

32

Naming helps solving

The tower of Hanoi

(36)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

33

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.

(37)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

34

In Python

def

hanoi

(n:

int

, a_from:

str

, c_to:

str

,

b_intermediate:

str

):

,→

if

n

== 1

:

print

(

'Move 1 disk from '

+

a_from

+

' to '

+

c_to)

return

hanoi(n

- 1

, a_from, b_intermediate, c_to)

print

(

'Move 1 disk from '

+

a_from

+

' to '

+

c_to)

hanoi(n

- 1

, b_intermediate, c_to, a_from)

(38)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

35

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

(39)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

36

(40)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

37

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

(41)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

38

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

.

(42)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

39

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

5

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

(43)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

40

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

(44)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

41

(45)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

42

Homework status

Students list has 26 names. . .

. . . but only 18 accepted the assignment on GitHub

Classroom

(46)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

43

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

]

(47)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

44

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

,→

print

(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

print

(x, y)

x

=

[

100

,

200

,

300

]

print

(x, y)

(48)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

45

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

(49)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

46

Homework

https://classroom.github.com/a/gd_s1ybQ

https://classroom.github.com/a/6AI0xnot

https://classroom.github.com/a/exi-O3Kl

(50)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

47

(51)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

48

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

(52)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

49

Dictionaries

A composite type

dict

that implements a

mapping

between

immutable

keys

and

values

.

d

=

{

'key'

:

'foo'

,

3

:

'bar'

}

print

(d[

'key'

])

# 'foo'

print

(d[

3

])

# 'bar'

print

(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():

print

(d[x])

A sequence of values can be obtained with d

.

values. A

sequence of 2-tuples (key, value) with d

.

items().

(53)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

50

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

})

(54)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

51

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

(55)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

52

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.

(56)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

53

File read access

f

=

open

(

'filename.txt'

,

'r'

)

# read only

# iterating on a file reads (all) the lines

for

i

in

f:

print

(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:

(57)

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

c

ay

Creation Indexing Vectorization Array operations Homework Matplotlib Graphical commands OO plotting Gray-Scott Discrete Laplacian Exception handling Iterators Theano Monte-Carlo

54

Exercises

Write a function to compute the complement of a DNA

strand: every A becomes a T, every T an A, every C an G,

every G an C.

Apply the function to every line of a file with a DNA

sequence

Write a function that gives the set of (unique) sequences

of 10 nucleic acids in a file

References

Related documents

4 They may be in the form of homework assignments, practice sheets, programming exercises, (e-) tests, tasks assigned within a university internship program, etc.

– Object based programming (classes, objects, encapsulation) – Object oriented programming (inheritance, polymorphism) – Generic programming (class and function templates)...

The ________ function returns Boolean True value if all the keys in the dictionary are True else returns False.. Predict the output of the

A: IP and IPX are not encapsulation types. They are routing protocols. B: Only PPP is an encapsulation type. C: SDLC is a protocol used in IBM SNA systems. Interconnecting

The code breaks when expiration dates reach the year 2000, despite the use of a Date abstract data type with a stable interface and programming language enforced strong

Morning eggs (8-10am; 10-12noon) recorded higher mean values in both external and internal egg quality traits than afternoon collection.. Hu for both sessions were higher

Promote equalities and community cohesion, including the legal duties to promote race and disability equality by:1. Presentations to Community Forums, Tenants and Residents

They encap- sulate the Solaris rwlock t synchronization variable (the POSIX pthreads and Win32 threads implementation uses other mechanisms) and is described in Section 5.1.3..