Introduction to Python Introduction to Python 1 Materials based on - - PowerPoint PPT Presentation

introduction to python introduction to python
SMART_READER_LITE
LIVE PREVIEW

Introduction to Python Introduction to Python 1 Materials based on - - PowerPoint PPT Presentation

Introduction to Python Introduction to Python 1 Materials based on contents from the course Programming with Python by Chad Haynes Outline Outline Overview Built-in objects Functions and scopes Object-oriented programming


slide-1
SLIDE 1

1

Introduction to Python Introduction to Python

Materials based on contents from the course Programming with Python by Chad Haynes

slide-2
SLIDE 2

2

Outline Outline

  • Overview
  • Built-in objects
  • Functions and scopes
  • Object-oriented programming
  • Functional programming
  • Exercise
slide-3
SLIDE 3

3

Python At First Glance Python At First Glance

import math def showArea(shape): print "Area = %d" % shape.area() def widthOfSquare(area): return math.sqrt(area) class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height ###### Main Program ###### r = Rectangle(10, 20) showArea(r)

Function definition Class definition Import a library module Calling a function Comment Object instantiation

slide-4
SLIDE 4

4

Why use Python? Why use Python?

  • Simple, clean syntax
  • Portable
  • Flexible
  • Large standard library
  • Short development time
  • Lots of 3rd-party tools/add-ons
  • Many good implementations

– CPython, PyPy, IronPython, Jython

  • Strong support from open-source community
slide-5
SLIDE 5

5

Similarities to Java Similarities to Java

  • Everything inherits from "object"

– Also numbers, functions, classes, … – Everything is first-class

  • Vast, powerful standard library
  • Garbage collection
  • Introspection, serialization, threads, net,…
slide-6
SLIDE 6

6

Similarities to C++ Similarities to C++

  • Multi-paradigm

– OOP, procedural, generic, functional (a little)

  • Multiple inheritance
  • Operator overloading
slide-7
SLIDE 7

7

Python vs. Java/C++/C Python vs. Java/C++/C

  • Typing: strong, but dynamic

– Names have no type – Objects have types

  • No declarations
  • Sparse syntax

– No { } for blocks, just indentation – No ( ) for if/while conditions

  • Interactive interpreter
  • # for comments

if (x < 10) { x = x + tmp; y = y * x; } System.out.println(y);

Java

if x < 10: x = x + tmp y = y * x print y

Python

slide-8
SLIDE 8

8

Getting Started Getting Started

  • Python already included in most Linux

distributions

  • Windows users can download from:

– http://python.org/download – Add python to PATH to run scripts from command line

slide-9
SLIDE 9

9

using System; class Hello { static void Main() { Console.WriteLine("Hello, World"); } }

Hello, World! Hello, World!

  • C#
  • Python

print "Hello, World!"

slide-10
SLIDE 10

10

Variables Variables

>>> x = 23 >>> print x 23 >>> x = 'foo' >>> print x foo >>> del x >>> print x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined >>> name x means 23 now it means 'foo' x becomes undefined

slide-11
SLIDE 11

11

Var1 Var1_copy Var2

Variables Variables

  • Reference Model

– Variables refer to an object – More than one variable can refer to the same

  • bject
slide-12
SLIDE 12

12

Numeric Types Numeric Types

  • Integers

– Generally 32 signed bits

  • Long Integers

– Unlimited size – Format: <number>L – Example: 4294967296L

  • Float

– Platform dependant “double” precision

  • Complex

– Format: <real>+<imag>j – Example: 6+3j

slide-13
SLIDE 13

13

Strings Strings

  • A sequence of characters enclosed with quotes
  • 3 quoting styles

– 'Single Quotes' – "Double Quotes" – """Triple Quotes"""

  • Examples

>>> print 'This may contain a "' This may contain a " >>> print "A ' is allowed" A ' is allowed >>> print """Either " or ' are OK""" Either " or ' are OK

slide-14
SLIDE 14

14

>>> info = raw_input('-> ')

  • > Here is info

>>> print info Here is info

Built Built-

  • in Function:

in Function: raw_input raw_input

  • Syntax: raw_input([prompt])

– Use prompt to ask user to input a string – Example

slide-15
SLIDE 15

15

  • Arithmetic
  • + -

* // / ** % abs

  • Example

>>> 5 + 3 # Addition 8 >>> 2 ** 8 # Exponentiation 256 >>> 13 / 4 # Integer (Truncating) Division* 3 >>> float(13) / 4 # Float Division 3.25 >>> 13 % 4 # Remainder 1 >>> abs(-3.5) # Absolute Value 3.5

  • * Becomes float division in version 3.x
