Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (1 / 10)
Goals for this hour
A quiz
Do a few exercises.
Play around.
You can work alone, in pairs, in triples,
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (2 / 10)
Lists I
3. list[-1]
4. list(0)
5. list(-1)
6. list(1)
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (3 / 10)
Lists II
3. list[-1]
4. list(-1)
5. list(1)
6. list[-0]
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (4 / 10)
Exercises
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (5 / 10)
Object Identity
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (6 / 10)
Object Identity
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (6 / 10)
sum
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (7 / 10)
sum
d e f sum ( xs , s t a r t=0 ) :
s = sum ( xs , s t a r t=0 )
Returns t h e sum o f a l l v a l u e s i n xs + s t a r t ( wh
f o r x i n xs :
s t a r t += x
return start
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (7 / 10)
numbers = s e t ( [ 1 , 2 ] )
f o r i i n xrange ( 5 ) :
numbers . add ( i )
p r i n t l e n ( numbers )
This prints:
7
6
5
4
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (8 / 10)
Numpy
import numpy a s np
X = np . a r r a y ( [ 0 , 1 , 2 , 1 , 2 , 1 , 2 , 1 ] )
X += 0 . 1
p r i n t X[ 0 ]
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (9 / 10)
Matplotlib
import numpy a s np
from m a t p l o t l i b import p y p l o t a s p l t
X = np . l i n s p a c e ( - 4 , 4 , 100 )
Y = np . exp ( . 5 -X*X)
1.8
1.6
1.4
1.2
1.0
expx22
0.8
0.6
0.4
0.2
0.0 4 3 2 1 0 1 2 3 4
x
Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (10 / 10)