An Introduction To Python From Harvard
An Introduction To Python From Harvard
An Introduction To Python From Harvard
• Downloads: http://www.python.org
• Documentation: http://www.python.org/doc/
• Free book: http://www.diveintopython.org
You could make the *.py file executable and add the
following #!/usr/bin/env python to the top to make it
runnable.
http://docs.python.org/modindex.html
• Downloads: http://numpy.scipy.org/
• Tutorial: http://www.scipy.org/
Tentative_NumPy_Tutorial
#!/usr/bin/env python
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
• Downloads: http://matplotlib.sourceforge.net/
• Downloads: http://www.stsci.edu/resources/
software_hardware/pyfits
• Sage: http://www.sagemath.org/
• Sage is a free open-source mathematics software system
licensed under the GPL. It combines the power of many existing
open-source packages into a common Python-based interface.
• You create a name the first time it appears on the left side of
an assignment expression:
! x = 3
>>> y
>>> x, y = 2, 3
>>> x
2
>>> y
3
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4
Name: x
Ref: <address1>
Type: Integer
Data: 4
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Ref: <address1>
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Ref: <address1> Type: Integer
Data: 4
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Ref: <address2> Type: Integer
Data: 4
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Ref: <address2> Type: Integer
Data: 4
immutable mutable
>>> x = 3 x = some mutable object
>>> y = x y = x
>>> y = 4 make a change to y
>>> print x look at x
3 x will be changed as well
a = [1, 2, 3] a 1 2 3
a
b=a 1 2 3
b
a
a.append(4) 1 2 3 4
b
2. Strings
• Immutable
• Conceptually very much like a tuple
3. List
• Mutable ordered sequence of items of mixed types
• Key difference:
• Tuples and strings are immutable
• Lists are mutable
• The operations shown in this section can be
applied to all sequence types
• most examples will just show the operation
performed on one
Omit the first index to make a copy starting from the beginning
of the container.
>>> t[:2]
(23, ‘abc’)
Omit the second index to make a copy starting at the first index
and going to the end of the container.
>>> t[2:]
(4.56, (2,3), ‘def’)
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’
Confusing:
• Extend takes a list as an argument.
• Append takes a singleton as an argument.
>>> li.append([10, 11, 12])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10, 11, 12]]
>>> li.sort(some_function)
# sort in place using user-defined comparison
• To convert between tuples and lists use the list() and tuple()
functions:
li = list(tu)
tu = tuple(li)
63
def times(x,y):
! return x*y
>>> func(1,2)
1 2 10 100
>>> func(1,2,3,4)
1,2,3,4
71
if x == 3:
print “X equals 3.”
elif x == 2: assert(number_of_players < 5)
print “X equals 2.”
else:
print “X equals something else.”
print “This is outside the ‘if’.”
x = 3 for x in range(10):
while x < 10: if x > 7:
if x > 7: x += 2
x += 2 continue
continue x = x + 1
x = x + 1 print “Still in the loop.”
print “Still in the loop.” if x == 8:
if x == 8: break
break print “Outside of the loop.”
print “Outside of the loop.”
import module
module.function()
>>> at = atom(6,0.0,1.0,2.0)
>>> print at
6 0.0000 1.0000 2.0000
>>> at.symbol()
'C'
class qm_molecule(molecule):
! def __init__(self,name="Generic",basis="6-31G**"):
!! self.basis = basis
!! super(qm_molecule, self).__init__(name)
86
>>> try:
... 1 / 0
... except:
... print('That was silly!')
... finally:
... print('This gets executed no matter what')
...
That was silly!
This gets executed no matter what
fileptr = open(‘filename’)
somestring = fileptr.read()
for line in fileptr:
print line
fileptr.close()
>>> a = 1
>>> b = 2.4
>>> c = 'Tom'
>>> '%s has %d coins worth a total of $%.02f' % (c, a, b)
'Tom has 1 coins worth a total of $2.40'