Getting Python if you don’t already have it
If you use Linux or Mac OS, you already have Python, and you can run them by typing python scriptname.py in the terminal. If you use Windows, you will need to install Python and probably a graphical interface to use with it. To download python for windows, your RA should go to this page and click on “Python 2.7.3 Windows Installer”, and download and install it. Next, go to this page and click on “pywin32-218.win32-py2.7.exe” to download the interface, and then install that. Then run PythonWin to open the graphical interface and use Python.
If you want to learn more about programming in Python beyond the basics introduced on this page, a list of free and open-source guides, books, exercises, etc. is compiled here.
Types
Python has several different variable types, including these:
- integer: a whole number or a negative whole number. For example: 3
- float: a number with a decimal point. For example: 3.1
- bool: True or False. For example: True
- string: some text which is indicated by single or double quotes. For example,
'I am a string.'
is a string. - list: an ordered list of numbers, strings, other lists, or anything. For example,
['I', 'am', 'a', 'string.']
is a list containing four strings. List items can be referred to by their index (starting at 0), i.e.,l[2]
is the third item in the listl
andl[-2]
is the second-to-last item.l[:2]
is the list containing the first two items, andlist[1:]
contains all but the first item in the list. The same methods work with strings. - dictionary: an unordered collection of keys and entries, indicated with curly brackets. If you supply the key, the dictionary will “look it up” and give you the entry. The key can be a string and the entry can be anything. For example
population
is a dictionary with four entries:
population = {'Toronto': 5202300, 'Montreal': 3607200, 'Vancouver': 2173100, 'Ottawa': 1145500}.
If you typepopulation['Ottawa']
, python will respond1145500
.
Loops and if statements
Conceptually, these are the same as their counterparts in the Praat scripting language and R. The syntax is somewhat different. Here is a simple for loop:
for i in range(4): print i
range(4)
is equivalent to the list [0, 1, 2, 3]
, so for i in range(4):
is like for i from 0 to 3
in the Praat scripting language. In Python you can loop through any list, whether or not it is a sequence of numbers:
list = ['a', 'list', 'of', 'words'] for word in list: print word
This is equivalent, using the list indices instead of the list items:
for i in range(len(list)): print list[i]
This is a simple way to get the list items and their indices at the same time:
for i,word in enumerate(list): print i, word
Here is how if statements are formatted in Python:
threshold = 0.5 n = 0.25 if n > threshold: print n, 'is greater than', threshold elif n == threshold: print n, 'is equal to', threshold else: print n, 'is less than', threshold
Whitespace
Unlike R and the Praat scripting language, Python requires whitespace to indicate the levels of loops and if statements. Because it uses whitespace to parse loops, it doesn’t require endwhile or curly brackets.
Reading and writing files
Python is very handy for reading text files, working with the contents of the filea, and writing the results to a new text file. Do something like this to open untitled.txt for reading:
myfile = open('/home/jeff/folder/untitled.txt')
or
myfile = open('C:/folder/untitled.txt')
To do anything with the contents of the file, read its lines into a list (we’re naming it mydata):
mydata = myfile.readlines()
Now you can work with it like any other list. mydata[-1]
will return the last line of the file.
To open untitled.txt in order to add to it, do this:
myfile = open('/home/jeff/folder/untitled.txt', 'a')
‘a’ is for “append”. This makes open()
work like Praat’s fileappend
. If you want to create a new file, or overwrite the contents of an existing file, use 'w'
for “write”. This is like filedelete
+ fileappend
in a Praat script:
myfile = open('/home/jeff/folder/untitled.txt', 'w')
Once myfile
is open for appending/writing, you can add lines to it like this :
myfile.write('a line with a carriage return at the end\n')
The '\n'
at the end is the “newline” string, like Praat’s 'newline$'
Built-in functions
Python has a lot of useful built-in functions. For example, sum([1,1,1])
returns 3
.
Here are some functions to use with strings:
'antelope'.replace('elope', '')
returns the stringant
.'string with spaces'.split(' ')
returns the list['string', 'with', 'spaces']
.'antelope'.startswith('a')
returns the boolTrue
.
Here are some functions to use with lists:
'_'.join(['string', 'with', 'spaces'])
returns the string'string_with_spaces'
.'string with spaces'.split(' ')
returns the list['string', 'with', 'spaces']
.l = ['a','b','c']
andl.append('d')
will result in the listl
being [‘a’,’b’,’c’,’d’].l.sort()
will changel
so that its items are in alphabetical or numberical order.
Here are some functions to use with dictionaries:
dict.keys()
returns the list of keys for the dictionarydict
.dict.values()
returns the list of values for the dictionarydict
.dict.has_key('Ottawa')
returns a bool reflecting whether'Ottawa'
is one of the keys ofdict
.
Defining functions
A powerful feature of Python and many other computer languages is the ability to define your own functions. In this case the name of the function is reduplicate
, and it takes the argument original
, which is expected to be a string, and the result of calling the function is a new string. The text in triple quotes is a description of what the function does:
def reduplicate(original): ''' apply shm-reduplication to words that start with one consonant ''' new = original+', shm'+original[1:] return new
This is the command to call the function reduplicate() with ‘history’ as its argument, and print the result:
print reduplicate('history')
Importing modules
Python has many specialized modules that can be loaded at the beginning of a script. These are like libraries in R. You can open one or more of them with the same import command.
import re import os, sys import math, random