Tuesday, August 16, 2011

Common Python Questions

Where can I get Python?
You can download Python from the official website. However, I recommend that you consider the Enthought Python Distribution, which includes very useful Python packages.
What version should I download?


Anything above 2.7 should be fine.


How can I get started?


It is important to realize that Python has excellent online documentation. Begin with sections 3, 4, and 5 of the Python tutorial. Do not just read the tutorial: work through it using the interactive Python interpreter. Your constant companion should be the Python Library Reference. If you have no programming experience at all, perhaps the First Steps tutotial will be useful. Be sure to be careful about operator precedence.


How do I get colorized Python code when I am programming?


Use an editor that supports syntax highlighting. I like Vim


How do I copy a set?


Look at the documentation for the set types. There you find that sets have a ``copy`` method, so if ``s`` is a set you can produce a copy as ``s.copy()``. You also find that sets have an ``add`` method, so if ``s`` is a set and ``x`` is an element you wish to add, you can do so as ``s.add(x)``.


What is a boolean type?


Python has a special Boolean type, represented by the constants ``False`` and ``True``. They are special constants, like 0 and 1. These are not strings! A string is only false if empty. Compare:

>>> bool('')
False
>>> bool('False')
True
>>> bool(False)
False



How do I create a NumPy matrix?


After you install Python, you need to download and install NumPy. (Unless you use a distribution that includes NumPy.) Once it is installed, you can instantiate the matrix class in one of the following ways:

#best way (but least convenient)
import numpy
x = numpy.mat('1 2; 3 4')

#2nd best: import just the command you want
from numpy import mat
x = mat('1 2; 3 4')

#most common and least safe: import all numpy commands
from numpy import *
x = mat('1 2; 3 4')

That final way is used in the Numpy tutorial and in the
NumPy examples. You will also want to look at the NumPy Book.

Getting Started with Python (Useful Books)

Tuesday, August 31, 2010

Numerical Solutions of Linear Equations

Given the linear system Ax=b, we often represent the solution as x=A-1b. But this does not imply you should actually compute an inverse when you need to solve a system. John Cook offers a short explanation of why you should avoid inversion. For example, if you import NumPy's linear algebra module as la, then you can use la.solve(A,b) instead of la.inv(A)*b.

Wednesday, August 11, 2010

Introductory Announcements

There will be an introduction to Python programming in the SSRL on Friday, August 27, from 5:30-7:00pm. Please be punctual.
Course materials, including the syllabus and course notes, are available via Blackboard. Be sure to sign up for the class mailing list by following the directions on the syllabus.
All software used in this course should be available in the SSRL. If you want to work at home, please install the free and open source Python 2.6.5 and Numpy 1.4.1. (You can also find these bundled together in the Enthought Python Distribution.) Additionally, you will need the commercial Scientific Notebook, which you will use to write up your homework assignments. (SN is reasonably priced, but MacKichan additionally offers cheap short-term licenses.)

Monkeynomics

Laurie Santos's 2010 TED talk provides some interesting insight into the biological basis of human decision making strategies. Dr. Santos is a bit quick to call behavior “irrational” when it deviates from classical predictions, but that does not diminish the interest of her results.