Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Tutorial Adapted from slides by Prof. Mitch Marcus.

Similar presentations


Presentation on theme: "Python Tutorial Adapted from slides by Prof. Mitch Marcus."— Presentation transcript:

1 Python Tutorial Adapted from slides by Prof. Mitch Marcus

2 Python  Python is an open source scripting language.  Developed by Guido van Rossum in the early 1990s  Named after Monty Python  Available on eniac  Available for download from http://www.python.orghttp://www.python.org

3 Why Python?  Object Oriented  With functional constructs: map, generators, list comprehension  NLP Processing: Symbolic  Python has built-in datatypes for strings, lists, and more  NLP Processing: Statistical  Python has strong numeric processing capabilities: matrix operations, etc.  Suitable for probability and machine learning code.  NLTK: Natural Language Tool Kit  Widely used for teaching NLP  First developed for this course  Implemented as a set of Python modules  Provides adequate libraries for many NLP building blocks  Google "NLTK" for more info, code, data sets, book..

4 Why Python?  Powerful but unobtrusive object system  Every value is an object  Classes guide but do not dominate object construction  Powerful collection and iteration abstractions  Dynamic typing makes generics easy

5 Python  Interpreted language: works with an evaluator for language expressions  Dynamically typed: variables do not have a predefined type  Rich, built-in collection types:  Lists  Tuples  Dictionaries (maps)  Sets  Concise

6 Language features  Indentation instead of braces  Newline separates statements  Several sequence types  Strings '…' : made of characters, immutable  Lists […] : made of anything, mutable  Tuples (…) : made of anything, immutable  Powerful subscripting (slicing)  Functions are independent entities (not all functions are methods)  Exceptions

7 Dynamic typing  Java/C++: statically typed  Variables are declared to refer to objects of a given type  Methods use type signatures to enforce contracts  Python  Variables come into existence when first assigned to  A variable can refer to an object of any type  All types are (almost) treated the same way  Main drawback: type errors are only caught at runtime

8 Playing with Python (1) >>> 2+3 5 >>> 2/3 0 >>> 2.0/3 0.66666666666666663 >>> x=4.5 >>> int(x) 4

9 Playing with Python (2) >>> x='abc' >>> x[0] 'a' >>> x[1:3] 'bc' >>> x[:2] 'ab' >>> x[1]='d' Traceback (most recent call last): File " ", line 1, in x[1]='d' TypeError: 'str' object does not support item assignment

10 Playing with Python (3) >>> x=['a','b','c'] >>> x[1] 'b' >>> x[1:] ['b', 'c'] >>> x[1]='d' >>> x ['a', 'd', 'c']

11 Playing with Python (4) >>> def p(x): if len(x) < 2: return True else: return x[0] == x[-1] and p(x[1:-1]) >>> p('abc') False >>> p('aba') True >>> p([1,2,3]) False >>> p([1,'a','a',1]) True >>> p((False,2,2,False)) True >>> p(('a',1,1)) False

12 Python dictionaries (Maps) >>> d={'alice':1234, 'bob':5678, 'clare':9012} >>> d['alice'] 1234 >>> d['bob'] 5678 >>> d['bob'] = 7777 >>> d {'clare': 9012, 'bob': 7777, 'alice': 1234} >>> d.keys() ['clare', 'bob', 'alice'] >>> d.items() [('clare', 9012), ('bob', 7777), ('alice', 1234)] >>> del d['bob'] >>> d {'clare': 9012, 'alice': 1234}

13 Installing & Running Python

14 The Python Interpreter  Interactive interface to Python % python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>  Python interpreter evaluates inputs: >>> 3*(7+2) 27

15 IDLE Development Environment  Shell for interactive evaluation.  Text editor with color-coding and smart indenting for creating Python files.  Menu commands for changing system settings and running files.

16 Running Interactively on Linux On Unix… % python >>> 3+3 6  Python prompts with '>>>'.  To exit Python (not Idle):  In Unix, type CONTROL-D  In Windows, type CONTROL-Z +

