Python for Dummies

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.)

Preparation

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:

exit()

Now that you know how to open and close python using the terminal, let’s proceed to doing more interesting stuff!

Strings

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 mystring

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:

True

Obviously, replacing “fluff” with any other word in quotes would let you do the same with that word.

Conclusion

I hope that this simple tutorial gave you basic understanding of Python. From here, if you are interested in exploring it further, I recommend visiting this, or this tutorial.

 

Quang Tran

7 Comments

  1. Interesting tool you covered here! I’m glad how you introduced a new technology with such detailed steps. Things are easy to follow and all easy to understand! If an opportunity comes to use Python, I would definitely look back at this tutorial.

  2. I’m glad you picked Python, as I’ve been searching for something like this! Your tutorial provides a good basis for learning the language. I’m sure others will benefit from this as, like you said, it is so wide-used in the DH community. Your examples were extremely helpful.

  3. I find your tutorial very easy to follow. I already have some background knowledge in Python, but when I was initially trying to delve into the world of programming languages, a lot of the tutorials I found were confusing and too technical. Your however, tutorial simplifies it, and makes it understandable for beginners to follow through.

  4. Really easy-to-follow, focused, and well written tutorial. For someone who was completely new to programming, this would probably be a great place to start! Talking about only strings was a great move–you can introduce some of the basic parts of python, like variable names, operators, and booleans, really easily like that. Nice work!

  5. When I first saw the title of the post, I was somewhat hesitant as to how it would be executed simply because of my preconceived notions (aka my fears) of Python. However, after reading your tutorial, I definitely want to learn more. Thanks for the super concise and clear post!

  6. I’m glad you chose Python as a tutorial! I’ve always found it to be one of the easiest languages to learn, so it fits really nicely into this tutorial. You outline some of the basic commands well, and after doing something so easily, it’s likely anyone would want to learn more about how Python works.

  7. Quang,

    I agree with the other comments that this was a very clear and accessible introduction to Python, which you rightly stress is a widely used language in the DH community. Your introduction effectively established the use case and your target audience, by stating what knowledge you assumed in your readers and linking to a shell tutorial for those who need it.

    Focusing on strings was wise, and I like that you included a use case for how you could do some basic text analysis by checking to see if a word was contained within a string. It might have been helpful to include a few sentences at the end to preview more concretely what else Python can do and how it might be used for DH, but I like the tutorials you linked to for further reading. For Digital Humanities in particular, I would add the list of python tutorials at the Programming Historian, which discuss a number of standard DH methods like web-scraping and text analysis.

    Thanks for sharing this intro to Python for Dummies!

Leave a Reply to Austin Cancel reply

Your email address will not be published. Required fields are marked *