slide-16
SLIDE 16

16

  • Comparison
  • < <= > >= == != <>
  • Results in 1 (true) or 0 (false)
  • Example

>>> 4 > 1.5 1 >>> 'this' != 'that' 1 >>> 4+3j == 4-2j >>> '5' == 5 >>> 0 < 10 < 20 1

slide-17
SLIDE 17

17

  • Boolean
  • and or not
  • Based on Boolean Algebra
  • i1

i2 i1 and i2 1 1 1 1 1 i1 or i2 1 1 1 i1 not i1 1 1

slide-18
SLIDE 18

18

  • Boolean
  • Example

>>> 1 == 1 and 2 >= 3 >>> 1 == 1 or 2 >= 3 1 >>> not 5.3 != 2.2 # same as: not (5.3 != 2.2) >>> 2 and '23' > '11' or 0 1

slide-19
SLIDE 19

19

  • Concatenation (+)
  • Syntax: string1 + string2
  • Example:

>>> 'Rockefeller' + 'University' 'RockefellerUniversity'

  • Repetition (*)
  • Syntax: string * number
  • Example:

>>> 'dog' * 5 'dogdogdogdogdog'

slide-20
SLIDE 20

20

  • C-Style formatting (extended printf)
  • Examples:

>>> "%i %s in the basket" % (2, "eggs") '2 eggs in the basket' >>> "%f to 2 decimal places: %.2f" %(2.0/9.0, 2.0/9.0) '0.222222 to 2 decimal places: 0.22' >>> length = 5 >>> obj = "fence" >>> "Length of the %(obj)s is %(length)i" % vars() 'Length of the fence is 5'

slide-21
SLIDE 21

21

  • Syntax: type(object)
  • Used to determine the type of an object
  • Example

>>> type(2.45) <type 'float'> >>> type('x') <type 'str'> >>> type(2**34) <type 'long'> >>> type(3+2j) <type 'complex'>

slide-22
SLIDE 22

22

  • Use built-in functions to convert between types
  • str() int() float() long() complex() bool()
  • Example

>>> str(42.3) '42.3' >>> float('-1.32e-3')

  • 0.00132

>>> int('0243') 243 >>> int(2**34) Traceback (most recent call last): File "<pyshell#12>", line 1, in ? int(2**34) OverflowError: long int too large to convert to int >>> long(2**34) 17179869184L

slide-23
SLIDE 23

23

  • Lists
  • Tuples
  • Dicts
slide-24
SLIDE 24

24

  • Construction
  • Syntax: [elem1, elem2, …]
  • Heterogeneous, ordered sequence
  • Mutable
  • Example:

>>> list1 = [1, 'hello', 4+2j, 123.12] >>> list1 [1, 'hello', (4+2j), 123.12] >>> list1[0] = 'a' >>> list1 ['a', 'hello', (4+2j), 123.12]

slide-25
SLIDE 25

25

  • Concatenation (+)
  • Syntax: list1 + list2
  • Example:

>>> [1, 'a', 'b'] + [3, 4, 5] [1, 'a', 'b', 3, 4, 5]

  • Repetition (*)
  • Syntax: list * number
  • Example:

>>> [23, 'x'] * 4 [23, 'x', 23, 'x', 23, 'x', 23, 'x']

slide-26
SLIDE 26

26

  • Indexing operator: [ ]
  • Positive indices count from the left
  • Negative indices count from the right
  • 1

2 3 4 5 6

  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1

a b c d e f g sequence[0] == a sequence[-7] == a sequence[6] == g sequence[-1] == g sequence[2] == c sequence[-5] == c

slide-27
SLIDE 27

27

  • Two indices separated by a colon
  • Available for both strings and lists
  • Example

>>> sequence = [0, 1, 2, 3, 4, 5, 6, 7] >>> sequence[1:4] [1, 2, 3] >>> sequence[2:-1] [2, 3, 4, 5, 6]

  • Missing Index implies end point

>>> sequence[:2] [0, 1] >>> sequence[3:] [3, 4, 5, 6, 7]

slide-28
SLIDE 28

28

  • Immutable version of list
  • Syntax: (elem1, elem2, …)
  • Items in tuple can not be altered
  • Example:

>>> tuple1 = (1, 5, 10) >>> tuple1[2] = 2 Traceback (most recent call last): File "<pyshell#136>", line 1, in ? tuple1[2] = 2 TypeError: object doesn't support item assignment

slide-29
SLIDE 29

29

  • Syntax: len(object)
  • Return the length of object
  • Example

