40
Introduction to Python Programming Luis Pedro Coelho § On the web: http://luispedro.org On twitter: @luispedrocoelho European Molecular Biology Laboratory April 3, 2014 Luis Pedro Coelho ([email protected]) (EMBL) Python I April 3, 2014 (1 / 36)

Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Embed Size (px)

Citation preview

Page 1: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (1 / 36)

Page 2: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Python

Let’s digress for a moment discussing the language…

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (2 / 36)

Page 3: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (3 / 36)

Page 4: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Popularity

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (4 / 36)

Page 5: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Popularity

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (5 / 36)

Page 6: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (6 / 36)

Page 7: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

What is a Computer?

...1 Memory

...2 Processor

...3 Magic

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (7 / 36)

Page 8: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Python Model

...1 Objects

...2 Operations on objects

...3 Magic

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (8 / 36)

Page 9: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Python Example

pr in t ” He l lo World”

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (9 / 36)

Page 10: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (10 / 36)

Page 11: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Computer Program

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (11 / 36)

Page 12: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Running a Program

...1 Shell

...2 IDE

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (12 / 36)

Page 13: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Let me show you a demonstration…

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (13 / 36)

Page 14: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

More Complex Example

What is 25 times 5?

pr in t 25 * 5

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (14 / 36)

Page 15: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

More Complex Example

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (14 / 36)

Page 16: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

More Complex Example

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (15 / 36)

Page 17: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Blackboard demonstration

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (16 / 36)

Page 18: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Conditionals

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

e l s e :<statement 3>

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (17 / 36)

Page 19: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (18 / 36)

Page 20: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (19 / 36)

Page 21: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (20 / 36)

Page 22: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (21 / 36)

Page 23: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Loops

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

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (22 / 36)

Page 24: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (23 / 36)

Page 25: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (23 / 36)

Page 26: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (24 / 36)

Page 27: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (25 / 36)

Page 28: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (26 / 36)

Page 29: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (27 / 36)

Page 30: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (28 / 36)

Page 31: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (29 / 36)

Page 32: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (29 / 36)

Page 33: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (30 / 36)

Page 34: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (30 / 36)

Page 35: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Python So Far

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

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (31 / 36)

Page 36: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (32 / 36)

Page 37: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (33 / 36)

Page 38: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Tuples (II)

Tuples are like immutable lists.

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (34 / 36)

Page 39: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

Dictionaries

Dictionaries are associative arrays.

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

p r i n t gene2ensembl [ ’SMAD9’ ]

Luis Pedro Coelho ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (35 / 36)

Page 40: Introduction to Python Programming - Luis Pedro Coelholuispedro.org/files/talks/2014/04-sdu/python-01.pdf · Introduction to Python Programming LuisPedroCoelho ... v = 3.3*9.2 if

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 ([email protected]) (EMBL) ⋆ Python I ⋆ April 3, 2014 (36 / 36)