In [1]:
print "hello world"
hello world
In [2]:
a='hell'
b='o, w'
c='orld'
print a+b+c
hello, world
In [3]:
d=a+b+c
In [4]:
print d+'!'
hello, world!
In [5]:
d[3:6]
Out[5]:
'lo,'
In [6]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [7]:
arange(10)**2
Out[7]:
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
In [13]:
figure(figsize=(8,6))
plot(arange(10))
plot(arange(10)**2,'o-r')
legend(['y=x','$y=x^2$'],loc='upper left')
grid('on')
xlabel('x')
ylabel('y')
title('simple graph');
In [14]:
legend?
In [17]:
#see how many of 100 coin flips come up heads
#rand(N) is a list of 100 random numbers between 0 and 1
#sum() in this case counts the number of times the condition is True

sum([r < .5 for r in rand(100)])
Out[17]:
46

stdev for this is sqrt(N*p(1-p))=sqrt(100/4) = 5, so by "68-96-99.7 rule":
roughly 68% of the time between 45 and 55
95.5% of the time between 40 and 60,
and 99.7% of the time between 35 and 65 .
Let's do T=100,000 trials to confirm, then draw a histogram:

In [18]:
T=100000
data=[sum([r < .5 for r in rand(100)]) for t in xrange(T)]
for s in 5,10,15:
print '50 +/-',s,len([d for d in data if 50-s <= d < 50+s])/float(T)
50 +/- 5 0.68044
50 +/- 10 0.95458
50 +/- 15 0.99714
In [19]:
hist(data,bins=range(0,101,5))
xlim(30,70);
In [ ]: