Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions in Python, Exercises of Functional Programming

Function definition begins with “def.” Function name and its arguments. The keyword 'return' indicates the value to be sent back to the caller. Colon.

Typology: Exercises

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Functions in Python
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Functions in Python and more Exercises Functional Programming in PDF only on Docsity!

Functions in Python

The indentation matters… First line with less indentation is considered to be outside of the function definition.

Defining Functions

No header file or declaration of types of function or arguments def get_final_answer(filename): “““ Documentation String ””” line line return total_counter Function definition begins with “def.” (^) Function name and its arguments. The keyword ‘return’ indicates the value to be sent back to the caller. Colon.

Calling a Function

  • The syntax for a function call is: >>> def myfun(x, y): return x * y >>> myfun(3, 4) 12
  • Parameters in Python are Call by Assignment
    • Old values for the variables that are parameter
names are hidden, and these variables are
simply made to refer to the new values
  • All assignment in Python, including binding
function parameters, uses reference semantics.

Functions without returns

  • All functions in Python have a return value,

even if no return line inside the code

  • Functions without a return return the special

value None

  • None is a special constant in the language
  • None is used like NULL , void , or nil in other

languages

  • None is also logically equivalent to False
  • The interpreter’s REPL doesn’t print None

Default Values for Arguments

  • You can provide default values for a

function’s arguments

  • These arguments are optional when the

function is called

def myfun(b, c=3, d=“hello”): return b + c myfun(5,3,”hello”) myfun(5,3) myfun(5)

All of the above function calls return 8

Keyword Arguments

  • Can call a function with some/all of its arguments

out of order as long as you specify their names

def foo(x,y,z): return(2x,4y,8*z) foo(2,3,4) (4, 12, 32) foo(z=4, y=2, x=3) (6, 8, 32) foo(-2, z=-4, y=-3) (-4, -12, -32)

  • Can be combined with defaults, too

    def foo(x=1,y=2,z=3): return(2x,4y,8*z) foo() (2, 8, 24) foo(z=100) (2, 8, 800)

Lambda Notation

  • Python’s lambda creates anonymous functions

    applier(lambda z: z * 42, 7) 14

  • Note: only one expression in the lambda body; its value is always returned
  • Python supports functional programming idioms: map, filter, closures, continuations, etc.

Lambda Notation

Be careful with the syntax

f = lambda x,y : 2 * x + y f <function at 0x87d30> f(3, 4) 10 v = lambda x: xx(100) v <function at 0x87df0> v = (lambda x: xx)(100) v 10000

Example: closure

def counter(start=0, step=1): x = [start] def _inc(): x[0] += step return x[0] return _inc c1 = counter() c2 = counter(100, -10) c1() 1 c2() 90

map, filter, reduce

def add1(x): return x+ def odd(x): return x%2 == 1 def add(x,y): return x + y map(add1, [1,2,3,4]) [2, 3, 4, 5] map(+,[1,2,3,4],[100,200,300,400]) map(+,[1,2,3,4],[100,200,300,400]) ^ SyntaxError: invalid syntax map(add,[1,2,3,4],[100,200,300,400]) [101, 202, 303, 404] reduce(add, [1,2,3,4]) 10 filter(odd, [1,2,3,4])

foo(z=4, y=2, x=3) (6, 8, 32) >>> foo(-2, z=-4, y=-3) (-4, -12, -32) - Can be combined with defaults, too >>> def foo(x=1,y=2,z=3): return(2x,4y,8z) >>> foo() (2, 8, 24) >>> foo(z=100) (2, 8, 800) ### Lambda Notation - Python’s lambda creates anonymous functions >>> applier(lambda z: z * 42, 7) 14 - Note: only one expression in the lambda body; its value is always returned - Python supports functional programming idioms: map, filter, closures, continuations, etc. ### Lambda Notation #### Be careful with the syntax >>> f = lambda x,y : 2 * x + y >>> f <function at 0x87d30> >>> f(3, 4) 10 >>> v = lambda x: xx(100) >>> v <function at 0x87df0> >>> v = (lambda x: x*x)(100) >>> v 10000 ### Example: closure >>> def counter(start=0, step=1): x = [start] def _inc(): x[0] += step return x[0] return _inc >>> c1 = counter() >>> c2 = counter(100, -10) >>> c1() 1 >>> c2() 90 ### map, filter, reduce >>> def add1(x): return x+ >>> def odd(x): return x%2 == 1 >>> def add(x,y): return x + y >>> map(add1, [1,2,3,4]) [2, 3, 4, 5] >>> map(+,[1,2,3,4],[100,200,300,400]) map(+,[1,2,3,4],[100,200,300,400]) ^ SyntaxError: invalid syntax >>> map(add,[1,2,3,4],[100,200,300,400]) [101, 202, 303, 404] >>> reduce(add, [1,2,3,4]) 10 >>> filter(odd, [1,2,3,4]) [1, 3]

Functions are first-class objects Functions can be used as any other datatype, eg:

  • Arguments to function
  • Return values of functions
  • Assigned to variables
  • Parts of tuples, lists, etc

def square(x): return x*x def applier(q, x): return q(x) applier(square, 7) 49

Lambda Notation

Python’s lambda creates anonymous functions

lambda x: x + 1 <function at 0x1004e6ed8> f = lambda x: x + 1 f <function at 0x1004e6f50> f(100) 101

Lambda Notation Limitations

  • Note: only one expression in the lambda body; Its value is always returned
  • The lambda expression must fit on one line!
  • Lambda will probably be deprecated in future versions of python Guido is not a lambda fanboy

Functional programming

  • Python supports functional programming idioms
  • Builtins for map, reduce, filter, closures, continuations, etc.
  • These are often used with lambda