DOC PREVIEW
UMass Amherst CMPSCI 591N - Introduction to Python

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhIntroduction to PythonLecture #3Computational LinguisticsCMPSCI 591N, Spring 2006University of Massachusetts AmherstAndrew McCallumAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhToday’s Main Points• Check in on HW#1. Demo.• Intro to Python computer programming language.• Some examples Linguistic applications.• The NLTK toolkit.• Pointers to more Python resources.Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhPython Outline• Introduction– Python attributes and ‘Why Python?’– Running programs– Modules• Basic object types– Numbers and variables– Strings– Lists, Tuples– Dictionaries• Control Flow– Conditionals– LoopsAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhPython Features• Free. Runs on many different machines.• Easy to read.– Perl = “write only language”• Quick to throw something together.– NaiveBayes Java vs Python• Powerful. Object-oriented.• THE modern choice for CompLing.• NLTKAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhUsing Python Interactively$ python>>> print “Hello everyone!”Hello everyone!>>> print 2+24>>> myname = “Andrew”>>> myname‘Andrew’The easiest way to give Python a whirl is interactively.(Human typing in red. Machine responses in black.)Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhModulesprint 25*3 # multiply by 3print ‘CompLing ‘ + ‘lecture 3’ # concatenate with +myname = ‘Andrew’To save code you need to write it in files.Module: a text file containing Python code.Example: write the following to file foo.py$ python foo.py75CompLing lecture 3$(No leading spaces!)Then run it as follows:Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhImporting Modules$ python>>> import foo75CompLing lecture 3>>> foo.myname‘Andrew’Every file ending in .py is a Python module.Modules can contain attributes such as functions.We can import this module into Python.Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhModule Reloading>>> import foo75CompLing lecture 3Importing is expensive--after the first import of a module, repeatedimports have no effect (even if you have edited it).Use re load to force Python to rerun the file again.Edit foo.py to print 25*4 (instead of 25*3) and reload>>> reload(foo)75CompLing lecture 3<module ‘foo’ from ‘foo.py’>Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhModule Attributesuniversity = ‘UMass’department = ‘Linguistics’Consider file bar.py>>> import bar>>> print bar.departmentLinguistics>>> from bar import department>>> print departmentLinguistics>>> from bar import *>>> print universityUMassfrom copies named attributes from a module, so they are variables in the recipient.Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhPython Program Structure• Programs are composed of modules• Modules contain statements• Statements contain expressions• Expressions create and process objects• Statements include– variable assignment, function calls– control flow, module access– building functions, building objects– printingAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhPython’s built-in objects• Numbers: integer, floating point• Strings• Lists• Dictionaries• Tuples• FilesAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhNumbers and Variables• Usual number operators, e.g: +, *, /, **• Usual operator precedence:A * B + C * D = (A * B) + (C * D)(use parens for clarity and to reduce bugs)• Useful modules: math, random• Variables– created when first assigned a value– replaced with their values when used in expressions– must be assigned before use– no need to declare ahead of timeAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhStrings• String handling in Python is easy andpowerful (unlike C, C++, Java)• Strings may be written using single quotes:‘This is a Python string’• or double quotes“and so is this”• They are the same, it just makes it easy toinclude single (or double) quotes:‘He said “what?”’ or “He’s here.”(Learning Python, chapter 5)Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhBackslash in stringsBackslash \ can be used to escape (protect) certain non-printing orspecial characters.For example, \n is newline, \t is tab.>>> s = ‘Name\tAge\nJohn\t21\nBob\t44’>>> print sName AgeJohn 21Bob 44>>> t = ‘”Mary\’s”’>>> print t“Mary’s”Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhTriple quoteUse a triple quote (“”” or ‘’’) for a string over severa lines:>>> s = “””this is... a string... over 3 lines”””>>> t = ‘’’so... is... this’’’>>> print sthis isa stringover 3 lines>>> print tsoisthisAndrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhString operationsConcatenation (+)Length (len)Repetition (*)Indexing and slicing ([])s = ‘computational’t = ‘linguistics’cl = s + ‘ ‘ + t # ‘computational linguistics’l = len(cl) # 25u = ‘-’ * 6 # ------c = s[3] # px = cl[11:16] # ‘al li’y = cl[20:] # ‘stics’z = cl[:-1] # ‘computational linguistic’Andrew McCallum, UMass Amherst, including material from Eqan Klein and Steve Renals, at Univ EdinburghhString methodsMethods are functions applied to and associated with objectsString methods allow strings to be processed in a more sophisticated ways = ‘example’s = s.capitalize() # ‘Example’t = s.lower() # ‘example’flag = s.isalpha() # Trues = s.replace(‘amp’,‘M’) # ‘exMle’i = t.find(‘xa’) # 1n = t.count(‘e’) # 2Andrew McCallum, UMass Amherst, including material from Eqan Klein


View Full Document

UMass Amherst CMPSCI 591N - Introduction to Python

Download Introduction to Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?