17 Running Programs on UNIX % python filename.py You can make a python file executable by adding following text as the first line of the file to make it runable: #!/usr/bin/python

18 The Basics

19 A Code Sample x = 34 - 23 # A comment. y = "Hello" # Another one. z = 3.45 if z == 3.45 or y == "Hello": x = x + 1 y = y + " World" # String concat. print x print y

20 Enough to Understand the Code  Indentation matters to the meaning of the code:  Block structure indicated by indentation  The first assignment to a variable creates it.  Variable types don't need to be declared.  Python figures out the variable types on its own.  Assignment uses = and comparison uses ==.  For numbers + - * / % are as expected.  Special use of + for string concatenation.  Special use of % for string formatting (as with printf in C)  Logical operators are words ( and, or, not ) not symbols  Simple printing can be done with print.

21 Basic Datatypes  Integers (default for numbers) z = 5 / 2 # Answer is 2, integer division.  Floats x = 3.456  Strings  Can use "" or '' to specify. "abc" 'abc' (Same thing.)  Unmatched can occur within the string. "matt's"  Use triple double-quotes for multi-line strings or strings than contain both ' and " inside of them: """a'b"c"""

22 Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines.  Use a newline to end a line of code.  Use \ when must go to next line prematurely.  No braces { } to mark blocks of code in Python… Use consistent indentation instead.  The first line with less indentation is outside of the block.  The first line with more indentation starts a nested block  Often a colon appears at the start of a new block. (E.g. for function and class definitions.)

23 Comments  Start comments with # – the rest of line is ignored.  Can include a "documentation string" as the first line of any new function or class that you define.  The development environment, debugger, and other tools use it: it's good style to include one def my_function(x, y): """This is the docstring. This function does blah blah blah.""" # The code would go here...

24 Assignment  Binding a variable in Python means setting a name to hold a reference to some object.  Assignment creates references, not copies  Names in Python do not have an intrinsic type. Objects have types.  Python determines the type of the reference automatically based on what data is assigned to it.  You create a name the first time it appears on the left side of an assignment expression: x = 3  A reference is deleted via garbage collection after any names bound to it have passed out of scope.  Python uses reference semantics

25 Naming Rules  Names are case sensitive and cannot start with a number. They can contain letters, numbers, and underscores. bob Bob _bob _2_bob_ bob_2 BoB  There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while

26 Accessing Non-Existent Name  Trying to access a name before it's been properly created (by placing it on the left side of an assignment), gives an error: >>> y Traceback (most recent call last): File " ", line 1, in -toplevel- y NameError: name 'y' is not defined >>> y = 3 >>> y 3

27 Sequence types Tuples, Lists and Strings

28 Sequence Types 1. Tuple  A simple immutable ordered sequence of items  Items can be of mixed types, including collection types 2. Strings  Immutable  Conceptually very much like a tuple  (8-bit characters. Unicode strings use 2-byte characters.) 3. List  Mutable ordered sequence of items of mixed types

29 Similar Syntax  All three sequence types (tuples, strings, and lists) share much of the same syntax and functionality.  Key difference:  Tuples and strings are immutable  Lists are mutable  The operations shown in this section can be applied to all sequence types  most examples will just show the operation performed on one

30 Sequence Types  Tuples are defined using parentheses (and commas). >>> tu = (23, 'abc', 4.56, (2,3), 'def')  Lists are defined using square brackets (and commas). >>> li = ["abc", 34, 4.34, 23]  Strings are defined using quotes (", ', or """). >>> st = "Hello World" >>> st = 'Hello World' >>> st = """This is a multi-line string that uses triple quotes."""

31 Sequence Types  We can access individual members of a tuple, list, or string using square bracket "array" notation.  Note that all are 0 based… >>> tu = (23, 'abc', 4.56, (2,3), 'def') >>> tu[1] # Second item in the tuple. 'abc' >>> li = ["abc", 34, 4.34, 23] >>> li[1] # Second item in the list. 34 >>> st = "Hello World" >>> st[1] # Second character in string. 'e'

