Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python...

128
1 Introduction to Python Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

Transcript of Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python...

Page 1: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

1

Introduction to PythonIntroduction to Python

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

Page 2: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

2

OutlineOutline

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

Page 3: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

3

Python At First GlancePython At First Glanceimport 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 = widthself.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

Page 4: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

Page 5: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

5

Similarities to JavaSimilarities to Java

• Everything inherits from "object"– Also numbers, functions, classes, …

– Everything is first-class

• Vast, powerful standard library• Garbage collection• Introspection, serialization, threads, net,…

Page 6: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

6

Similarities to C++Similarities to C++

• Multi-paradigm– OOP, procedural, generic, functional (a little)

• Multiple inheritance• Operator overloading

Page 7: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

7

Python vs. Java/C++/CPython 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 + tmpy = y * x

print y

Python

Page 8: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

8

Getting StartedGetting 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

Page 9: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

9

using System;class Hello{

static void Main(){

Console.WriteLine("Hello, World");}

}

Hello, World!Hello, World!

• C#

• Pythonprint "Hello, World!"

Page 10: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

10

VariablesVariables

>>> x = 23>>> print x23>>> x = 'foo'>>> print xfoo>>> del x>>> print xTraceback (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

Page 11: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

11

Var1

Var1_copy

Var2

VariablesVariables

• Reference Model– Variables refer to an object

– More than one variable can refer to the same object

Page 12: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

12

Numeric TypesNumeric 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

Page 13: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

13

StringsStrings• 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

Page 14: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

14

>>> info = raw_input('-> ')-> Here is info>>> print infoHere is info

BuiltBuilt --in Function: in Function: raw_inputraw_input

• Syntax: raw_input([prompt])– Use prompt to ask user to input a string

– Example

Page 15: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

15

• Arithmetic- + - * // / ** % abs

- Example

>>> 5 + 3 # Addition8>>> 2 ** 8 # Exponentiation256>>> 13 / 4 # Integer (Truncating) Division*3>>> float(13) / 4 # Float Division3.25>>> 13 % 4 # Remainder1>>> abs(-3.5) # Absolute Value3.5

Basic OperationsBasic Operations

* Becomes float division in version 3.x

Page 16: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

16

• Comparison- < <= > >= == != <>

- Results in 1 (true) or 0 (false)

- Example>>> 4 > 1.51>>> 'this' != 'that'1>>> 4+3j == 4-2j0>>> '5' == 50>>> 0 < 10 < 201

Basic OperationsBasic Operations

Page 17: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

17

• Boolean- and or not

- Based on Boolean Algebra

Basic OperationsBasic Operations

i 1 i 2 i 1 and i 2

1

1

1

0

0

0

1

0

1

0

0

0

i 1 or i 2

1

1

1

0

i 1 not i 1

1

0 1

0

Page 18: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

18

• Boolean

- Example>>> 1 == 1 and 2 >= 30>>> 1 == 1 or 2 >= 31>>> not 5.3 != 2.2 # same as: not (5.3 != 2.2)0>>> 2 and '23' > '11' or 01

Basic OperationsBasic Operations

Page 19: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

19

• Concatenation (+)- Syntax: string1 + string2

- Example:

>>> 'Rockefeller' + 'University'

'RockefellerUniversity'

• Repetition (*)- Syntax: string * number

- Example:

>>> 'dog' * 5

'dogdogdogdogdog'

Strings Strings -- OperationsOperations

Page 20: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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'

Strings Strings -- FormattingFormatting

Page 21: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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'>

BuiltBuilt--in Function: typein Function: type

Page 22: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

Type ConversionsType Conversions

Page 23: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

23

• Lists

• Tuples

• Dicts

Data StructuresData Structures

Page 24: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

ListsLists

Page 25: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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']

Lists Lists -- OperationsOperations

Page 26: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

26

• Indexing operator: [ ]

• Positive indices count from the left

• Negative indices count from the right

IndexingIndexing

0 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

Page 27: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

List SlicingList Slicing

Page 28: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

TuplesTuples

Page 29: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in Function: in Function: lenlen

Page 30: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

30

• Mapping- Associate a key with a value

- Each key must be unique

DictionariesDictionaries

'z' 'ab' 2.1 3keys

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

Page 31: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

DictionariesDictionaries

Page 32: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

32

Control FlowControl 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

Page 33: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

BuiltBuilt--in Function: rangein Function: range

Page 34: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

34

• Using range with for

- Generate list used by for with range

- Example

>>> for i in range(4):

print i

0

1

2

3

Controlling FlowControlling Flow

Page 35: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

Controlling FlowControlling Flow

1

3

5

7

Page 36: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

0

1

2

3

Breaking

Controlling FlowControlling Flow

Page 37: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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', 'reve rse', 'sort']

Using Data StructuresUsing Data Structures

Page 38: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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']

Strings Strings -- MethodsMethods

Page 39: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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'

Strings Strings -- MethodsMethods

Page 40: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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']

Lists Lists -- MethodsMethods

Page 41: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

Lists Lists -- MethodsMethods

Page 42: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

Lists Lists -- MethodsMethods

Page 43: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

Lists Lists -- MethodsMethods

Page 44: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

44

• sort- Syntax: list.sort([cmpfunc])

- Sort list in place

- Example:

>>> list3 = [4, 12, 3, 9]

>>> list3.sort()

>>> list3

[3, 4, 9, 12]

Lists Lists -- MethodsMethods

Page 45: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

45

• reverse- Syntax: list.reverse()

- Reverse elements of list in place

- Example:

>>> list3 = [4, 12, 3, 9]

>>> list3.reverse()

>>> list3

[9, 3, 12, 4]

Lists Lists -- MethodsMethods

Page 46: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

Dictionaries Dictionaries -- MethodsMethods

Page 47: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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')

0

Dictionaries Dictionaries -- MethodsMethods

Page 48: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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']

Dictionaries Dictionaries -- MethodsMethods

Page 49: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

49

Getting HelpGetting 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 objec t.| 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

:

Page 50: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

50

Functions and ScopesFunctions and Scopes

Page 51: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

Defining FunctionsDefining Functions

Page 52: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

FunctionsFunctions

Page 53: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

FunctionsFunctions

Page 54: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

FunctionsFunctions

Page 55: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

FunctionsFunctions

Page 56: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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'>

FunctionsFunctions

Page 57: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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)]