>>> list1 = [1, 2, 3, 4, 5] >>> len(list1) 5 >>> string1 = "length of a string" >>> len(string1) 18

slide-30
SLIDE 30

30

  • Mapping
  • Associate a key with a value
  • Each key must be unique
  • 'z'

'ab' 2.1 3 keys 10 [2] (3,8) 'hello' values

slide-31
SLIDE 31

31

  • Construction
  • Syntax: {key1: value1, key2: value2 …}
  • Unordered map
  • Example:

>>> dict1 = {'a': 1, 'b': 2} >>> dict1 {'a': 1, 'b': 2} >>> dict1['a'] 1 >>> dict1['b'] 2

slide-32
SLIDE 32

32

Control Flow Control Flow

if condition: body elif condition: body else: body

Examples

if x%2 == 0: y = y + x else: y = y - x while i < 0: count = count + 1 for x in [1,2,3]: sum = sum + x while condition: body for name in iterable: body

slide-33
SLIDE 33

33

  • Syntax: range([start,] stop[, step])
  • Generate a list of numbers from start to stop stepping every step
  • start defaults to 0, step defaults to 1
  • Example

>>> range(5) [0, 1, 2, 3, 4] >>> range(1, 9) [1, 2, 3, 4, 5, 6, 7, 8] >>> range(2, 20, 5) [2, 7, 12, 17]

slide-34
SLIDE 34

34

  • Using range with for
  • Generate list used by for with range
  • Example

>>> for i in range(4): print i 1 2 3

slide-35
SLIDE 35

35

  • The continue statement
  • Continue to next iteration of loop, skipping remainder of body
  • Example:

>>> for x in range(8): if x%2 == 0: continue print x

  • 1

3 5 7

slide-36
SLIDE 36

36

  • The break statement
  • Break out of inner most loop
  • Example:

>>> for number in range(10): if number == 4: print 'Breaking' break else: print number 1 2 3 Breaking

slide-37
SLIDE 37

37

  • Data structures also have methods
  • Use built-in function dir to list all available methods
  • Example

>>> lst = [1, 3, 2] >>> dir(lst) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

slide-38
SLIDE 38

38

  • split
  • Syntax: string.split([sep])
  • Returns a list of strings
  • Example:

>>> text = "1 2 4 5 1" >>> text.split() ['1', '2', '4', '5', '1'] >>> test = "a, b, c, d, e" >>> test.split(',') ['a', ' b', ' c', ' d', ' e']

  • !"

!"

slide-39
SLIDE 39

39

  • strip
  • Syntax: string.strip()
  • Remove leading and trailing white space (tabs, new lines, etc)
  • Example:

>>> padded = " stuff " >>> padded.strip() 'stuff' >>> padded ' stuff ' >>> unpadded = padded.strip() >>> unpadded 'stuff'

  • !"

!"

slide-40
SLIDE 40

40

  • append
  • Syntax: list.append(element)
  • Add element to end of list
  • Example:

>>> list1 = [3, '10', 2] >>> list1.append('new') >>> list1 [3, '10', 2, 'new']

  • !"

!"

slide-41
SLIDE 41

41

  • pop
  • Syntax: list.pop([index])
  • Remove and return item at position index from list
  • Default is to remove last item
  • Example:

>>> list1 = [3, '10', 2, 9, 11] >>> list1.pop() 11 >>> list1 [3, '10', 2, 9]

  • !"

!"

slide-42
SLIDE 42

42

  • insert
  • Syntax: list.insert(index, element)
  • Insert element into list at position index
  • Example:

>>> list2 = [0, 1, 2, 3, 4, 5] >>> list2.insert(3, 'new') >>> list2 [0, 1, 2, 'new', 3, 4, 5]

  • !"

!"

slide-43
SLIDE 43

43

  • remove
  • Syntax: list.remove(element)
  • Removes the first occurrence of element in list
  • Example:

>>> list2 = [0, 1, 3, 4, 3, 5] >>> list2.remove(3) >>> list2 [0, 1, 4, 3, 5]

  • !"

!"

slide-44
SLIDE 44

44

  • sort
  • Syntax: list.sort([cmpfunc])
  • Sort list in place
  • Example:

>>> list3 = [4, 12, 3, 9] >>> list3.sort() >>> list3 [3, 4, 9, 12]

  • !"

!"

slide-45
SLIDE 45

45

  • reverse
  • Syntax: list.reverse()
  • Reverse elements of list in place
  • Example:

>>> list3 = [4, 12, 3, 9] >>> list3.reverse() >>> list3 [9, 3, 12, 4]

  • !"