32 Positive and negative indices >>> t = (23, 'abc', 4.56, (2,3), 'def') Positive index: count from the left, starting with 0. >>> t[1] 'abc' Negative lookup: count from right, starting with –1. >>> t[-3] 4.56

33 Slicing: Return Copy of a Subset >>> t = (23, 'abc', 4.56, (2,3), 'def') Return a copy of the container with a subset of the original members. Start copying at the first index, and stop copying before the second index. >>> t[1:4] ('abc', 4.56, (2,3)) You can also use negative indices when slicing. >>> t[1:-1] ('abc', 4.56, (2,3))

34 Slicing: Return Copy of a Subset >>> t = (23, 'abc', 4.56, (2,3), 'def') Omit the first index to make a copy starting from the beginning of the container. >>> t[:2] (23, 'abc') Omit the second index to make a copy starting at the first index and going to the end of the container. >>> t[2:] (4.56, (2,3), 'def')

35 The 'in' Operator  Boolean test whether a value is inside a collection (often called a container in Python: >>> t = [1, 2, 4, 5] >>> 3 in t False >>> 4 in t True >>> 4 not in t False  For strings, tests for substrings >>> a = 'abcde' >>> 'c' in a True >>> 'cd' in a True >>> 'ac' in a False  Be careful: the in keyword is also used in the syntax of for loops and list comprehensions.

36 The + Operator  The + operator produces a new tuple, list, or string whose value is the concatenation of its arguments. >>> (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) >>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] >>> "Hello" + " " + "World" 'Hello World'

37 Mutability: Tuples vs. Lists

38 Lists: Mutable >>> li = ['abc', 23, 4.34, 23] >>> li[1] = 45 >>> li ['abc', 45, 4.34, 23]  We can change lists in place.  Name li still points to the same memory reference when we're done.

39 Tuples: Immutable >>> t = (23, 'abc', 4.56, (2,3), 'def') >>> t[2] = 3.14 Traceback (most recent call last): File " ", line 1, in -toplevel- tu[2] = 3.14 TypeError: object doesn't support item assignment You can't change a tuple. You can make a fresh tuple and assign its reference to a previously used name. >>> t = (23, 'abc', 3.14, (2,3), 'def')  The immutability of tuples means they're faster than lists.

40 Operations on Lists Only 1 >>> li = [1, 11, 3, 4, 5] >>> li.append('a')# Note the method syntax >>> li [1, 11, 3, 4, 5, 'a'] >>> li.insert(2, 'i') >>>li [1, 11, 'i', 3, 4, 5, 'a']

41 The extend method vs the + operator  + creates a fresh list (with a new memory reference)  extend operates on list li in place. >>> li.extend([9, 8, 7]) >>>li [1, 2, 'i', 3, 4, 5, 'a', 9, 8, 7] Confusing:  extend takes a list as an argument.  append takes a singleton as an argument. >>> li.append([10, 11, 12]) >>> li [1, 2, 'i', 3, 4, 5, 'a', 9, 8, 7, [10, 11, 12]]

42 Operations on Lists Only 3 >>> li = ['a', 'b', 'c', 'b'] >>> li.index('b') # index of first occurrence * 1 * more complex forms exist >>> li.count('b') # number of occurrences 2 >>> li.remove('b') # remove first occurrence >>> li ['a', 'c', 'b']

43 Operations on Lists Only 4 >>> li = [5, 2, 6, 8] >>> li.reverse() # reverse the list *in place* >>> li [8, 6, 2, 5] >>> li.sort() # sort the list *in place* >>> li [2, 5, 6, 8] >>> li.sort(some_function) # sort in place using user-defined comparison

44 Summary: Tuples vs. Lists  Lists slower but more powerful than tuples.  Lists can be modified, and they have lots of handy operations we can perform on them.  Tuples are immutable and have fewer features.  To convert between tuples and lists use the list() and tuple() functions: li = list(tu) tu = tuple(li)

45 Dictionaries: a mapping collection type

46 Dictionaries: A Mapping type  Dictionaries store a mapping between a set of keys and a set of values.  Keys can be any immutable type.  Values can be any type  Values and keys can be of different types in a single dictionary  You can  define  modify  view  lookup  delete the key-value pairs in the dictionary.

