Python Programming - Exercises
Python Programming - Exercises
DTU Compute
Technical University of Denmark
Installation
Install Python
$ python
>>> import simplejson
>>> import feedparser
>>> import cherrypy
>>> import pymongo
>>> import nltk
>>> nltk.download()
>>> from nltk.corpus import brown
>>> brown.words()
[The, Fulton, County, Grand, Jury, said, ...]
Install Python
Start with:
ipython -pylab
In [1]: plot(sin(linspace(0,8,100)))
http://www.student.dtu.dk/faan/cgi-bin/helloworld
~/public_html/cgi-bin/
Extra task
Note that this requires the installation of a SQL database. One of the
line in the bonus-sqlobject.py file states:
If you dont want to install MySQL try installing the simpler sqlite and
its python support and then change the connection line.
Install spyder
General Python
>>> ishashad(81)
True
Dictionaries
List example:
l = [a, b, f, f, b, b]
c = {a: 1, b: 3, f: 2}
Recursion
>>> factorial(4)
24
(4! = 1 2 3 4 = 24)
Classes
1 2
3 4
Project Euler
Encoding
UTF-8 encoding/UNICODE
https://raw.github.com/gist/1035399
Make a correction.
See also:
http://finnaarupnielsen.wordpress.com/2011/06/20/simplest-sentiment-analysis-in-python-
with-af/
UTF-8 encoding/UNICODE
Translate the AFINN sentiment word list with a language translation web
service, or perhaps just a part it to a language you know and see
if it works with with a couple of sentences.
Numerical python
1 2
3 4
Matrix rank
Function header:
Statistical distributions
Generate 10000 sets with 10 Gaussian distributed samples, square each
element and sum over the 10 samples. Plot the histogram of the 10000
sums together with the teoretically curve of the probability density func-
tion.
2
10 PDF from the pdf() function in the scipy.stats.chi2 class
Coauthors
Plot the largest connected component part of the network with NetworkX.
Text mining
Try both with the re module as well as with a function from nltk.
Email mining
Web serving
Create a web service that will take a series of numbers and model the
data, e.g., with a linear model.
You can, e.g., use the below pointer for the class which makes the com-
putation.
unimodeler.py
Pandas
Read in the assignment results Excel sheets (available under File Sharing
in CampusNet) with Pandas into several dataframes.
Produce a table/matrix of scatter plots of the score results for the dif-
ferent.