Ling 431/631: Corpus Linguistics

Ben Bergen

 

Meeting 12: The most useful thing you will ever learn, ever

November 5, 2007

 

Why program

 

Often you want to get something done that would be arduous to perform by hand, and for which there doesn't exist any easily accessible program. For example:

 

What programming is

 

Computers follow very explicit instructions; a program is a set of such instructions that tells the computer what operations to perform. A program is structured like a flow chart.

 

           

Conditional paths: If X then Y else Z  :                       Loops: Do X until or while Y:

 

A program takes input and produces output of various types - these can be input from the keyboard or a file, and output to a file, to the screen, etc.

 

>> print "Hello, world!"

            Hello, world!

            >> greeting = "Howzit."

            >> print greeting

            Howzit.

 

>> greeting = input("What greeting should I use?")

>> print greeting

[prints out whatever greeting you type in]

A couple of things have been implicit in these examples.

 

First, we've used data, namely strings of characters. Other types of data are numbers, arrays, etc.

>> print 2 + 2

4

 

Second, we've been using a variable, greeting, and assigning values to it using the equal sign.

>> radius = input("What is the radius of the circle?")

>> circumf = radius * 2 * 3.14

>> print "The circumference is", circumf

            [prints out circumference]

 

Third, we've been implicitly using operations like * and +. There's a huge number of useful predefined operations and functions available in modules that you can include in your program, like splitting a string at the white spaces, removing carriage returns, etc.

import sys        # will import the module called 'sys'

 

Operations can be performed conditionally, using if.

>> age = input("How old are you?")

>> if age > 29:

>>       print "You are not to be trusted."

>> else:

>>       print "You need to buy these expensive trendy jeans!"

            [prints out appropriate string]

 

A key mechanism in programming is the use of loops. A loop defines a criterion or set of criteria under which an operation or set of operations should be performed repeatedly. Python has while and for loops.

>> for form in "sneer", "rot", "yodel":

>>       print form, "is a regular verb"

sneer is a regular verb

rot is a regular verb

yodel is a regular verb

 

This means: For every element n in the list, print "n is a regular verb". The block inside the loop is executed once for each element, and each time, the current element is assigned to the variable form [in this case]. Another example:

 

>> for number in range(1,10):

>>       print "Today's number:", number

>> print "Ta-da!"

 


>> number = 0

>> high = input("How high should I count? ")

>> while number <= high:

>>       print number

>>       number = number + 1

>> print "Done."

 


You can create new functions that are used just like print and input. They sit outside the main flow of the program and can be called at any point in it.

>> def addfive(number):

            >>       new = number + 5

            >>       return new

>> print addfive(4)

9


 

Essential programming habits

Spaces, tabs, quotations, colons, etc. all mean something. Be as precise with your code as you have to be with regular expressions.

I guarantee that even if you know what your code does when you write it, you will forget immediately. Every programmer does. That's why all good programmers document their code thoroughly. I am not a good programmer, but even I document [nearly] every line. In Python, do this using the # sign. Anything after this on the same line will be ignored by the program

>> # this is a program that prints the sum of two numbers

>> a = 3         # set variable a to value 3

>> b = 5         # set variable b to value 5

>> print a + b  # print their sum

8

Test every little change you make to the program, using print calls to tell you that the change is doing what you want it to. This makes it way easier to figure out what's going wrong.

>> a = 3         # set variable a to value 3

>> print a

 

What you need

 

The current Python release [2.5.1]. Includes a piece of software called IDLE, which is an Integrated Development Environment. This is a special text editor that knows a lot about Python, linked to an output window where you can see immediate results of your programming.