!"

slide-46
SLIDE 46

46

  • keys
  • Syntax: dict.keys()
  • Return a list of all the keys in dict
  • Example:

>>> dict1 = {1: 'a', 9: 'cat', 2: [2, 1]} >>> dict1.keys() [1, 2, 9]

  • !"

!"

slide-47
SLIDE 47

47

  • has_key
  • Syntax: dict.has_key(key)
  • Determines if key is in dict
  • Example:

>>> dict1 = {'x': 1, 'y': 2} >>> dict1.has_key('x') 1 >>> dict1.has_key('w')

  • !"

!"

slide-48
SLIDE 48

48

  • values
  • Syntax: dict.values()
  • Returns a list of all values in dict
  • Example:

>>> dict1 = {(1,2): 'a', (3,4): 'b', (9,8,7): 'c'} >>> dict1.values() ['a', 'c', 'b']

  • !"

!"

slide-49
SLIDE 49

49

Getting Help Getting Help

  • For interactive use, calling help function will invoke the

built-in help system

  • Call help() without argument for interactive mode

>>> help(str) Help on class str in module __builtin__: class str(basestring) | str(object) -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object. | | Method resolution order: | str | basestring | object | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x :

slide-50
SLIDE 50

50

Functions and Scopes Functions and Scopes

slide-51
SLIDE 51

51

  • Syntax: def func(arg1, …):

body

  • Body of function must be indented
  • If no value is returned explicitly, function will return None
  • Example:

>>> def average(num1, num2, num3): sum = num1 + num2 + num3 avg = sum / 3.0 return avg

# #

slide-52
SLIDE 52

52

  • Parameters
  • Parameters can be any type
  • A function can take any number of parameters (or none at all)
  • Example:

>>> def usage(programName, version): print ‘%s Version %i' % (programName, version) print 'Usage: %s arg1 arg2‘ % (programName) >>> usage('Test', 1.0) Test Version 1.0 Usage: Test arg1 arg2

slide-53
SLIDE 53

53

  • Default Parameters
  • One or more parameters can be given a default value
  • The function can be called with fewer arguments than there are

parameters

  • All non-default (required) parameters must precede default parameters
  • Example:

>>> def printName(last, first, mi=""): print "%s, %s %s" % (last, first, mi) >>> printName("Smith", "John") Smith, John >>> printName("Smith", "John", "Q") Smith, John Q

slide-54
SLIDE 54

54

  • Calling functions
  • Syntax: func(arg1, arg2, … argn)
  • Order of arguments must match order of declared parameters
  • No type checking is done
  • Example

>>> def display(arg1, arg2, arg3): print arg1 print arg2 print arg3 >>> display(1, 'x', 4.3) 1 x 4.3

slide-55
SLIDE 55

55

  • Keyword arguments
  • Functions can be called using the keyword of the argument
  • Syntax: func(keyword=value, …)
  • The order of the values passed by keyword does not matter
  • Example

def keywords(key1="X", key2="X", key3="X",key4="X"): print key1, key2, key3, key4 >>> keywords(key3="O", key2="O") X O O X >>> keywords() X X X X

slide-56
SLIDE 56

56

  • Functions as variables
  • Functions can be assigned
  • Example

def sub(a, b): return a-b >>> op = sub >>> print op(3, 5)

  • 2

>>> type(op) <type 'function'>

slide-57
SLIDE 57

57

  • Functions as parameters
  • Functions can be passed to other functions
  • Example

def convert(data, convertFunc): for i in range(len(data)): data[i] = convertFunc(data[i]) return data >>> convert(['1', '5', '10', '53'], int) [1, 5, 10, 53] >>> convert(['1', '5', '10', '53'], float) [1.0, 5.0, 10.0, 53.0] >>> convert(['1', '5', '10', '53'], complex) [(1+0j), (5+0j), (10+0j), (53+0j)]

slide-58
SLIDE 58

58

  • Returning multiple values
  • Return a tuple containing the values to return
  • Example

def separate(text, size=3): start = text[:size] end = text[-size:] return (start, end) >>> separate('sample text') ('sam', 'ext') >>> start, end = separate('sample text') >>> print start sam >>> print end ext

slide-59
SLIDE 59

59

Generators Generators

  • Generators are functions that generate

sequence of items

– Generated sequence can be infinite

def fibonacci(): i = j = 1 while True: r, i, j = i, j, i+j yield r for rabbits in fibbonacci(): print rabbits if rabbits > 100: break 1 1 2 3 5 8 13 21 34 55 89 144

slide-60
SLIDE 60

60

