Introduction to Python Programming - Luis Pedro...

Preview:

Citation preview

Introduction to Python Programming

Luis Pedro Coelho§

On the web: http://luispedro.orgOn twitter: @luispedrocoelho

European Molecular Biology Laboratory

April 3, 2014

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (1 / 36)

Python

Let’s digress for a moment discussing the language…

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (2 / 36)

Python Language History

HistoryPython was started in the late 80’s.It was intended to be both easy to teach and industrial strength.It is (has always been) open-source.In the last 10 years, it has become one of the most widely usedlanguages (top 10).

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (3 / 36)

Popularity

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (4 / 36)

Popularity

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (5 / 36)

Python Versions

Python VersionsThe current versions of Python are 2.7 and 3.4This class assumes you have 2.6–2.7There are some small differences when compared to version 3.x

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (6 / 36)

What is a Computer?

...1 Memory

...2 Processor

...3 Magic

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (7 / 36)

Python Model

...1 Objects

...2 Operations on objects

...3 Magic

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (8 / 36)

Python Example

pr in t ” He l lo World”

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (9 / 36)

Running Python...1 From a file...2 Interactively

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (10 / 36)

Computer Program

helloword.pypr in t ’ He l l o World ’

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (11 / 36)

Running a Program

...1 Shell

...2 IDE

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (12 / 36)

Let me show you a demonstration…

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (13 / 36)

More Complex Example

What is 25 times 5?

pr in t 25 * 5

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (14 / 36)

More Complex Example

What is 25 times 5?pr in t 25 * 5

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (14 / 36)

More Complex Example

name = 2other = 3yetanother = name + othername = 5pr in t yetanother + name

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (15 / 36)

Blackboard demonstration

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (16 / 36)

Conditionals

i f <cond i t i on>:<statement 1><statement 2>

e l s e :<statement 3>

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (17 / 36)

Conditionals (Example)

pr in t ’ Before t e s t i n g . . . ’i f 3 . 3*9 . 2 > 30 :

p r i n t ’ Greater than 30 ’e l s e :

p r i n t ’ Smal ler or equal ’p r i n t ’ After ’

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (18 / 36)

Conditionals (Example)

pr in t ’ Before t e s t i n g . . . ’i f 3 . 3*9 . 2 > 31 :

p r i n t ’ Greater than 31 ’e l i f 3 . 3*9 . 2 > 30 :

p r i n t ’ Greater than 30 ’e l s e :

p r i n t ’ Smal ler or equal ’p r i n t ’ After ’

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (19 / 36)

Conditionals (Example)

pr in t ’ Before t e s t i n g . . . ’v = 3 . 3*9 . 2i f v > 31 :

p r i n t ’ Greater than 31 ’e l i f v > 30 :

p r i n t ’ Greater than 30 ’e l s e :

p r i n t ’ Smal ler or equal ’p r i n t ’ After ’

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (20 / 36)

Lists

s tudents = [ ’ Luis ’ , ’Mark ’ , ’ Rita ’ ]

p r i n t s tudents [ 0 ]p r i n t s tudents [ 1 ]p r i n t s tudents [ 2 ]

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (21 / 36)

Loops

s tudents = [ ’ Luis ’ , ’Mark ’ , ’ Rita ’ , . . . ]

f o r s t in s tudents :p r i n t s t

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (22 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

sum = 0f o r v in va lue s :

sum = sum + vpr in t sum

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (23 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

sum = 0f o r v in va lue s :

sum = sum + vpr in t sum

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (23 / 36)

Exercise

How do you obtain the number of elements in a list?Use this to compute the mean of a list of numbers

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (24 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

sum = 0 . 0sum2 = 0 . 0f o r v in va lue s :

sum = sum + vsum2 = sum2 + v * v

mu = sum/ len ( va lue s )mu2 = sum2/ l en ( va lue s )p r i n t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (25 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

sum = 0 . 0sum2 = 0 . 0f o r v in va lue s :

sum += vsum2 += v * v

mu = sum/ len ( va lue s )mu2 = sum2/ l en ( va lue s )p r i n t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (26 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

mu = 0 . 0mu2 = 0 . 0f o r v in va lue s :

mu += vmu2 += v * v

mu /= len ( va lue s )mu2 /= len ( va lue s )p r i n t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (27 / 36)

Example

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

mu = 0 . 0mu2 = 0 . 0f o r v in va lue s :

mu += vmu2 += v * v

mu /= len ( va lue s )mu2 /= len ( va lue s )p r i n t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (28 / 36)

ExerciseAdapt the code to ignore negative numbers.

va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

mu = 0 . 0mu2 = 0 . 0n = 0 . 0f o r v in va lue s :

i f v >= 0 . 0 :mu += vmu2 += v * vn += 1

mu /= nmu2 /= npr in t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (29 / 36)

ExerciseAdapt the code to ignore negative numbers.va lue s = [ 0 . 11 , - 0 . 23 , - 0 . 16 , 0 . 18 , 0 . 23 , 0 . 19 ]

mu = 0 . 0mu2 = 0 . 0n = 0 . 0f o r v in va lue s :

i f v >= 0 . 0 :mu += vmu2 += v * vn += 1

mu /= nmu2 /= npr in t ’ Average : {0} ’ . format (mu)p r in t ’ Std Dev : {0} ’ . format (mu2 - mu*mu)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (29 / 36)

Loops (II)Greatest Common Divisor (Euclid’s Method)

gcd(a,b) =

a if b = agcd(a− b,b) if a > bgcd(a,b− a) o.w.

a = 9344b = 6497

whi l e a != b :i f a > b :

a , b = a -b , be l s e :

a , b = a , b - ap r i n t a

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (30 / 36)

Loops (II)Greatest Common Divisor (Euclid’s Method)

gcd(a,b) =

a if b = agcd(a− b,b) if a > bgcd(a,b− a) o.w.

a = 9344b = 6497

whi l e a != b :i f a > b :

a , b = a -b , be l s e :

a , b = a , b - ap r i n t a

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (30 / 36)

Python So Far

Python...1 Basic types: int, float, list...2 Control flow: for, while, if, else, elif

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (31 / 36)

List Indexing

s tudents = [ ’ Luis ’ , ’ Rita ’ , ’ Sabah ’ , ’ Grace ’ ]p r i n t s tudents [ 0 ]p r i n t s tudents [ 1 : 2 ]p r i n t s tudents [ 1 : ]p r i n t s tudents [ - 1 ]p r i n t s tudents [ - 2 ]

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (32 / 36)

Tuples (I)

A = (0 , 1 , 2 )B = (1 , )

p r i n t A[ 0 ]p r i n t l en (B)

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (33 / 36)

Tuples (II)

Tuples are like immutable lists.

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (34 / 36)

Dictionaries

Dictionaries are associative arrays.

gene2ensembl = {}gene2ensembl [ ’SMAD9’ ] = ’ENSG00000120693 ’gene2ensembl [ ’ZNF670 ’ ] = ’ENSG00000135747 ’

p r i n t gene2ensembl [ ’SMAD9’ ]

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (35 / 36)

Dictionary Methods

gene2expre s s i on = {’SMAD9’ : 12 . 3 ,’ZNF670 ’ : 4 . 3 ,

}

p r i n t l en ( gene2ensembl )p r i n t gene2ensembl . keys ( )

Luis Pedro Coelho (luis@luispedro.org) (EMBL) ⋆ Python I ⋆ April 3, 2014 (36 / 36)