1 introduction to python ling 5200 computational corpus linguistics martha palmer
TRANSCRIPT
1
Introduction to Python
LING 5200Computational Corpus LinguisticsMartha Palmer
LING 5200, 2006 BASED on Kevin Cohen’s LING
52002
What's a programming language? Structural elements Way of converting a text file to
instructions for the machine
LING 5200, 2006 BASED on Kevin Cohen’s LING
52003
What's a programming language? lexicon syntax
Vs. natural languages: no ambiguity
LING 5200, 2006 BASED on Kevin Cohen’s LING
52004
What does a program do?
Take in data (input) Do something with it (processing) Produce output
LING 5200, 2006 BASED on Kevin Cohen’s LING
52005
What does a program do?
Input your regex one or more files switches
For each line in each file, determine whether or not it matches your regex
Tell you about it
egrep '^[0-9]+\/' epw.cd
LING 5200, 2006 BASED on Kevin Cohen’s LING
52006
Producing output
print "hello, world"
LING 5200, 2006 BASED on Kevin Cohen’s LING
52007
Producing output
print "hello, world"
verb
LING 5200, 2006 BASED on Kevin Cohen’s LING
52008
Producing output
print "hello, world"
noun (object)
LING 5200, 2006 BASED on Kevin Cohen’s LING
52009
Producing output
#!/usr/local/bin/python
print "hello, world"
LING 5200, 2006 BASED on Kevin Cohen’s LING
520010
Producing output
Filename: helloWorld.py What do the file's permissions need to
be?
LING 5200, 2006 BASED on Kevin Cohen’s LING
520011
Producing output
babel>python helloWorld.py
hello, worldbabel>
LING 5200, 2006 BASED on Kevin Cohen’s LING
520012
Producing output
#!/usr/local/bin/python
print "hello, world\n";
"escape" character
LING 5200, 2006 BASED on Kevin Cohen’s LING
520013
Producing output
\t tab \n "newline"
LING 5200, 2006 BASED on Kevin Cohen’s LING
520014
Comments
#1 use for comments: adding notes to yourself/other programmers
# the purpose of this program
# is to print "hello, world" to
# the screen.
# author: [email protected]
# 303-492-1300
LING 5200, 2006 BASED on Kevin Cohen’s LING
520015
Comments
#!/usr/local/bin/perl
# the purpose of this program
# is to print "hello, world" to
# the screen.
# author:[email protected]
# 303-492-1300
# do the actual printing
print "hello, world\n"
"Commenting" your code
LING 5200, 2006 BASED on Kevin Cohen’s LING
520016
Comments
Other use: causing Python to ignore a line
# print "hello, world\n";print "goodbye, cruel world\n";
"Commenting out" a line of code
LING 5200, 2006 BASED on Kevin Cohen’s LING
520017
Comments
Own-line or end-of-line formats
# print it
print "hello, world\n"
print "hello, world\n" # print it
LING 5200, 2006 BASED on Kevin Cohen’s LING
520018
Producing output
I'd like to print something different every once in a while…
$name = “Martha”
print name
LING 5200, 2006 BASED on Kevin Cohen’s LING
520019
Variables
Name Contents Location in memory
LING 5200, 2006 BASED on Kevin Cohen’s LING
520020
Variables
Name (name) Contents (Kinder) Location in memory (13025)
$name
LING 5200, 2006 BASED on Kevin Cohen’s LING
520021
Using +
name = “Martha"
print "hello, ” + name
LING 5200, 2006 BASED on Kevin Cohen’s LING
520022
Python for Linguists
http://mime.colorado.edu/~bethard/Python%20for%20Linguists/Python-1.pdf