FunctionsFunctions

Page 58: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

FunctionsFunctions

Page 59: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

59

GeneratorsGenerators

• Generators are functions that generate sequence of items– Generated sequence can be infinite

def fibonacci():i = j = 1while True:

r, i, j = i, j, i+jyield r

for rabbits in fibbonacci():print rabbitsif rabbits > 100: break

1 1 2 3 5 8 13 21 34 55 89 144

Page 60: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

60

Namespaces and ScopesNamespaces 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

Page 61: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

61

Scope ExampleScope Example

x = 99def func(Y):

Z = X+Y # X is not assigned, so it's globalreturn Z

func(1)

Page 62: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

ModulesModules

import mathimport string…

math.py string.py

Page 63: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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', 'at an2', '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

importimport StatementStatement

Page 64: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

importimport StatementStatement

Page 65: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

importimport StatementStatement

Page 66: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

66

Python Standard LibrariesPython Standard Libraries

• Some examplessys - System-specific parameters and functionstime - Time access and conversionsthread - Multiple threads of controlre - Regular expression operationsemail - Email and MIME handling packagehttplib - HTTP protocol clientTkinter - GUI package based on Tcl/Tk

• See http://docs.python.org/library/index.html

Page 67: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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'>

File ObjectsFile Objects

Page 68: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

File Objects File Objects -- MethodsMethods

Page 69: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

File Objects File Objects -- MethodsMethods

Page 70: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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 ze ro

ExceptionsExceptions

Page 71: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

ExceptionsExceptions

Page 72: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

ExceptionsExceptions

Page 73: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

73

• Handling Syntaxtry:

<try code block>

except <Exception List>:

<exception code block>

except <Exception List>:

<exception code block>

except:

<exception code block>

else:

<else code>

ExceptionsExceptions

Page 74: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

74

• Exampletry:

x = 1 / 0

except ZeroDivisionError:

print 'Divide by zero error'

Divide by zero error

ExceptionsExceptions

Page 75: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

75

• Exampletry:

x = 1 / 0

except IOError:

print 'Input/Output error'

except:

print 'Unknown error'

Unknown error

ExceptionsExceptions

Page 76: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

ExceptionsExceptions

Exception

StandardError

StopIteration

SystemExit

ArithmeticError

LookupError

ValueError

IndexError

KeyError

OverflowError

ZeroDivisionError

Page 77: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

77

• Exampletry:

x = 1 / 0

except Exception:

print 'Exception caught'

Exception caught

ExceptionsExceptions

Page 78: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

ExceptionsExceptions

Page 79: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

79

• Exampletry:

x = 1 / 0

except Exception:

print 'Exception caught'

Exception caught

ExceptionsExceptions

Page 80: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

80

OO ProgrammingOO Programming

Page 81: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

81

Defining a ClassDefining a Class

• Syntax:

• Creating a class with no superclass

• Basically, classes are simply namespacesclass MyClass(object):

myvar = 30

>>> MyClass.myvar30

class name[( base)]:body

class name:body

Old-style class New-style class

class name( object ):body

Page 82: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

82

Class ExampleClass 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 = widthself.height = height

def area(self):return self.width * self.height

>>> r = Rectangle(10, 20)>>> Rectangle.area(r)200>>> r.area()200

