Sei sulla pagina 1di 12

Guided Exercises

Luis Pedro Coelho

Programming for Scientists

October 22, 2012

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

How do you access the rst element of a list?


Assume list is a list:
1. list[1]
2. list[0]

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

How do you access the last element of a list?


Assume list is a list:
1. list[1]
2. list(-0)

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

What is the dierence between the following two code examples:


A)
A = [1, 2, 3]
B = [1, 2, 3]
B)
A = [1, 2, 3]
B = A
Write a small piece of code (should be 2 or 3 lines) that behaves
dierently if you insert it after each of the two segments above.

Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (6 / 10)
Object Identity

What is the dierence between the following two code examples:


A)
A = [1, 2, 3]
B = [1, 2, 3]
B)
A = [1, 2, 3]
B = A
Write a small piece of code (should be 2 or 3 lines) that behaves
dierently if you insert it after each of the two segments above.
B[ 0 ] = 0
print A

Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (6 / 10)
sum

1. Learn about the built-in function sum


2. Write an implementation of this function

Luis Pedro Coelho (Programming for Scientists) Guided Exercises October 22, 2012 (7 / 10)
sum

1. Learn about the built-in function sum


2. Write an implementation of this function

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 ]

What does this print?


Why?

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)

Potrebbero piacerti anche