

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 25
This page cannot be seen from the preview
Don't miss anything!
The indentation matters… First line with less indentation is considered to be outside of the function definition.
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.
def myfun(b, c=3, d=“hello”): return b + c myfun(5,3,”hello”) myfun(5,3) myfun(5)
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)
def foo(x=1,y=2,z=3): return(2x,4y,8*z) foo() (2, 8, 24) foo(z=100) (2, 8, 800)
applier(lambda z: z * 42, 7) 14
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
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
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: x x(100) >>> v <functionat 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:
def square(x): return x*x def applier(q, x): return q(x) applier(square, 7) 49
lambda x: x + 1 <function
at 0x1004e6ed8> f = lambda x: x + 1 f <function at 0x1004e6f50> f(100) 101