Constructor

Page 83: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

83

InheritanceInheritance

• 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

Page 84: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

84

PolymorphismPolymorphism

• 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()...251628.2743338823

Page 85: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

85

Python Object HooksPython 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:...

Page 86: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

86

Functional ProgrammingFunctional Programming

Page 87: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

87

• Taken from functional languages- Lisp/Scheme

- Haskell

• Added to Python as built-in functions- map()

- filter()

- reduce()

- zip()

Functional ApproachesFunctional Approaches

Page 88: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: mapmap

Page 89: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

89

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ1

Page 90: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

90

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ2ŷ1

Page 91: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

91

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ3ŷ1 ŷ2

Page 92: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

92

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ4ŷ1 ŷ2 ŷ3

Page 93: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

93

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ5ŷ1 ŷ2 ŷ3 ŷ4

Page 94: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

94

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ6ŷ1 ŷ2 ŷ3 ŷ4 ŷ5

Page 95: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

95

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ7ŷ1 ŷ2 ŷ3 ŷ4 ŷ5 ŷ6

Page 96: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

96

BuiltBuilt--in function: in function: mapmap

func

y1 y2 y3 y4 y5 y6 y7 y8

ŷ8ŷ1 ŷ2 ŷ3 ŷ4 ŷ5 ŷ6 ŷ7

Page 97: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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 )

BuiltBuilt--in function: in function: mapmap

Page 98: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

98

• What if the function requires more than one argument?- Multiple lists can be passed to the map function

• Syntax 2: map(func, list 1, …, list n)

- All lists must be of same length

- The function must take n parameters

BuiltBuilt--in function: in function: mapmap

Page 99: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

99

• Example: adding numbersdef 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]

BuiltBuilt--in function: in function: mapmap

Page 100: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

100

Code ComparisonCode Comparison

lst1 = [1, 2, 3, 4]

lst2 = []

for element in lst1:

lst2.append(str(element))

lst1 = [1, 2, 3, 4]

lst2 = map(str, lst1)

Page 101: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

101

Code ComparisonCode Comparison

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)

Page 102: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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 fou nd

>>> string.join( map(str, lst1) ) # Correct

'1 2 3 4'

BenefitsBenefits

Page 103: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: filterfilter

Page 104: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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 )

BuiltBuilt--in function: in function: filterfilter

Page 105: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

105

Code ComparisonCode Comparison

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)

Page 106: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: reducereduce

Page 107: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

107

BuiltBuilt--in function: in function: reducereduce

func

y1 y2 y3 y4

t1

func

t2

func

t3

Page 108: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

108

Example: Reduce by AddingExample: Reduce by Adding

1+10

1 10 20 30

11

11+20

31

31+30

61

Page 109: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: reducereduce

Page 110: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

BuiltBuilt--in function: in function: reducereduce

Page 111: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

111

Code ComparisonCode Comparison

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)

Page 112: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

112

Code ComparisonCode Comparison

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])

Page 113: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: zipzip

Page 114: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

114

BuiltBuilt--in function: in function: zipzip

y1 y2 y3 y4x1 x2 x3 x4

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

Page 115: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

BuiltBuilt--in function: in function: zipzip

Page 116: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

116

Code ComparisonCode Comparison

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)

Page 117: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

oranges cost $0.45

pears cost $0.55

Uses for Uses for zipzip

Page 118: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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}

Uses for Uses for zipzip

Page 119: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

119

• Generate new lists from old ones- Can simultaneously mapand filter

- More flexible than the built-in functions

List ComprehensionsList Comprehensions

Page 120: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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']

Basic List ComprehensionsBasic List Comprehensions

Page 121: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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]

More List ComprehensionsMore List Comprehensions

Page 122: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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', 'c a', 'cg', 'cc', 'ct', 'ta', 'tg', 'tc', 'tt']

More List ComprehensionsMore List Comprehensions

Page 123: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

123

Code ComparisonCode Comparison

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]

Page 124: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

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

Lambda FunctionsLambda Functions

Page 125: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

125

• Syntax : lambda p 1[,…,p n]: <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

Lambda FunctionsLambda Functions

Page 126: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

126

ReferencesReferences

• Python Documentation� http://docs.python.org

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

by Thomas Wouters

Page 127: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

127

ExerciseExercise

• 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.tx t345: the221: of192: to184: a151: or128: you102: license

98: and97: work91: that

$

Page 128: Introduction to Pythoncpj/204223/slides/Intro-Python.pdf · 2016-08-22 · 1 Introduction to Python Materials based on contents from the course Programming with Python by Chad Haynes

128

Exercise Exercise –– HintsHints

• Accessing command-line arguments

• Reading a webpage

• Extracting all English words from text

import urllibcontents = urllib.urlopen( url).read()

import sysurl = sys.argv[1]

import rewords = re.findall('[A-Za-z]+', text)