CIS192: Python Programming

Similar documents
CIS192 Python Programming

CIS192 Python Programming

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

CIS192 Python Programming

MUTABLE LISTS AND DICTIONARIES 4

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

CIS192 Python Programming

CIS192 Python Programming

CSE : Python Programming

LECTURE 2. Python Basics

peval Documentation Release Bogdan Opanchuk

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

LECTURE 16. Functional Programming

CIS192 Python Programming

Python Programming: Lecture 3 Functions

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

CIS192 Python Programming

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

TAIL RECURSION, SCOPE, AND PROJECT 4 11

COMP 364: Functions II

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Shell CSCE 314 TAMU. Haskell Functions

TUPLES AND RECURSIVE LISTS 5

Positional, keyword and default arguments

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

CS2304: Python for Java Programmers. CS2304: Advanced Function Topics

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Python debugging and beautification

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Extending Jython. with SIM, SPARQL and SQL

CIS192 Python Programming

Try and Error. Python debugging and beautification

Introduction to Programming in Python (2)

Introduction to Python! Lecture 2

Introduction to Python

functional programming in Python, part 2

Sequences and iteration in Python

IAP Python - Lecture 2

CIS192 Python Programming

Programming with Python

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Principles of Programming Languages

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Python in 10 (50) minutes

Chapter 11 :: Functional Languages

Python Interview Questions & Answers

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

funcsigs Documentation

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

ITERATORS AND STREAMS 9

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Subprogram Concept. COMP3220 Principle of Programming Languages. Zhitao Gong Spring

Lecture #11: Immutable and Mutable Data. Last modified: Sun Feb 19 17:03: CS61A: Lecture #11 1

Programming Languages: Application and Interpretation

SD314 Outils pour le Big Data

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

SCHEME AND CALCULATOR 5b

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Assignment 7: functions and closure conversion (part 1)

Working with Lists 4

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Basic Python 3 Programming (Theory & Practical)

Python Decorators. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 28, 2010

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

Part I Basic Concepts 1

UCT Department of Computer Science Computer Science 1017F. Functions. Lighton Phiri April 2015

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CS115 - Module 9 - filter, map, and friends

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Functions parameters, scope CS GMU

Recall that strings and tuples are immutable datatypes, while lists are mutable datatypes. What does this mean?

Computational Concepts Toolbox. Object Oriented Programming. Today: class. Review: Objects

Assignment 7: functions and closure conversion


Python Basics. Lecture and Lab 5 Day Course. Python Basics

python 01 September 16, 2016

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Structure and Interpretation of Computer Programs

CSE : Python Programming

LECTURE 3 Python Basics Part 2

Structure and Interpretation of Computer Programs

Recap: Functions as first-class values

Scala : an LLVM-targeted Scala compiler

(CC)A-NC 2.5 by Randall Munroe Python

Introduction to Python programming, II

User-defined Functions. Conditional Expressions in Scheme

61A Lecture 2. Friday, August 28, 2015

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Lecture #12: Quick: Exceptions and SQL

Transcription:

CIS192: Python Programming Functions and Functional Programming Harry Smith University of Pennsylvania January 25, 2018 Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 1 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 2 / 39

List Comprehensions [expr for v in iter] [expr for v1,v2 in iter] [expr for v in iter if cond] Translate the following into a loop: res = [v1 * v2 for v1, v2 in lst if v1 > v2] res = [] for {1} in {0}: if {2}: {3} Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 3 / 39

List Comprehensions [expr for v in iter] [expr for v1,v2 in iter] [expr for v in iter if cond] Translate the following into a loop: res = [v1 * v2 for v1, v2 in lst if v1 > v2] res = [] for {1} in lst: if {2}: {3} Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 4 / 39

