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
His notes violate a fundamental tenet of programming, which is “know your standard library.” (The “standard library” is the set of types, functions, etc., that come with the language by default, meaning they are available without need for any third-party add-ons.) For example, his discussion of counting (section 4.9 of his notes) is “non-Pythonic”, meaning he is hand-rolling a solution when a better, safer, and more idiomatic method is available in the langauge’s standard library. To be fair, he is doing this for pedagogical reasons, but reinventing the wheel is a bad habit that a few seconds with Google can usually help you avoid.
They are Python2-centric. The current language standard is Python3. See here for Py2/Py3 differences. Many of these differences will lead to failure under Py3. The big one is “printing”, which is now a function in Py3. To save your sanity, get used to saying this in your PyX scripts:
#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
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.
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.