Python is an incredible simple yet powerful programming language that is widely used in Digital Humanities community. With Python scripting, one can instantaneously do work that would otherwise require hours of manual labor. In this post, I will explain the basics of Python 2.7. I chose version 2.7 because it is widely used and comes preinstalled with OS X. For this tutorial, I will assume that you have some basic understanding of the terminal and can do stuff like navigating through directories using OS X commands. (For more information, visit this tutorial.)
Before we start, make sure that you have Python downloaded and installed on your computer. To start Python, simply open Terminal (Applications -> Utilities -> Terminal) and type: python.
eduroam-174-25:HackingHumanities dangquang2011$ python
Your terminal should return a message similar to this:
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.
This means that your python is working and running. To exit from python back to command line, type:
Now that you know how to open and close python using the terminal, let’s proceed to doing more interesting stuff!
Strings are like sentences. The are written in code as a quoted sequence of characters. For example, “I love Digital Humanities” is a string.
Suppose the string you wanted to analyze was my favorite quote from Winnie-the-Pooh, “People say nothing is impossible, but I do nothing every day.” To let python know about this string, open Python and type:
mystring = "People say nothing is impossible, but I do nothing every day."
Note that mystring is just a variable name. You can replace it with any word of your choice. Variable names are simply identifiers through which you can later access your data. For instance, if you want Python to remind you of your sentence, type:
Print is the command that tells Python to return back objects in textual form to the terminal. Combined with an object, print produces a statement. The command above returns us the following statement:
“People say nothing is impossible, but I do nothing every day.”
Now, let’s add another string to python:
anotherstring = "If the person you are talking to doesn't appear to be listening, be patient." laststring = "It may simply be that he has a small piece of fluff in his ear."
And combine our three strings into one:
combinedstring = mystring + anotherstring + laststring
You can verify that combinedstring includes all of our sentences by using print.
Now, suppose you want to check whether some word (say, “fluff”) is part of combinedstring. This might seem silly since combinedstring consists of only 3 sentences, but is extremely handy when your string is of larger size (imagine having some 1000-page book as your string). To do this, type:
"fluff" in combinedstring
Which makes Python respond:
Obviously, replacing “fluff” with any other word in quotes would let you do the same with that word.