47 Creating and accessing dictionaries >>> d = {'user':'bozo', 'pswd':1234} >>> d['user'] 'bozo' >>> d['pswd'] 1234 >>> d['bozo'] Traceback (innermost last): File ' ' line 1, in ? KeyError: bozo

48 Updating Dictionaries >>> d = {'user':'bozo', 'pswd':1234} >>> d['user'] = 'clown' >>> d {'user':'clown', 'pswd':1234}  Keys must be unique.  Assigning to an existing key replaces its value. >>> d['id'] = 45 >>> d {'user':'clown', 'id':45, 'pswd':1234}  Dictionaries are unordered  New entry might appear anywhere in the output.  (Dictionaries work by hashing)

49 Removing dictionary entries >>> d = {'user':'bozo', 'p':1234, 'i':34} >>> del d['user'] # Remove one. >>> d {'p':1234, 'i':34} >>> d.clear() # Remove all. >>> d {} >>> a=[1,2] >>> del a[1]# (del also works on lists) >>> a [1]

50 Useful Accessor Methods >>> d = {'user':'bozo', 'p':1234, 'i':34} >>> d.keys() # List of current keys ['user', 'p', 'i'] >>> d.values() # List of current values ['bozo', 1234, 34] >>> d.items() # List of item tuples. [('user','bozo'), ('p',1234), ('i',34)]

51 Functions in Python (Methods later)

52 Python and Types Python determines the data types of variable bindings in a program automatically."Dynamic Typing" But objects have types and Python enforces the types of objects. "Strong Typing" So, for example, you can't just append an integer to a string. You must first convert the integer to a string itself. x = "the answer is " # Decides x is bound to a string. y = 23 # Decides y is bound to an integer. print x + y # Python will complain about this.

53 Calling a Function  The syntax for a function call is: >>> def myfun(x, y): return x * y >>> myfun(3, 4) 12  Parameters are passed by “Call by Value”  Each argument expression is evaluated and the resulting value is bound to the corresponding variable in the function (which is a new one for each invocation)  All assignment in Python, including binding function parameters, use reference semantics, since values are all object references.  Many web discussions on this are simply confused and call it “call by assignment” or “call by sharing”.

54 Functions without returns  All functions in Python have a return value  even if no return line inside the code.  Functions without a return return the special value None.  None is a special constant in the language.  None is used like NULL, void, or nil in other languages.  None is also logically equivalent to False.  The interpreter doesn't print None

55 Function overloading? No.  There is no function overloading in Python.  Unlike C++, a Python function is specified by its name alone  The number, order, names, or types of its arguments cannot be used to distinguish between two functions with the same name.  Two different functions can't have the same name, even if they have different arguments.  But: see operator overloading in later slides

56 Functions are first-class objects in Python  Functions can be used as any other data type  They can be  Arguments to function  Return values of functions  Assigned to variables  Parts of tuples, lists, etc  … >>> def myfun(x): return x*3 >>> def applier(q, x): return q(x) >>> applier(myfun, 7) 21

57 Logical Expressions

58 True and False  True and False are constants in Python.  Other values equivalent to True and False :  False : zero, None, empty container or object  True : non-zero numbers, non-empty objects  Comparison operators: ==, !=, <, <=, etc.  X and Y have same value: X == Y  Compare with X is Y :  X and Y are two variables that refer to the identical same object.

59 Boolean Logic Expressions  You can also combine Boolean expressions.  True if a is True and b is True : a and b  True if a is True or b is True : a or b  True if a is False : not a  Use parentheses as needed to disambiguate complex Boolean expressions.  Actually, evaluation of expressions is lazy…

60 Special Properties of and and or  Actually and and or don't return True or False.  They return the value of one of their sub-expressions (which may be a non-Boolean value).  X and Y and Z  If all are true, returns value of Z.  Otherwise, returns value of first false sub-expression.  X or Y or Z  If all are false, returns value of Z.  Otherwise, returns value of first true sub-expression.  and and or use lazy evaluation, so no further expressions are evaluated

