If you have a Mac or a Windows computer, the easiest way of getting Python together with all the most useful scientific libraries installed is to just install a Python Distribution. You can choose between:
Both of these are one-click installers containing Python and a collection of scientific libraries, including the Ipython Notebook that we use for the lecture.
If you are a Linux user, you may prefer to use the standard package manager to install the required packages. You will need at least python2.7
, ipython-notebook
, python-numpy
, python-scipy
, and python-matplotlib
.
In [2]:
print 'hello world'
In [3]:
Out[3]:
In [ ]:
print "Hello World!"
Run it with:
$ python hello.py
In [4]:
2 + 2
Out[4]:
In [5]:
4 - 2
Out[5]:
In [6]:
2.1 + 2
Out[6]:
In [7]:
3/2
Out[7]:
In [8]:
3.0/2.0
Out[8]:
In [9]:
2*3
Out[9]:
In [10]:
1+2j
Out[10]:
In [11]:
(3.0*10.0 - 25.0)/5.0
Out[11]:
In [12]:
3.085e18*1e6
Out[12]:
In [15]:
t = 1.0
a = 9.8
s = 0.5*a*(t**2)
print s
In [17]:
x = 1
x = x + 3
x
Out[17]:
In [ ]:
x *= 5
x
In [19]:
x1 = 10
x2 = 10
In [20]:
x1 == x2
Out[20]:
In [21]:
x1 < 3
Out[21]:
In [22]:
x1 >= 10
Out[22]:
In [25]:
x3 = 12
x1 < 11 or 11 >= x3
Out[25]:
In [26]:
x = "spam"
x
Out[26]:
In [27]:
print "hello\nworld!\tI said."
In [28]:
"hello" == 'hello'
Out[28]:
In [30]:
"hello" > "hallo"
Out[30]:
In [34]:
"hello" > "bike"
# this is a comment
def myfunc(x):
""" Prints x
:param x: stuff to print
"""
print(x)
In [41]:
In [32]:
y = """Multiline string are
in triple quotes
Indentation is preserved!
"""
print y
In [ ]:
s = "hello" ; e = "world"
print s + e
In [42]:
s = 'hello'
len(s)
Out[42]:
In [43]:
s[0]
Out[43]:
In [44]:
s[-1]
Out[44]:
In [45]:
s[1:3]
Out[45]:
In [46]:
s[2:]
Out[46]:
In [47]:
x = float('3.2')
y = 4
y+x
Out[47]:
In [51]:
y + float('3.2')
Out[51]:
In [52]:
l = [1, 2, 3, 4]
l
Out[52]:
In [54]:
l2 = [1, "hello", "x", 1/3., 4 > 2, l]
l2
Out[54]:
In [55]:
l[0]
Out[55]:
In [56]:
l[-2]
Out[56]:
In [60]:
l[1:3:1]
Out[60]:
In [59]:
l = range(11)
l
Out[59]:
In [61]:
l[0:8:2]
Out[61]:
In [64]:
l[::66]
Out[64]:
In [66]:
l
Out[66]:
In [67]:
l[0] = "world"
l
Out[67]:
In [69]:
l.sort()
l
Out[69]:
+
operator:
In [70]:
l2 = ['x', 'y', 'z']
long_l = l + l2
long_l
Out[70]:
In [71]:
l = [10, 7, 9, 1, 5]
l.sort()
l
Out[71]:
In [72]:
tel = {'John': 7192, 'Jim': 9871, 'Jan': 1253}
tel
Out[72]:
In [75]:
tel['John']
Out[75]:
In [76]:
tel['Marc'] = 2345
tel
Out[76]:
In [77]:
tel.keys()
Out[77]:
In [87]:
tel[[3,4,5]] = 'ladia'
In [85]:
tel
Out[85]:
In [ ]:
'John' in tel
In [ ]:
'Sam' in tel
In [90]:
myset = set(['a', 333, 'b', 'a'])
myset
Out[90]:
In [ ]:
'a' in myset
In [ ]:
222 in myset
In [91]:
if 2*2 == 5:
print 'I know that!'
In [93]:
if 2*2 == 4:
print 'I know that!'
In [94]:
if 2*2 == 4:
print 'I know that'
print 'Wrong indentation'
In [96]:
if 2*2 == 4:
print 'I know that'
print 'This closes the block'
In [98]:
x = 55
if x > 0:
print "positive"
elif x == 0:
print "zero"
elif x == 55:
print '55'
else:
print "negative"
In [100]:
x = 10
y = 20
if x < 0 or y < 100:
print "yes"
else:
print "no"
In [ ]:
y = 20
x = y+2 if y < 10 else 100
print x
In [101]:
for i in range(10):
print i
In [102]:
for i in [0,1,2]:
print i+2
In [103]:
l = ['x', 10, {'Mark': 1012}]
for francis in l:
print francis
In [104]:
tel_book = {'Jan': 1253, 'Jim': 9871, 'John': 7192}
for name in tel_book:
print name, 'telephone number is:', tel_book[name]
In [116]:
l = [(3,5), (4, 'per'), 5, 6]
for x, y in l:
print x, y
In [113]:
l = [3,4,5,6]
counter = 0
for value in l:
l[counter] = value +2
counter += 1
l
Out[113]:
In [111]:
for index, name in enumerate(tel_book):
print index, name
In [ ]:
for index, obj in enumerate(l):
print index, obj
In [ ]:
squares = [x**2 for x in range(10)]
squares
In [ ]:
squares = []
for i in range(10):
value = i**2
squares.append(value)
print squares
In [ ]:
squares = [x**2 for x in range(10) if x**3 < 50]
squares
In [ ]:
squares = []
for x in range(10):
val = x**2
if x**3 < 50:
squares.append(val)
print squares
In [117]:
# find all odd numbers whose square is smaller than 10000
# and whose cube is greater than 500
interesting_numbs = []
x = 0
while x**2 < 1e4:
if (x % 2 != 0) and (x**3 > 500):
interesting_numbs.append(x)
x += 1
print interesting_numbs
In [ ]:
# throw random numbers until we get one smaller than 0.2
from random import random
counter = 0
while True:
x = random()
counter += 1
if x < 0.04:
break
print 'After', counter, 'trials, I got', x
In [128]:
def double_and_z(x, z=10):
"""Return twice the input
:param x: the value to be doubled
:return: twice the amount of x
"""
# maybe use + instead of * ?
y = x*2 + z
return y
In [132]:
z = double_and_z(z=55, 10)
print z
In [120]:
double('hello world!')
Out[120]:
In [121]:
double(2+1j)
Out[121]:
In [ ]:
double()
In [ ]:
def double_improved(x, method='*'):
"""Return twice the input.
:param x: the value to be doubled
:param method:
*optional* argument specifying
the method to double `x`
:return: twice the amount of x
:raises:
ValueError in case the chosen method is not supported
"""
print 'Doubling using', method
if method == '*':
return x*2
elif method == '+':
return x+x
else:
raise ValueError('Method '+method+' not known!')
In [ ]:
double_improved(2)
In [ ]:
double_improved(3, method='+')
In [ ]:
double_improved('x')
In [ ]:
double_improved(10, '*')
In [ ]:
double_improved(1, '/')
In [ ]:
double({'a':1})
In [ ]:
def multiply(x, y):
"""Mutiply x by y"""
return x*y
def double_generic(x, method=multiply):
"""Return twice the input.
:param x: the value to be doubled
:param method:
*optional* argument, the actual function
that is applied to `x`
:return: twice the amount of x
"""
print 'Doubling using', method
return method(x, 2)
In [ ]:
double_generic(10)
In [ ]:
def donothing(x, y):
"""This function is useless"""
return x
In [ ]:
double_generic(10, method=donothing)
In [ ]:
help(double_generic)
In [123]:
help(enumerate)
In [122]:
double_generic?
In [ ]:
l = [3, 1, 2]
l.sort()
l
In [ ]:
"hello world".split()
In [ ]:
for num, word in enumerate("hello world. I am here".split()):
print "Word number",num, ':', word
In [127]:
s = "hello world" # hitting TAB will show:
s.replace('hello', 'good-bye')
Out[127]:
s.capitalize s.format s.isupper s.rindex s.strip
s.center s.index s.join s.rjust s.swapcase
s.count s.isalnum s.ljust s.rpartition s.title
s.decode s.isalpha s.lower s.rsplit s.translate
s.encode s.isdigit s.lstrip s.rstrip s.upper
s.endswith s.islower s.partition s.split s.zfill
s.expandtabs s.isspace s.replace s.splitlines
s.find s.istitle s.rfind s.startswith
In [ ]:
help(s.replace)
In [ ]:
s = "hello world"
s.replace("hello", "go away")
Let us first write a script, that is a file with a sequence of instructions that are executed each time the script is called.
The extension for Python files is .py. Write or copy-and-paste the following lines in a file called weather.py
In [ ]:
things = 'day, weather, life'
qualities = ['bad', 'really bad', 'terrible']
for thing in things.split(','):
for quality in qualities:
print 'This', thing.strip(), 'is', quality
message_in_the_bottle = "Euthanasia!"
print "You want:", message_in_the_bottle
You can execute the script interactively in ipython by running
%run weather.py
The local variables are available
In [ ]:
%run weather.py
In [ ]:
message_in_the_bottle
...or you can execute the script as a standalone program by running in a terminal
$python test.py
Create your own collection of functions: a module
Put the following functions in test.py
In [ ]:
def scream(x):
return x.upper()
def whisper(x):
return x.lower()
In [ ]:
import test
print test.scream('Is there anybody here?')
print test.whisper('I LIKE CAPS!')
In [ ]:
fh = open('lorem.txt', 'r')
In [ ]:
for line in fh:
print line[:10], '...',
words = line.split()
print len(words)
In [ ]:
for line in fh:
print "Still more lines!"
else:
print "No lines!"
In [ ]:
# rewind
fh.seek(0)
count = 0
for line in enumerate(fh):
count += 1
print count
In [ ]:
fh.close()
In [ ]:
fhw = open("tobewritten.txt", 'w+')
In [ ]:
for i in range(10):
fhw.write(str(i)+"\n")
fhw.flush()
In [ ]:
fhw.seek(0)
content = fhw.read()
In [ ]:
content
In [ ]:
content.splitlines()
In [ ]:
fhw.seek(0)
content = fhw.read()
lines = content.splitlines()
int_list = [int(i) for i in lines]
print int_list
In [ ]:
fhw.close()
The Batteries included!
SimpleHTTPServer
... to converting between color systems: colorsys
cProfile
... to secure hash and digest algorithms hashlib
threading
to subprocesses: subprocess
pickle
: The one battery you will need for sure – Python object serialization.
In [144]:
from pickle import dump, load
In [145]:
l = [1, 2, 3]
In [146]:
dumpfile = open('dump.pic', 'w')
In [147]:
dump(l, dumpfile)
In [148]:
dumpfile.close()
In [149]:
dumpfile = open('dump.pic', 'r')
In [150]:
x = load(dumpfile)
In [151]:
x
Out[151]:
In [ ]:
def print_fourth_element(mylist):
print 'The forth element is:'
print mylist[3]
def main():
a = 42
b = 'lorem ipsum'
c = 4+4j
mylist = [a,b,c]
print_fourth_element(mylist)
main()
In [ ]:
%debug
In [ ]:
import pdb
def lets_debug_this(idx):
pdb.set_trace()
print ('I am in iteration %d' % idx)
for irun in range(4):
lets_debug_this(irun)
In [ ]:
class MyClass(object):
def __init__(self, name='Custom Class'):
self.name = name
def print_name(self):
print 'My name is ' + self.name
myobject = MyClass('Example Object')
myobject.print_name()
In [142]:
import this
In [ ]: