print 'hello world' 2+2 #illustration of for loop and range count =0 for i in range(10): if i != 0: print '+', else: print ' ', print i count += i print "---\n",' '+str(count) #illustration of strings as lists string = '' alphabet='abcdefghijklmnopqrstuvwxyz' for i in range(1,10): string += alphabet[-i] print len(string),string #illustration of lists and dict list=[] dict={} string='' for i in range(1,10): string += alphabet[-i] list.append(string) list += [string] dict[string] = i print list,"\n",dict list[3] dict['z'] #dict values can be any object dict['a']=['a1','a2','a3','a4'] dict['b'] = ['a'+str(j) for j in range(10)] dict['c']={} dict['c']['xyz']='alpha' dict #dicts can be sorted on keys score={'Alice':90,'Bob':65,'Eve':95,'Charles':75} for name in sorted(score.keys()): print name,score[name] print '\n','average=',sum(score.values())/len(score) #or on values for name in sorted(score.keys(),key=score.get,reverse=True): print name,score[name] print '\n','average=',sum(score.values())/len(score) #http://matplotlib.org/examples/api/unicode_minus.html plot(10*np.random.randn(100), 10*np.random.randn(100), 'o') title('Some Random Data') data=(1,3,8,6,4,2) plot(range(len(data)),data,label="first") moredata = -array(data) plot(range(len(moredata)),moredata,label="second") grid(True) xlabel('time') ylabel('Gb used') legend() #http://matplotlib.org/examples/pylab_examples/histogram_demo.html mu, sigma = 100, 15 x = mu + sigma*np.random.randn(10000) # the histogram of the data n, bins, patches = hist(x, 50, normed=1, facecolor='green', alpha=0.75) # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) l = plot(bins, y, 'r--', linewidth=1) xlabel('Smarts') ylabel('Probability') title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') axis([40, 160, 0, 0.03]) grid(True) #savefig("kln.png", dpi=72)