Programming for Linguists An Introduction to Python.

Slides:



Advertisements
Similar presentations
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Program Design and Development
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
JavaScript, Third Edition
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to C Programming
Python.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Programming for Linguists An Introduction to Python.
Programming for Linguists An Introduction to Python 17/11/2011.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Python
Invitation to Computer Science, Java Version, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 1 From SDU:
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 2 From SDU:
Input, Output, and Processing
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Python From the book “Think Python”
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Variables, Expressions, and Statements
Variables, Expressions and Statements
Review, Pseudocode, Flow Charting, and Storyboarding.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables, Types, Expressions Intro2CS – week 1b 1.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Programming In Python. Starter Using the internet… Find what a programming language is.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Topics Designing a Program Input, Processing, and Output
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Introduction to C++ Programming
Lecture 2 Python Programming & Data Types
T. Jumana Abu Shmais – AOU - Riyadh
Topics Designing a Program Input, Processing, and Output
Lecture 2 Python Programming & Data Types
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
12th Computer Science – Unit 5
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Programming for Linguists An Introduction to Python

Contact Claudia Peersman Lange Winkelstraat 40, room L202 (2 nd floor)

Literature “Think Python. How to Think Like a Computer Scientist?” by Allen B. Downey freely available at: “Natural Language Processing with Python. Analyzing Text with the Natural Language Toolkit” by Steven Bird, Ewan Klein, and Edward Loper freely available at:

The Python programming language Part 1 Formal vs. natural languages The way of the program Programming for linguists? What is a program? Debugging Your first program

Formal vs. natural languages Natural Languages: spoken languages, e.g. English, Dutch, French… not designed by people evolved naturally Formal Languages: designed by people for specific applications, e.g.: in mathematics: notation which denotes relationships among numbers and symbols in chemistry: represent the chemical structure of molecules

Many features in common: tokens, structure, syntax and semantics A lot of differences: Natural LanguagesFormal Languages AmbiguityNearly unambiguous RedundancyCompact Idioms and metaphors Literal: they mean exactly what they say

Some Examples = 10 H2O = 1$0 ??? Zz ??? Illegal tokens $ and Zz 5 +: 5 = 10 ??? Legal tokens, but illegal structure +:

The way of the program Programming = the art of problem solving: formulate problems think creatively about possible solutions express a solution clearly and accurately trial and error

Programming for linguists? aim: handle large linguistic corpora automatic frequency counts distribution of linguistic features across different categories, corpora look up context existing tools are limited, cost money

About Python… open source executed by an interpreter in two ways: interactive mode script mode

interactive mode: open the interpreter >>> prompt = ready to begin type a command interpreter prints the results >>>

script mode: open a new window in the interpreter type a number of commands save the program as a python script: e.g. test.py the program is executed whenever you tell the interpreter to run it the results are printed when the script is run

Which mode to use? interactive mode: good for testing small parts of the program before you go on does not save the program! script mode: put together all small parts of code in a sequence of instructions for the computer to execute save your program use it again in the future

What is a program? a sequence of instructions that specifies how to perform a computation for linguists: the computation can also be e.g. looking up the context of words in a text, calculating average word lengths, sentence lengths, …

Some basic instructions input: data you type, a text you load output: display data on the screen, send data to a file math: perform basic mathematical operations like +, -, X, :

conditional execution: check for certain conditions and execute the appropriate instructions repetition: perform some action repeatedly, usually with some variation Programming = breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions

Debugging

Bugs = programming errors Debugging = process of tracking down programming errors Three kinds of bugs: syntax errors runtime errors semantic errors

Syntax errors refer to the structure of the program and the rules about that structure if there is even a single syntax error in your code: Python will display an error message the execution of your program will quit immediately

An example parentheses: (1 + 2) : correct syntax 2) : syntax error Syntax errors are very common in the beginning. The more you practice and gain experience, the fewer mistakes you will make and the faster you will find them.

Runtime errors also called exceptions do not appear until after the program has started to run Python will display an error message For example: you give the instruction to open a file, but you have typed in the wrong file name or wrong directory

Semantic errors The program will run perfectly, but it will not produce the results you wanted: the meaning of the program (semantics) is wrong Tricky errors, because: Python will not display an error message !! you need to work backward looking at the output of the program and try to figure out what it is doing exactly

An example Python function read( ) vs. readline( ) vs. readlines( )

Debugging is equally important to programming itself: not only learn how to write a program learn to write a program that works learn to write a program that does what you want it to do Always try out small pieces of code before you go on with writing your program Try out your code on short pieces of text, so that you can verify your results manually

Your first program open IDLE The first program is usually called “Hello, world!” In Python: >>> print “Hello, world!”or >>> print ‘Hello, world!’ Mind the quotation marks!

This is the print statement The quotation marks mark the beginning and the end of the text to be displayed The quotation marks do not appear in the result

Why we teach Python: e.g. in Java: public class Hello { public static void main( String[] args ) { System.out.println( "Hello, World!" ); } }

Make some mistakes What happens if you: leave out one of the quotation marks replace “ by ‘ or vice versa in one case spell “print” wrong double the quotation marks double the quotation marks, but change the order

By making mistakes on purpose you will: learn which details are important in writing program code learn to debug more efficiently, because you get to know what the error messages mean