List Comprehensions [expr for v in iter] [expr for v1,v2 in iter] [expr for v in iter if cond] Translate the following into a loop: res = [v1 * v2 for v1, v2 in lst if v1 > v2] res = [] for v1, v2 in lst: if {2}: {3} Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 5 / 39

List Comprehensions [expr for v in iter] [expr for v1,v2 in iter] [expr for v in iter if cond] Translate the following into a loop: res = [v1 * v2 for v1, v2 in lst if v1 > v2] res = [] for v1, v2 in lst: if v1 > v2: {3} Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 6 / 39

List Comprehensions [expr for v in iter] [expr for v1,v2 in iter] [expr for v in iter if cond] Translate the following into a loop: res = [v1 * v2 for v1, v2 in lst if v1 > v2] res = [] for v1, v2 in lst: if v1 > v2: res.append(v1 * v2) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 7 / 39

Nested List Comp [[4 for 3 in 2] for 1 in 0] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 8 / 39

Nested List Comp [[4 for 3 in 2] for 1 in lst2] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 9 / 39

Nested List Comp [[4 for 3 in 2] for tup in lst2] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 10 / 39

Nested List Comp [[4 for 3 in tup] for tup in lst2] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 11 / 39

Nested List Comp [[4 for x in tup] for tup in lst2] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 12 / 39

Nested List Comp [[x for x in tup] for tup in lst2] Translate the following into the above list comprehension, given that lst2 is a list of tuples res = [] for tup in lst2: inter = [] for x in tup: inter.append(x) res.append(inter) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 13 / 39

Extra for s and if s [x for x in lst1 if x > 2 for y in lst2 for z in lst3 if x + y + z > 8] Translation: res = [] for x in lst1: if x > 2: for y in lst2: for z in lst3: if x + y + z > 8: res.append(x) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 14 / 39

Extra for s and if s Translate this?? res = [] for x in lst1: if x > 2: for y in lst2: for z in lst3: if x + y + z > 8: res.append(x) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 15 / 39

Extra for s and if s [x for x in lst1 if x > 2 for y in lst2 for z in lst3 if x + y + z > 8] Translate this?? res = [] for x in lst1: if x > 2: for y in lst2: for z in lst3: if x + y + z > 8: res.append(x) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 16 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 17 / 39

Positional Arguments def func(arg1, arg2, arg3): arg1 arg2 and arg3 are positional arguments When calling func exactly 3 arguments must be given The order in the call determines which arg they are bound to func(a, b, c) The expressions a, b, c are evaluated before the call The value of a is bound to arg1 in the body of func Likewise b to arg2 and c to arg3 Calling a function with the wrong number of args gives a TypeError Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 18 / 39

Named Arguments After the positional args, named args are allowed def func(arg1, named1=val1, named2=val2): named1 and named2 are variables usable in the body of func val1 and val2 are default values for those variables. Omitting named arguments in a call uses the default value func(a, named2=b, named1=c) named arguments can be given out of order func(a, named2=b) The default value, val1 will be bound to named1 Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 19 / 39

Default Arguments Default arguments are evaluated when the function is defined In all calls, the object that the expression evaluated to will be used. If the default is mutable, updates in one call effect following calls def func(a=[]) Will mutate the default on each call def func(a=none): if a is None: a = [] Use None as the default to avoid mutation Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 20 / 39

Memoization Memoization is an optimization technique that stores results of function calls The previously computed answers can be looked up on later calls Use a dictionary default arg to store answers def func(arg, cache={}): Store answers in cache[arg] = ans Check for arg in cache before doing any work Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 21 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 22 / 39

*args A variable number of positional arguments can be specifed Use *args in between positional and named args Could use any identifier but args is conventional def func(arg1, *args, named=val) args is a tuple of 0 or more objects func(a, b, c) arg1 = a, args = (b, c) named gets the default object Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 23 / 39