Namespaces and Scopes Namespaces and Scopes

  • Namespace

– A mapping from names to objects – (Currently) implemented as Python dictionaries

  • Scope

– A region of program where a namespace is directly accessible – Name references search at most 3 scopes: local, global, built-in – Assignments create or change local names by default – Can force arguments to be global with global command

slide-61
SLIDE 61

61

Scope Example Scope Example

x = 99 def func(Y): Z = X+Y # X is not assigned, so it's global return Z func(1)

slide-62
SLIDE 62

62

  • A file containing Python definitions and statements
  • Modules can be “imported”
  • Module file name must end in .py
  • Used to divide code between files

! !

import math import string … math.py string.py

slide-63
SLIDE 63

63

  • Syntax 1: import <module name>
  • Module name is the file name without the .py extension
  • You must use the module name to call the functions
  • Example

>>> import math >>> dir(math) ['__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> print math.e 2.71828182846 >>> print math.sqrt(2.3) 1.51657508881

import import

slide-64
SLIDE 64

64

  • Syntax 2: from <module> import <name>
  • Import only a specific name from a module into global namespace
  • Module name is not required to access imported name
  • Example

>>> from math import sqrt >>> sqrt(16) 4 >>> dir(math) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined

import import

slide-65
SLIDE 65

65

  • Syntax 2a: from <module> import *
  • Import everything from a module into global namespace
  • Example

>>> dir() ['__builtins__', '__doc__', '__name__'] >>> from time import * >>> dir() ['__builtins__', '__doc__', '__name__', 'accept2dyear', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 'struct_time', 'time', 'timezone', 'tzname'] >>> time() 1054004638.75

import import

slide-66
SLIDE 66

66

Python Standard Libraries Python Standard Libraries

  • Some examples

sys - System-specific parameters and functions time - Time access and conversions thread - Multiple threads of control re - Regular expression operations email - Email and MIME handling package httplib - HTTP protocol client Tkinter - GUI package based on Tcl/Tk

  • See http://docs.python.org/library/index.html
slide-67
SLIDE 67

67

  • Opening a file
  • Syntax: open(fileName, mode)
  • Mode is one of:
  • 'r' : Read
  • 'w' : Write
  • 'a' : Append
  • If a file opened for writing does not exist it will be created
  • Example

>>> inFile = open('input.txt', 'r') >>> type(infile) <type 'file'>

$% $%

slide-68
SLIDE 68

68

  • read([size])
  • Read at most size bytes and return text as a string
  • readlines([size])
  • Read the lines of the file into a list of strings.
  • Use size as an approximate bound on the number of bytes returned

$% $% !" !"

slide-69
SLIDE 69

69

  • write(text)
  • Write text to the file
  • writelines(string_sequence)
  • Write each string in the sequence to the file
  • New lines are not added to the end of the strings

$% $% !" !"

slide-70
SLIDE 70

70

  • Definition
  • Errors detected during execution
  • Basic Example
  • Divide by zero

>>> 1 / 0 Traceback (most recent call last): File "<pyshell#0>", line 1, in ? 1 / 0 ZeroDivisionError: integer division or modulo by zero

& &

slide-71
SLIDE 71

71

  • Motivation
  • Move error handling code away from main code block
  • Deal with “exceptional” cases separately
  • How it works
  • Exceptions are thrown (or raised) and caught
  • Control exits the current code block when the exception is thrown
  • An exception can then be caught by a catching code block

& &

slide-72
SLIDE 72

72

  • Throwing
  • Many common operations may throw an exception
  • List index out of bounds
  • Invalid type conversions
  • Exceptions can be thrown manually using the raise keyword
  • >>> raise ValueError, "Bad Value“
  • Catching
  • Thrown exceptions must be caught
  • If the exception is never caught the progam will terminate

& &

slide-73
SLIDE 73

73

  • Handling Syntax

try: <try code block> except <Exception List>: <exception code block> except <Exception List>: <exception code block> except: <exception code block> else: <else code>

& &

slide-74
SLIDE 74

74

  • Example

try: x = 1 / 0 except ZeroDivisionError: print 'Divide by zero error' Divide by zero error

& &

slide-75
SLIDE 75

75

  • Example

try: x = 1 / 0 except IOError: print 'Input/Output error' except: print 'Unknown error' Unknown error

& &

slide-76
SLIDE 76

76

  • Types of Exceptions
  • There is a hierarchy of exceptions
  • All built-in exceptions are derived from Exception
  • An exception will be caught by any type higher up in the hierarchy

& &

Exception StandardError StopIteration SystemExit ArithmeticError LookupError ValueError IndexError KeyError OverflowError ZeroDivisionError

slide-77
SLIDE 77

77

  • Example

try: x = 1 / 0 except Exception: print 'Exception caught' Exception caught

& &

slide-78
SLIDE 78

78

  • Propagation
  • If no proper except block can be found in the current block, the

exception propagates back to the calling function

def func1(): try: a = 1 / 0 … except ValueError: print 'first' def func2(): try: func1() except: print 'second' >>> func2() second

& &

slide-79
SLIDE 79

79

  • Example

try: x = 1 / 0 except Exception: print 'Exception caught' Exception caught

& &

slide-80
SLIDE 80

80

OO Programming OO Programming

slide-81
SLIDE 81

81

Defining a Class Defining a Class

  • Syntax:
  • Creating a class with no superclass
  • Basically, classes are simply namespaces

class MyClass(object): myvar = 30 >>> MyClass.myvar 30

class name[(base)]: body class name: body

Old-style class New-style class

class name(object): body

slide-82
SLIDE 82

82

Class Example Class Example

  • All instance methods must explicitly take an

instance as the first parameter

– self is a commonly used name

class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height >>> r = Rectangle(10, 20) >>> Rectangle.area(r) 200 >>> r.area() 200

Constructor

slide-83
SLIDE 83

83

Inheritance Inheritance

  • Subclass must invoke parent's constructor

explicitly

class Square(Rectangle): def __init__(self, width): Rectangle.__init__(self, width, width) >>> s = Square(100) >>> s.area() 10000

slide-84
SLIDE 84

84

Polymorphism Polymorphism

  • All methods are virtual

import math class Circle(object): def __init__(self, radius): self.radius = radius def area(self): return math.pi*self.radius*self.radius >>> shapes = [Square(5), Rectangle(2,8), Circle(3)] >>> for x in shapes: ... print x.area() ... 25 16 28.2743338823

slide-85
SLIDE 85

85

Python Object Hooks Python Object Hooks

  • Objects can support built-in operators by

implementing certain special methods

– The usual operators: +, -, *, /, **, &, ^, ~, != – Indexing (like sequences): obj[idx] – Calling (like functions): obj(args,...) – Iteration and containment tests

  • for item in obj:...
  • if item in obj:...
slide-86
SLIDE 86

86

Functional Programming Functional Programming

slide-87
SLIDE 87

87

  • Taken from functional languages
  • Lisp/Scheme
  • Haskell
  • Added to Python as built-in functions
  • map()
  • filter()
  • reduce()
  • zip()

'" '"

slide-88
SLIDE 88

88

  • Perform an operation on each element of a list
  • A function is applied to each element
  • The results of each function call are used to generate a new list
  • The resulting list is always the same length as the original list
  • The original list is not altered
  • #

#map map

slide-89
SLIDE 89

89

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ1

slide-90
SLIDE 90

90

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ2 ŷ1

slide-91
SLIDE 91

91

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ3 ŷ1 ŷ2

slide-92
SLIDE 92

92

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ4 ŷ1 ŷ2 ŷ3

slide-93
SLIDE 93

93

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ5 ŷ1 ŷ2 ŷ3 ŷ4

slide-94
SLIDE 94

94

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ6 ŷ1 ŷ2 ŷ3 ŷ4 ŷ5

slide-95
SLIDE 95

95

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ7 ŷ1 ŷ2 ŷ3 ŷ4 ŷ5 ŷ6

slide-96
SLIDE 96

96

  • #

#map map

func y1 y2 y3 y4 y5 y6 y7 y8 ŷ8 ŷ1 ŷ2 ŷ3 ŷ4 ŷ5 ŷ6 ŷ7

slide-97
SLIDE 97

97

  • Syntax 1: map(func, list)
  • Example: Convert a list of integers to strings

>>> lst1 = [1, 2, 3, 4] >>> lst2 = map(str, lst1) >>> print lst2 ['1', '2', '3', '4']

  • The function (str) takes one argument
  • The result (lst2) is the same length as the original (lst1)
  • #

#map map

slide-98
SLIDE 98

98

  • What if the function requires more than one

argument?

  • Multiple lists can be passed to the map function
  • Syntax 2: map(func, list1, …, listn)
  • All lists must be of same length
  • The function must take n parameters
  • #

#map map

slide-99
SLIDE 99

99

  • Example: adding numbers

def add2(a, b): return a+b >>> lst1 = [0, 1, 2, 3] >>> lst2 = [4, 5, 6, 7] >>> print map(add2, lst1, lst2) [4, 6, 8, 10]

  • #

#map map

slide-100
SLIDE 100

100

  • lst1 = [1, 2, 3, 4]

lst2 = [] for element in lst1: lst2.append(str(element)) lst1 = [1, 2, 3, 4] lst2 = map(str, lst1)

slide-101
SLIDE 101

101

  • lst1 = [0, 1, 2, 3]

lst2 = [4, 5, 6, 7] lst3 = [] for index in range(len(lst1)): lst3.append(add2(lst1[index], lst2[index])) lst1 = [0, 1, 2, 3] lst2 = [4, 5, 6, 7] lst2 = map(add2, lst1, lst2)

slide-102
SLIDE 102

102

  • The map function can be used like an expression
  • Can be used as a parameter to a function
  • Example:

>>> lst1 = [1, 2, 3, 4] >>> string.join(lst1) # Error because lst1 contains ints … TypeError: sequence item 0: expected string, int found >>> string.join( map(str, lst1) ) # Correct '1 2 3 4'

# #

slide-103
SLIDE 103

103

  • Remove elements of a list based on a condition
  • Each element of a list is tested, those that fail are removed
  • The resulting list is the same length or shorter than the original
  • The original list is not altered
  • #

#filter filter

slide-104
SLIDE 104

104

  • Syntax: filter(func, list)
  • Example: Remove all negative numbers

def removeNegative(number): return number >= 0 >>> lst1 = [1, 2, -3, 4] >>> lst2 = filter(removeNegative, lst1) >>> print lst2 [1, 2, 4]

  • The function (str) takes one argument and returns 0 or 1
  • The result (lst2) is shorter than the original (lst1)
  • #

#filter filter

slide-105
SLIDE 105

105

  • lst1 = [1, 2, -3, 4]

lst2 = [] for element in lst1: if removeNegative(element): lst2.append(element) lst1 = [1, 2, -3, 4] lst2 = filter(removeNegative, lst1)

slide-106
SLIDE 106

106

  • Apply a function cumulatively to a sequence
  • The function must take 2 parameters
  • Function is applied to parameters from left to right
  • The list is reduced to a single value
  • The original list is not altered
  • #

#reduce reduce

slide-107
SLIDE 107

107

  • #

#reduce reduce

func y1 y2 y3 y4 t1 func t2 func t3

slide-108
SLIDE 108

108

&($' &($'

1+10 1 10 20 30 11 11+20 31 31+30 61

slide-109
SLIDE 109

109

  • Syntax 1: reduce(func, list)
  • Example: Find the sum of a list of integers

>>> lst1 = [1, 2, 3, 4] >>> sum = reduce(operator.add, lst1) >>> print sum 10

  • The function (operator.add) takes two arguments
  • The result is a single value
  • #

#reduce reduce

slide-110
SLIDE 110

110

  • Syntax 2: reduce(func, list, initialValue)
  • The initialValue is applied to func with the first value in the list
  • If the list is empty, the initialValue is returned
  • Example: Concatenating lists

>>> lst1 = [ [2, 4], [5, 9], [1, 7] ] >>> result = reduce(operator.add, lst1, [100]) >>> print result [100, 2, 4, 5, 9, 1, 7]

  • #

#reduce reduce

slide-111
SLIDE 111

111

  • lst1 = [1, 2, 3, 4]

sum = operator.add(lst1[0], lst1[1]) for element in lst1[2:]: sum = operator.add(sum, element) lst1 = [1, 2, 3, 4] sum = reduce(operator.add, lst1)

slide-112
SLIDE 112

112

  • lst1 = [ [2, 4], [5, 9], [1, 7] ]

result = operator.add([100], lst1[0]) for element in lst1[1:]: result = operator.add(sum, element) lst1 = [ [2, 4], [5, 9], [1, 7] ] result = reduce(operator.add, lst1, [100])

slide-113
SLIDE 113

113

  • Combine multiple lists into one
  • Elements are paired by index
  • The resulting list is the same length as the shortest list supplied
  • Each element of resulting list contains a tuple
  • The original lists are not altered
  • #

#zip zip

slide-114
SLIDE 114

114

  • #

#zip zip

y1 y2 y3 y4 x1 x2 x3 x4 (x1, y1) (x2, y2) (x4, y4) (x3, y3)

slide-115
SLIDE 115

115

  • Syntax: zip(list1, …, listn)
  • Example: Combine two lists

>>> lst1 = [1, 2, 3, 4] >>> lst2 = ['a', 'b', 'c', 'd', 'e'] >>> result = zip(lst1, lst2) >>> print result [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

  • The ‘e’ element was truncated since lst1 only has 4 elements
  • The result is a list of tuples
  • #

#zip zip

slide-116
SLIDE 116

116

  • lst1 = [1, 2, 3, 4]

lst2 = ['a', 'b', 'c', 'd', 'e'] lst3 = [] for index in range(min(len(lst1), len(lst2)): lst3.append( (lst1[index], lst2[index]) ) lst1 = [1, 2, 3, 4] lst2 = ['a', 'b', 'c', 'd', 'e'] lst3 = zip(lst1, lst2)

slide-117
SLIDE 117

117

  • Iterate over two lists simultaneously

>>> produce = ['apples', 'oranges', 'pears'] >>> prices = [0.50, 0.45, 0.55] >>> for fruit, cost in zip(produce, prices): print '%s cost $%.2f'%(fruit, cost) apples cost $0.50

  • ranges cost $0.45

pears cost $0.55

# #zip zip

slide-118
SLIDE 118

118

  • Create a dictionary using dict()

>>> produce = ['apples', 'oranges', 'pears'] >>> prices = [0.50, 0.45, 0.55] >>> priceDict = dict(zip(produce, prices)) >>> print priceDict {'pears': 0.55, 'apples': 0.5, 'oranges': 0.45}

# #zip zip

slide-119
SLIDE 119

119

  • Generate new lists from old ones
  • Can simultaneously map and filter
  • More flexible than the built-in functions

" "

slide-120
SLIDE 120

120

  • Basic Syntax: [<exp> for <var> in <list>]
  • The resulting list is the result of exp evaluated for each var in list
  • Example: Increase each element by 1

>>> [x+1 for x in range(5)] [1, 2, 3, 4, 5]

  • Example: Convert each element to a string

>>> [str(x) for x in range(5)] ['0', '1', '2', '3', '4']

" "

slide-121
SLIDE 121

121

  • Syntax: [<ex1> for <var> in <list> if <ex2>]
  • ex1 is only evaluated if ex2 is true
  • Example: Remove smallest element

>>> lst1 = [5, 10, 3, 9] >>> [x for x in lst1 if x != min(lst1)] [5, 10, 9]

  • Example: Sum all lists of size greater than 2

>>> lst1 = [[1, 2, 4], [3, 1], [5, 9, 10, 11]] >>> [reduce(operator.add, x) for x in lst1 if len(x) > 2] [7, 35]

!" !"

slide-122
SLIDE 122

122

  • Multiple for loops can be included
  • The loops will be nested
  • Example: Generate all possible combinations of letters a, c, g, t

>>> nucleo = ['a', 'g', 'c', 't'] >>> [a+b for a in nucleo for b in nucleo] ['aa', 'ag', 'ac', 'at', 'ga', 'gg', 'gc', 'gt', 'ca', 'cg', 'cc', 'ct', 'ta', 'tg', 'tc', 'tt']

!" !"

slide-123
SLIDE 123

123

  • nucleo = ['a', 'g', 'c', 't']

results = [] for a in nucleo: for b in nucleo: results.append(a+b) nucleo = ['a', 'g', 'c', 't'] results = [a+b for a in nucleo for b in nucleo]

slide-124
SLIDE 124

124

  • Anonymous functions
  • Body can consist of only a single expression
  • Execute slightly slower than normal functions
  • Can take any number of parameters
  • In general, lambda functions should be avoided

$ $

slide-125
SLIDE 125

125

  • Syntax : lambda p1[,…,pn]: <exp>
  • Expression should not use return
  • Example: adding two numbers

>>> add2 = lambda a,b: a+b >>> print add2(1, 5) 6

  • Example: simple factorial

>>> fac = lambda x: reduce(lambda a,b: a*b, range(1, x+1)) >>> print fac(5) 120

$ $

slide-126
SLIDE 126

126

References References

  • Python Documentation

http://docs.python.org

  • Python for Programmers by Alex Martelli
  • Advanced Python (Understanding Python)

by Thomas Wouters

slide-127
SLIDE 127

127

Exercise Exercise

  • Write a Python program freq.py to:

– Fetch a text file from the web – Then report the most 10 frequently used words

$ python freq.py http://www.cpe.ku.ac.th/~cpj/gpl.txt 345: the 221: of 192: to 184: a 151: or 128: you 102: license 98: and 97: work 91: that $

slide-128
SLIDE 128

128

Exercise Exercise – – Hints Hints

  • Accessing command-line arguments
  • Reading a webpage
  • Extracting all English words from text

import urllib contents = urllib.urlopen(url).read() import sys url = sys.argv[1] import re words = re.findall('[A-Za-z]+', text)