Try it yourselves We will make time to try out new things as we proceed Programming is a new way of thinking for linguists If there is a problem or you have a question, do not hesitate to mention it immediately

Values and Types values = basic elements of a program e.g. print “Hello, world!” each value has a type: integer string float

Integer: all non-decimal numbers e.g. 105 String: a string of letters e.g. “Hello, World!” Float: numbers with a decimal point e.g The interpreter can tell you the type of a value: >>> type(105)

Try to find out what the type is of the following values: “Hello!” Dag Jan “123” “ ”

Try this: >>> print 123,456 Float types always have a dot, never a comma To which kind of error could this lead? runtime error syntax error semantic error

Variables A name that refers to a value An assignment statement creates new variables and assigns values to them You can choose the name yourself e.g. >>> text = “Everything except ‘Hello, world!’” >>> age = 26 >>> pi =

The variables now carry the values we assigned to them: >>> print text >>> print age >>> print pi The interpreter can again tell you the type: >>> type(text)

Variable names: can be arbitrarily long can contain both letters and numbers have to begin with a letter can contain uppercase letters are case sensitive ! If you use an illegal character in your name, you will get a syntax error message: e.g. my name,

You cannot choose a name that is a keyword in Python: and del from as elif global assert else if break except import class exec in continue finally is def for lambda not while or with pass yield print raise return try Tip: try to choose names which describe what the variable is used for

Statements Units of code that the Python interpreter can execute So far we have seen the print statement and the assignment statement A program usually contains a series of statements that are executed in an order predetermined by the programmer

e.g. >>> age1 = 20 >>> age2 = 40 >>> print age2 40 >>> average_age = (age1 + age2)/2 >>> print average_age 30

You always have to assign a value to a variable before you can work with it Variables have to be spelled in the same way throughout the program If you assign a new value to an existing variable, the old value is deleted e.g. >>>age = 20 >>>age = age + 20 >>>print age

Operators and Operands Operators = special symbols that represent computations e.g. +, -, *, /, ** Operands = the values the operator is applied to e.g Try 2/3

When both operands are integers, the result is again an integer If you want a floating-point result, you have to make one of the operands a floating-point number: >>> 2/ you can also give a command at the beginning of your script: from __future__ import division

Expressions A combination of values, variables, and operators Try: >>>x = 5 >>>x + 1 Now make a script of it (File  New window) and run it (Run  Run module)

In a script an expression all by itself does not print a result !!! How can you modify the script so that it does produce a result ?

Order of Operations The order of evaluation depends on the rules of precedence For mathematical operators, Python follows mathematical conventions: Parentheses Exponentiation Multiplication and division Addition and subtraction

String Operations In general: no mathematical operations on strings e.g. “hello”/ “hi”  TypeError: unsupported operand type(s) for /: 'str' and 'str’ Except: the + and * operators

Try: “hello” + “hi” “hello”*2 String + string = concatenation string * int = repetition

An expression that is either True or False e.g. the operator == >>>5 == 5 True >>>5 == 6 False True & False: not string Boolean Expressions

x == yx is equal to y x != y x is not equal to y x > y x is greater than y x < y x is smaller than y x >= y x is greater than or equal to y x <= y x is smaller than or equal to y Relational Operators

Remember that: = is an assignment operator  used to assign a value to a variable == is a relational operator  used to express equality order is again important =, =!  do not work!

and or not Return a boolean expression: True or False Logical Operators

x = 5 Which would return True? x > 0 and x < 5 x == 3 or x == 4 not(x > 5)

Conditional statements check conditions and change the behaviour of the program accordingly if statement: e.g. >>>if x > 0 : print “x is positive” #body Conditional Execution

Only if the condition is True, the print statement will be executed There is no limit on the number of statements that can appear in the body There has to be at least one statement in the body You can use pass as a temporary substitute for code you have not written yet: if x > 0: pass

There are more than 2 possibilities if, elif (else if) & else e.g. if x > y : print “x is greater than y” elif x < y : print “x is smaller than y” else : print “x is equal to y” Chained Conditionals

There is no limit on the number of elif statements Every elif statement has to contain at least one statement The else statement has to come at the end, but is not necessary Each condition is checked in order

What would the result be if x = 8? if x == 0: print “x is 0” elif x > 0: print “x is greater than 0” elif x > 0 and x <10: print “x is between 0 and 10” else: print “x is smaller than 0”

If one of the conditions is True, the corresponding branch executes and the statement ends Even if more than one condition is True, only the first True branch executes !

Some Comments As programs grow and become more complicated, they get more difficult to read You can add notes which explain (for yourself and for others who read your code) what the program is doing: start a piece of code with “#” and add your comment everything from the # to the end of the line is ignored by the program

Put 2 numbers in different variables Print the results for the operands +, -, *, /, ** when they are applied to these 2 variables (with floating-point numbers as a result for division) Exercises

x = 2 y = 3 print "x =", x print "y =", y print "x + y =", x + y print "x - y =", x - y print "x * y =", x * y print "x / y =", x / float(y) print "x**y =", x**y

For Next Week… No class Exercises on Blackboard Deadline: 27/11

Python install (please use these links): for windows: thon msi thon msi for mac: thon macosx dmg thon macosx dmg

Please mail me the subject of your dissertation Thank you