Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Lists in Python.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Python functional programming. Functions are first-class objects Functions can be used as any other datatype, eg: Arguments to function Return values.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
If/else, return, user input, strings
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Higher Order Functions
Introduction to Higher Order (Functional Programming) (Python) part 2
CSC1018F: Functional Programming
Topic: Functions – Part 2
basic Python programs, defining functions
Introduction to Python
basic Python programs, defining functions
MapReduce, Dictionaries, List Comprehensions
Coding Concepts (Sub- Programs)
FP Foundations, Scheme In Text: Chapter 14.
expressions, variables, for loops
expressions, variables, for loops
while loops; logic; random numbers; tuples
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Lambda Functions, MapReduce and List Comprehensions
CISC101 Reminders All assignments are now posted.
Recursion, Higher-order-functions
Functional Programming
„Lambda expressions, Optional”
def-ining a function A function as an execution control structure
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
© Sangeeta M Chauhan, Gwalior
Week 2 Classes and Objects
Lecture 2 - Names & Functions
Presentation transcript:

Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under:

2 Functions as parameters Have you ever wanted to pass an entire function as a parameter Python has functions as first-class citizens, so you can do this You simply pass the functions by name

3 Properties of Functions FieldDescription __name__ This is the name of the function. This only have a meaningful value is the function is defined with “def”. __class__ This is a reference to the class a method belongs to. __code__ This is a reference to the code object used in the implementation of python __doc__ This is the documentation string for the function.

4 inspect FieldDescription getdoc(x) Returns a pretty version of the docstring for the give object. getcomments(x) Returns the comments that appear just above the given function/class/module. getsource(x) Returns the source code for the given function/class/module getmembers(x) Returns a list of the members (fields and methods) of a class A useful class for inspecting functions and classes. –from inspect import *

5 Function Parameter Example ex.py def mult_2(x): return x * 2 def add_2(x): return x + 2 def opp_on_item(item, func): return func(item) #main opp_on_item(12, mult_2) #result: 24 opp_on_item(12, add_2) #result: 14

6 Lambda Sometimes you need a simply arithmetic function Its silly to write a method for it, but redundant not too With lambda we can create quick simple functions Facts –Lambda functions can only be comprised of a single expression –No loops, no calling other methods –Lambda functions can take any number of variables Syntax: lambda param1,…,paramn : expression

7 Lambda Syntax lambda.py #Example 1 square_func = lambda x : x**2 square_func(4) #return: 16 #Example 2 close_enough = lambda x, y : abs(x – y) < 3 close_enough(2, 4) #return: True #Example 3 def get_func(n) : return lambda x : x * n + x % n my_func = get_func(13) my_func(4) #return: 56

8 operator Most of the built-in functions (len, +, *, <) can be accessed through the operator module Need to import the operator module –from operator import * OperatorFunction - sub(x, y) + add(x, y) * __mul__(self, other) OperatorFunction - neg(x) + pos(x) OperatorFunction == eq(x,y) != ne(x, y) < lt(x, y) > gt(x, y) <= le(x, y) >= ge(x, y)

9 Partially Instantiated Functions We have seen that we can create lambda functions for quick functions on the go We have also seen that we can use the built in operators through the operator class What we would like to do is use the built in operators with a silly lambda function We can do this by partially instantiating function with the partial function from the functools package –You supply some of the parameters and get a function back the needs the rest of the parameters in order to execute

10 partial partial.py def mult1(x): return 2 * x mult2 = lambda x : 2 * x mult3 = partial(mul, 2) x = 10 print(mult1(5)); #10 print(mult2(5)); #10 print(mult3(5)); #10

11 Higher-Order Functions A higher-order function is a function that takes another function as a parameter They are “higher-order” because it’s a function of a function Examples –Map –Reduce –Filter Lambda works great as a parameter to higher-order functions if you can deal with its limitations

12 Transform Example Let’s write a method called transform that takes a list and a function as parameters and applies the function to each element of the list transform.py def mult_2(x): return x * 2... #Main x = [1, 2, 3] transform(x, mult_2) print(x) #[2, 4, 6]

13 Transform Solution transform.py def transform(arr, func): for i in range(len(arr)): arr[i] = func(arr[i]) x = [1, 2, 3] transform(x, mult_2) print(x) #[2, 4, 6]