Required Keyword Args Any args after *args are keyword args If there is no default value specified, they are Required Keyword Args def func(*args, named): named is a required keword arg To specify required keyword args without allowing variable positional args use * def func(arg1, *, named) named is a required kwarg func must take exactly one pos arg and one kwarg Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 24 / 39

**kwargs A variable number of kwargs can be specifed Use **kwargs at the end Could use any identifier but kwargs is conventional def func(arg1, *args, named=val, **kwargs) kwargs is a dictionary of strings to values The keys of kwargs are the names of the keyword args func(a, extra1=b, extra2=c) arg1 = a, args = tuple() named gets the default object kwargs = { extra1 : b, extra2 : c} Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 25 / 39

*/** in Function Definition def(*args) args is a tuple that can take 0 or more values def(**kwargs) kwargs is a dictionary that can take 0 or more key-value pairs Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 26 / 39

*/** in Function Calls func(*expr) expr is an iterable It gets unpacked as the positional arguments of func Equivalently seq = list(expr); func(seq[0], seq[1],...) func(**expr) expr is a dictionary of form { string : val,...} It gets unpacked as the keyword arguments of func Equivalently func(string=val,...) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 27 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 28 / 39

Closures A function that knows about variable defined outside the function a = 42 def func(): print(a) func is a closure because it knows about a Closures are read-only in Python a = 42 def func(): print(a) a += 1 UnboundLocalError: local variable a referenced before assignment Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 29 / 39

global global can circumvent read-only closures the global keyword declares certain variables in the current code block to reference the global scope a = 42 def func(): global a print(a) a += 1 This does not raise an error Variables following global do not need to be bound already Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 30 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 31 / 39

Background Functional programming started with lambda(λ) calculus Alternative to Turing machines for exploring computability Expresses programs as functions operating on other functions Functional programming attempts to make it easier to reason about program behavior Mathematical interpretation of functions allows mathematical proofs If data is immutable and there are no side-effects then functions always behave the same way Python data is mutable and allows side-effects Has some functional concepts Not an ideal functional programming environment Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 32 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 33 / 39

First Class Functions A higher order function is a function that: Takes a function as one of its inputs Outputs a function You can use functions anywhere you would use a value Functions are immutable so you can use them as dictionary keys Functions can be the return value of another function Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 34 / 39

λ (lambda) Functions Anonymous functions are function objects without a name lambda arg: ret is the same as def <lambda>(arg): return ret lambdas can have the same arguments as regular functions lambda arg, *args, named=val, **kwargs: ret lambdas must be one-liners and do not support annotations Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 35 / 39

Higher Order Functions The most common are map, filter, and reduce(foldl) map(f, seq) returns an iterator containing each element of seq but with f applied filter(f, seq) returns an iterator of the elements of seq where bool(f(seq[i]))is True filter(none, seq) is the same as filter(lambda x: x, seq) reduce must be imported. from functools import reduce reduce(f, seq, base) Builds up result by calling f on elements of seq starting with base f(...f(f(base,seq[0]),seq[1]),...) If base is not specified then the first argument is seq[0] Calling reduce on an empty sequence is a TypeError Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 36 / 39

Functions as Keyword Args Many functions will accept another function as a kwarg sorted(seq, key=f) sorted will call f on the elements to determine order The elements in the resulting list will be the same objects in seq Have the key return a tuple to sort multiple fields min(seq, key=f) and max(seq, key=f) behave similarly This is a good spot for lambda Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 37 / 39

Outline 1 Comprehension Review Lists 2 Function Arguments Positional and Named Arguments Variable Number of Arguments Variables Declared Outside Function 3 Functional Programming Background Higher Order Functions Partial Application Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 38 / 39

Partial Application Partial application creates a new function by supplying an existing function with some of its arguments Say you have add(x, y): x + y You want add_3(y): 3 + y add_3 = add(3) raises a TypeError add_3 = functools.partial(add, 3) Harry Smith (University of Pennsylvania) CIS 192 Lecture 3 January 25, 2018 39 / 39