61 Conditional Expressions x = true_value if condition else false_value  Evaluation rule:  First, condition is evaluated  If True, true_value is evaluated and returned  If False, false_value is evaluated and returned

62 Control of Flow

63 if Statements if x == 3: print "X equals 3." elif x == 2: print "X equals 2." else: print "X equals something else." print "This is outside the 'if'." Be careful! The keyword if is also used in the syntax of filtered list comprehensions. Note:  Use of indentation for blocks  Colon (:) after boolean expression

64 while Loops >>> x = 3 >>> while x < 5: print x, "still in the loop" x = x + 1 3 still in the loop 4 still in the loop >>> x = 6 >>> while x < 5: print x, "still in the loop" >>>

65 break and continue  You can use the keyword break inside a loop to leave the while loop entirely.  You can use the keyword continue inside a loop to stop processing the current iteration of the loop and to immediately go on to the next one.

66 assert  An assert statement will check to make sure that something is true during the course of a program.  If the condition if false, the program stops (more accurately: the program throws an exception) assert(number_of_players < 5)

67 For Loops

68 For Loops / List Comprehensions  Python's list comprehensions provide a natural idiom that usually requires a for-loop in other programming languages.  As a result, Python code uses many fewer for-loops  Nevertheless, it's important to learn about for-loops.  Caveat! The keywords for and in are also used in the syntax of list comprehensions, but this is a totally different construction.

69 For Loops 1  For-each is Python's only for construction  A for loop steps through each of the items in a collection type, or any other type of object which is "iterable" for in :  If is a list or a tuple, then the loop steps through each element of the sequence.  If is a string, then the loop steps through each character of the string. for someChar in "Hello World": print someChar

70 For Loops 2 for in :  can be more complex than a single variable name.  When the elements of are themselves sequences, then can match the structure of the elements.  This multiple assignment can make it easier to access the individual parts of each element. for (x, y) in [(a,1), (b,2), (c,3), (d,4)]: print x

71 For loops and the range() function  Since a variable often ranges over some sequence of numbers, the range() function returns a list of numbers from 0 up to but not including the number we pass to it.  range(5) returns [0,1,2,3,4]  So we could say: for x in range(5): print x  (There are more complex forms of range() that provide richer functionality…)  xrange() returns an iterator that provides the same functionality here more efficiently

72 For Loops and Dictionaries >>> ages = { "Sam " :4, "Mary " :3, "Bill " :2 } >>> ages {'Bill': 2, 'Mary': 3, 'Sam': 4} >>> for name in ages.keys(): print name, ages[name] Bill 2 Mary 3 Sam 4 >>>

73 String Operations  A number of methods for the string class perform useful formatting operations: >>> "hello".upper() 'HELLO'  Check the Python documentation for many other handy string operations.  Helpful hint: use.strip() to strip off final newlines from lines read from files

74 Printing with Python  You can print a string to the screen using "print."  Using the % string operator in combination with the print command, we can format our output text. >>> print "%s xyz %d" % ("abc", 34) abc xyz 34 "Print" automatically adds a newline to the end of the string. If you include a list of strings, it will concatenate them with a space between them. >>> print "abc">>> print "abc", "def" abcabc def  Useful trick: >>> print "abc", doesn't add newline just a single space

75 Convert Anything to a String  The built-in str() function can convert an instance of any data type into a string.  You can define how this function behaves for user-created data types. You can also redefine the behavior of this function for many types. >>> "Hello " + str(2) "Hello 2"

76 Importing and Modules

77  Use classes & functions defined in another file.  A Python module is a single file with the same name (plus the.py extension)  Like Java import Where does Python look for module files?  The list of directories where Python looks: sys.path  To add a directory of your own to this list, append it to this list. sys.path.append('/my/new/path')

78 Import I import somefile  Everything in somefile.py can be referred to by: somefile.className.method("abc") somefile.myFunction(34)  from somefile import *  Everything in somefile.py can be referred to by: className.method("abc") myFunction(34)  Caveat! This can easily overwrite the definition of an existing function or variable!

