Week 1 materials

General comments/editorializing on Python.

def foo(x):
    """
    Yes, this is a dumb function...
    """
    if x is None:
        raise TypeError("x cannot be None!")
    try:
        return int(x)/2
    except:
        return None

Comments on Spector’s notes.

#importing from __future__ 
#must be at the VERY top
#of yours scripts, meaning
#BEFORE any other imports
from __future__ import print_function

#In Py2, you'd say print x, but
#that is now deprecated
print(x)

Similarly, there is only one integer type in Py3. For example, this Py2 code no longer works:

#Assign x to be an "unlimited" precision integer.
#type(x) would say 'long'.
x = 100L

Py3 has but one integer type:

#This is type 'int'
x=100

Other relevant materials:

Lab/homework exercise.

Write a Python script to count number of occurrences of every word in a file. Do it idiomatically, using the Python standard library. The file to parse is the GNU General Public License, version 3, aka GPL3.

To obtain a plain-text version of the GPL3, we need some tools that JJ hopefully covered in part 1 of this course :). You may use the wget utility to get the file:

wget https://www.gnu.org/licenses/gpl-3.0.txt

If ‘wget’ doesn’t exist (which is the default case on Apple’s OS X system), use curl instead:

curl https://www.gnu.org/licenses/gpl-3.0.txt > gpl-3.0.txt 

To accomplish this task you need to figure out how to:

Advanced: do your work in a git repo. Optionally, get a github account and put all of your work there.

More info/resources about Python

Larry asked about efficiency of things like lists. Efficiency is an important topic in general. Fortunately, Python is relatively efficient. That said, there are situations where you run into performance bottlenecks. Much like R, you can write extensions to Python using C/C++ (Py2 docs and Py3 docs). Python’s C application programming interface, or API, is quite nice. However, this is the hard way to go: you need to know C/C++. Another option is to use Cython, which allows you to write Python code that gets turned into the equvalent C code. It also allows you to functions from other C/C++ libraries in Python by generating Python wrappers to the libraries. We use it quite a bit in the lab.