Most recent comments
Jogging og blogging
Are, 9 måneder, 1 uke
Liveblogg nyttårsaften 2016
Are, 9 måneder, 1 uke
Reading in dark times
Are, 11 måneder, 1 uke
Moldejazz 2016
Camilla, 1 år, 2 måneder
Karoline, 1 år, 3 måneder
Tor, 1 år, 4 måneder
Sony Smartwatch 3 review
Tor, 1 år, 4 måneder
Numerikk, takk
Tor, 1 år, 4 måneder
Topp tur
Camilla, 1 år, 6 måneder
Tolkien reading day
Tor, 1 år, 6 måneder
50 book challenge
Camilla, 9 måneder, 2 uker
Ten years ago
To ting på en gang

Python for humanities people, part I

I don't know if it's because I talk about programming a lot, or because digital humanities is all the rage and some programming skills seem like a good thing to have on one's resumé or if it's just intellectual curiosity, but for whatever reason, my wife, doctor of English literature, has decided that I should teach her to program. I've been thinking for some time that it would be fun to write a book to introduce humanities people to Python, so I'll be taking notes. If the notes are any good, and the project is a success, i.e., my wife learns to program, I might try to turn them into a manuscript. For the being, what you are currently reading is the first installment in my series of notes. Expect more at irregular intervals.

First things first: You need a terminal and a Python installation. If you are running linux, or have a computer form The House of Jobs, then you already have these things. If you are running Windows, I would suggest that you go home and rethink your life, but if that seems like too much effort you can also just go to the Python website and download a suitable version.

We'll start out by playing around in the Python shell, also known as the Python interpreter. To access this, you need to open your terminal application, and type
(and press enter. Whenever I say to type something, you type it and press enter.) You should see something like this:
Python 2.7.3 (default, Nov 19 2012, 00:46:55)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.

If instead you get a message along the lines of the command not being found, chances are you have misspelled python, or you don't have a working python installation. Refer to the installation instruction on the Python homepage for help.

Python is what's known as an interpreted language. What this means is that a python program is essentially just a text file, and when you run the program, the python interpreter will, you guessed it, interpret each line and perform the commands. The practical consequence of this is that you don't have to compile your code before you run it, and that you can open the python interpreter and manually give it one command at the time and immediately see what happens. And that's just what we are about to do.

In the python shell you opened two paragraphs ago, try typing a few simple calculations, for example like this:
>>> 1+1
>>> 3*2
>>> 8/4
>>> 3**2

We're not actually going to focus a lot on maths, though it will come in handy from time to time, so we're doing this mainly to get an idea of what the interpreter does. It interprets our statements, and prints the results. In the examples above, the statements where all pretty straightforward (except possibly the last one, 3**2 is python for 32), but we can also test more complicated commands in the same manner.

Maths isn't a whole lot of fun with just numbers, however, and just like in high school we can also use letters in our expressions in python. When a letter (or indeed a word) is used to represent a number in programming, it's called a variable. I've heard many metaphors which are meant to explain what a variable is, but as this is a book for grown ups, I'll just go ahead with the truth. A variable is something which can hold a piece of data, where that data can be for example a number or a piece of text. Technically, a variable holds some binary data (that's the 0s and 1s), or rather the memory address of that data, plus a recipe for how to interpret that binary data into something meaningful like a number or a piece of text. Mostly, though, you don't have to think too much about the technical details, so let's just move on to some examples:
>>> a = 1
>>> a + a
>>> b = 1.0
>>> b + b
>>> c = 'hello'
>>> c
>>> type(a)
<type 'int'>
>>> type(b)
<type 'float'>
>>> type(c)
<type 'str'>

What's happening here is first that I create a variable named a, which holds the number 1. I then perform the calculation a+a, the result of which is 2. Next, I create a variable named b, holding the number 1.0, and I calculate b + b. Finally, I create c, which holds the text 'hello'. The result of the statement c is then simply the value of c, i.e., 'hello'. Then, I check the types of these variables, and we see that a is an 'int', which is of course short for integer, b is a 'float', which is short for floating point number, while c is a 'str', which is short for string.These types, or data types, are the recipes that allow the binary data in memory to be interpreted into either a number or some text. I'm almost done talking about maths for a while now, but it might be worthwhile to note the difference between integers and floating point numbers. Floats are just numbers with decimals, although as we saw, the part behind the decimal point may be 0.

There's one additional data type we're going to need before things get interesting, and that's the list. A list, as the name suggests, is a list of data. The things in the list are knows as elements, and may be of any type. For example, a single list can contain both numbers, text and indeed another list (in fact, a list can contain itself as one of it's elements, though that's mostly a curiosity, I think). To create a list, we do thusly:
>>> a = [1,2,3,'elephant']
>>> a
[1, 2, 3, 'elephant']
>>> a[0]
>>> a[3]
>>> a[-1]

Here, I first create a list containing the elements 1, 2, 3 and 'elephant'. I then verify that the list contain these elements (this isn't neccessary, it's just for pedagogical reasons), after which I access some of the elements in the list by their indices (the square brackets are used both for creating a list, and for accessing elements of a list). The first thing to notice is that the first element of the list has index 0. This isn't the case in all programming languages (in some languages, like fortran, you can even choose what the first index should be), but python uses what is aptly name zero indexing. The result of this is that in a list with four elements, the index of the last element is 3. Note also that negative indices are allowed. When the index goes below zero, we simply start counting from the final element and forwards along the list. The bit about negative indices isn't terribly important to remember, except for the handy trick that the element with index -1 is always the last element of the list, no matter how many elements there are.

In the next installment, we're going to look at some other ways to create lists, as well as the for loop. Stay tuned.
Camilla likes this


Camilla,  07.04.13 09:09

you have an odd obsession with the word "elephant". Where does it come from? Is this customary for your people?