79 Import II from somefile import className  Only the item className in somefile.py gets imported.  Refer to it without a module prefix.  Caveat! This can overwrite an existing definition. className.method("abc")  This was imported myFunction(34)  Not this one

80 Commonly Used Modules  Some useful modules to import, included with Python:  Module: sys- Lots of handy stuff.  Maxint  Module: os- OS specific code.  Module: os.path- Directory processing.

81 More Commonly Used Modules  Module: math- Mathematical code.  Exponents  sqrt  Module: Random- Random number code.  Randrange  Uniform  Choice  Shuffle  To see what's in the standard library of modules, check out the Python Library Reference:  http://docs.python.org/lib/lib.html http://docs.python.org/lib/lib.html  Or O'Reilly's Python in a Nutshell:  http://proquest.safaribooksonline.com/0596100469 http://proquest.safaribooksonline.com/0596100469  (URL works inside of UPenn, afaik, otherwise see the course web page)

82 Object Oriented Programming in Python: Defining Classes

83 It's all objects…  Everything in Python is really an object.  We've seen hints of this already… "hello".upper() list3.append('a') dict2.keys()  These look like Java or C++ method calls.  Programming in Python is typically done in an object oriented fashion.

84 Defining a Class  A class is a special data type which defines how to build a certain kind of object.  The class also stores some data items that are shared by all the instances of this class.  But no static variables!  Python doesn't use separate class interface definitions much. You just define the class and then use it.

85 Methods in Classes  Define a method in a class by including function definitions within the scope of the class block.  There must be a special first argument self in all of method definitions which gets bound to the calling instance.  Self is like this in Java  Self always refers to the current class instance  A constructor for a class is a method called __init__ defined within the class.

86 A simple class definition: student class student: """A class representing a student.""" def __init__(self,n,a): self.full_name = n self.age = a def get_age(self): return self.age

87 Creating and Deleting Instances

88 Instantiating Objects  There is no "new" keyword as in Java.  Merely use the class name with () notation and assign the result to a variable.  __init__ serves as a constructor for the class.  Example: b = student("Bob", 21)  An __init__ method can take any number of arguments.  Like other functions & methods, arguments can be defined with default values, making them optional to the caller.  But no real overloading

89 Self  Although you must specify self explicitly when defining the method, you don't include it when calling the method.  Python passes it for you automatically. Defining a method:Calling a method: (this code inside a class definition.) def set_age(self, num):>>> x.set_age(23) self.age = num

90 Access to Attributes and Methods

91 Definition of student class student: """A class representing a student.""" def __init__(self,n,a): self.full_name = n self.age = a def get_age(self): return self.age

92 Traditional Syntax for Access >>> f = student ("Bob Smith", 23) >>> f.full_name # Access an attribute. "Bob Smith" >>> f.get_age() # Access a method. 23 No public, private, protected, etc…

93 File Processing, Error Handling, Regular Expressions, etc.

94 File Processing with Python This is a good way to play with the error handing capabilities of Python. Try accessing files without permissions or with non-existent names, etc. You'll get plenty of errors to look at and play with! fileptr = open('filename') somestring = fileptr.read() for line in fileptr: print line fileptr.close() For more, see section 3.9 of the Python Library reference at http://docs.python.org/lib/bltin-file-objects.html http://docs.python.org/lib/bltin-file-objects.html

95 Exception Handling  Exceptions are Python classes  More specific kinds of errors are subclasses of the general Error class.  You use the following commands to interact with them:  Try  Except  Finally  Catch

96 Regular Expressions and Match Objects  Python provides a very rich set of tools for pattern matching against strings in module re (for regular expression)  As central as they are to much of the use of Python, we won't be using them in this course…  For a gentle introduction to regular expressions in Python see http://www.diveintopython.org/regular_expressions/index.html or http://www.amk.ca/python/howto/regex/regex.html


Download ppt "Python Tutorial Adapted from slides by Prof. Mitch Marcus."

Similar presentations


Ads by Google