introduction to computational linguistics programming i
TRANSCRIPT
Introduction to Computational Linguistics
Programming I
Resumé Algorithm: A well defined procedure for
the solution of a given problem in a finite number of steps.
Program: A set of instructions, written in a specific programming language, which a computer follows in solving a problem.
Control Structures specify how instructions are followed
Examples ofControl Structures Sequence: a series of instructions that
are executed in order. Branch: an instruction involving a
condition that affects the next instruction executed.
Loop: for handling repeated execution. Module: a named collection of
instructions that we write once but use in different contexts
Sequence
Branch
Loop
Module
Python
Python is a programming language that allows us to write programs. Python programs have to obey certain
syntactic conventions Python is also an interpreter, i.e. a
machine which understands the conventions of the language
Python is also an Interpreter that Understands Python Programs
PROGRAMWRITTEN
IN PYTHON
PYTHONINTERPRETER
OUTPUTINPUT
Here is the World’s SimplestPython Program
print “Hello World”
When this program is run (interpreted) it causes “Hello World” to be printed on the screen.
Demo
The print statement
The print statement consists of the word print followed by one or more arguments separated by commas:
>>> print "a", "b", "c"abc
In this case the arguments are strings.
Strings
In Python strings are enclosed in either single quotes or double quotes.
'abc' "abc" "my name is" "Mike's name is"
Multi-line programs
Here is a program consisting of a sequence of three instructions
print "My name is "print "Mike"print "!"
Data Data is the raw material that programs
manipulate. Data comes in different flavours called
types Examples of data types are
integer strings
Each type of data is associated with characteristic operations.
Examples of operators
Integers are associated with operators such as +, -, * etc.
Using these operators we can write down complex expressions
e.g. 2+2; 2-2; 2*2
Other Arithmetic Operators
Operation Symbol Example
Exponentiation ** 5 ** 2 == 25
Multiplication * 2 * 3 == 6
Division / 14 / 3 == 4
Remainder % 14 % 3 == 2
Addition + 1 + 2 == 3
Subtraction - 4 - 3 == 1
Arithmetic expressions a number: 1 a variable: b an expression involving an operator
a + 1 a complex expression
a + 3*2 / 3 to eliminate ambiguity complex
expressions can use parentheses (a + (3*2)) /3
Exercise 1.1
Discover the value of the following arithmetic expressions using the python interpreter
1 + 2/3 + 4 (1 + 2) / (3 + 4) 100 % 10 115 % 10
Multi-line program using arithmetic expressions
print "2 + 2 is", 2+2
print "3 * 4 is", 3 * 4
print 100 - 1, " = 100 – 1"
print "(33 + 2) / 5 + 11.5 = ",(33 + 2) / 5 + 11.5
What is the output of this program?
Python Editor Python allows us to edit, save, check
and run programs. In python shell, go to File menu. Select New Editor window appears Type program into editor window. To check or run program – go to Run
menu and select check or run. You will be prompted to save file. Any filename is OK, but filename.py is best.
Exercise 1.2
Use editor to write programs that print your name and date of birth
on same line on separate lines
print the numbers from 0 to 4 Save the work in different files Run the programs