#
# 1. What is Python?
# A general-purpose programming language
# ... with good language support and libraries for scientific computing
# ... a simple syntax
# ... and it is free and open source
# 2. Why Python?
# - MATLAB costs money and has limited support for general-purpose programming and rough edges
# - C is difficult to write and has poor support for interactive programming
# - Other languages have poor library support (though R is good for statistics)
# - Great support for GPUs!
# 3. Installation
# - python.org for a bare-bones installation
# - Enthought Python Distribution: http://www.enthought.com/products/epd.php
# - Sage if you're at least basically comfortable with Linux/Unix (OS X)
# 4. Basic Resources:
# - python.org has documentation for standard libraries "batteries included"
# - Google it!
# - IRC: irc.freenode.net #python -- you will get yelled at but it will make you strong
# - Stack Overflow
# - Ask each other. Communication is the basis for all stable relationships.
# - Various books are available if you're into book learnin'. I have no recommendations.
# 5. Scientific Python
# - numpy is the basic matrix/vector library
# - scipy is a broader set of scientific functions -- everything from statistics to linear algebra to whatever
# - matplotlib is the basic plotting library. MATLAB plotting interface clone for the most part, so if you know that you'll get it fairly quickly
# ... google your needs, someone on the internet probably has what you're looking for.
# 6. Mostly, you should learn by doing. Pick a small first project and do it. You'll pick it up fast.
#
# basic math
3 + 4
3.0 + 4
3 ** 2
1/3
1.0/3
float(1)/3
from math import *
sqrt(2)
# http://docs.python.org/library/math.html
# strings
print "ABC"
print "ABC", "def"
print "ABC"+"DEF"
print 10*"ab"
print """abc
def"""
# http://docs.python.org/library/string.html
# http://docs.python.org/library/re.html for regular expressions
# variables
x = 2
print x
# lists
a = [1, 2, 3]
b = ["one", 2, "three"]
a[0]
a[0:1]
a[0:2]
a[-1:]
a[:-1]
len(a)
a.append(4)
a.extend(b)
del a[0]
for item in a:
print item
a2 = [x*2 for x in a]
# http://docs.python.org/tutorial/datastructures.html
# tuples
t = 12345, "hello!"
t = (12345, "hello!")
t[0]
t.append("noooo") # ooooooooo!
t1, t2 = t
# sets
menu = set(('apple', 'orange', 'apple', 'pear', 'banana'))
if 'orange' in basket:
print '1 orange please!'
elif 'banana' in menu:
print 'ok, a banana'
else:
print 'grr'
# dicts
tel = {'jack': 1, 'jill': 2}
tel['jack']
del tel['jack']
tel['john'] = 3
for name, number in tel.items():
print '%s: %s' % (name, number)
for name in tel.keys():
print name
for number in tel.values():
print number
for name, number in tel.iteritems():
print name, number
tel.items()
tel.iteritems()
# range and xrange
for i in xrange(10):
print i
for i in xrange(5, 10):
print i
[i for i in xrange(0, 10, 2)] # start, end+1, step
range(10) # eagerly created if you drop the x, so be careful
# while loops
i = 10
while i < 10:
print i
i += 1
# functions and conditionals
def clip(n):
if n <= 0:
return 0
else:
return n
print [clip(n) for n in xrange(-10, 10)]
#
# TEST YOUR UNDERSTANDING
# Write code to compute n iterations of the logistic map, f(x) = r x (1-x).
#
def defaults(a, b=10):
print a + b
defaults(5)
defaults(5, 1)
defaults(b=10, a=3)
# numpy
from numpy import *
x = array([1, 2, 3])
y = array([1.0, 2, 3])
z = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
x.shape
y.shape
z.shape
x + 1
x + y
z * z
dot(z, z)
z[:,1]
z[:,1] = 0
z[0:2,0:2] = 3
eye(3)
zeros(3)
zeros((3,3))
zeros((3,3,3))
zeros((3,3,3,3))
empty((1000,1000))
rand((10,10))
help(array)
vstack([hstack([a,b]),
hstack([c,d])])
# http://www.scipy.org/NumPy_for_Matlab_Users
# http://www.scipy.org/Tentative_NumPy_Tutorial
z = arange(10)
z = z.reshape(2, 5)
def add_5(x):
x += 5
add_5(z)
#
# TEST YOURSELF
# Write a Python program that exponentiates a square matrix of any dimension by diagonalization. The steps are:
# 1. Read in a text file in which you've specified a square matrix A;
# 2. Find A's eigenvalues (a vector l) and eigenvectors (a matrix V);
# 3. Apply the Python function exp() to the components of l and form a diagonal matrix L with those values on the diagonal;
# 4. Multiply the resulting matrix L with the eigenvector matrix from both sides, in this way: V L V-1. This undoes the diagonlization. It turns out that the result is the exponential of the matrix A; that is, eA = V L V-1.
# 5. Print the resulting matrix to a text file.
#
# TODO: plotting
# TODO: ahh
# TODO: make slides
# TODO: send summary and links