week1 Fri

Contents

week1 Fri#

function#

# what is the output

def addone(x):
    x + 1

y = addone(2)
print(y)

# (<--) 3
# (-->) None
# ( ) 2

# if no return, then return None
None
# need to specify return
def addone(x):
    return x + 1

y = addone(2)
print(y)
3
def myadd(x):
    a = x + 1
    return a

a = 1
c = 2
b = myadd(c)

# what is a
# (<-) a = 3
# (->) a = 1
# ( ) a = 2
print(a)

# a is a local variable, does not change the variable a outside
1
def myfun(x):
    x[0] = 99
    return x

x = [0,1,2]
y = myfun(x)

print(x)
#  what is the output
# (<-) [0,1,2]
# (->) [99,1,2]
# ( ) None

# list is mutable, x and y are bound to the same list
[99, 1, 2]
y[-1] = -1
print(x)
[99, 1, -1]
def cubic(x):
    return x**3

def evaluate(f,x):
    return f(x)

print(evaluate(cubic, 2))

# function can be argument of another function
8
# function can have multiple output
def myfun(x):
    return x**2, x**3

a,b = myfun(2)
print(a)
print(b)
4
8
# what is 
# (<-) 4
# (->) (4,8)
# ( ) 8

c = myfun(2)
print(c)

# myfun actually return a tuple
(4, 8)
a,b = (0,1)
print(a)
print(b)

# and the tuple is unpacked to the variables
0
1
# function can have default arguments
def gen_list(x, y = 1):
    return [x,y]

print(gen_list(1,2))
[1, 2]
print(gen_list(1))
[1, 1]
a = 1
def myfun(x):
    return x + a

print(myfun(1))

# what is printed
# (<-) error
# (->) 2
# ( ) 1

# when the function does not find the definition of "a" inside (in the local scope)
# it try to find it outside (in the global scope of the file)
2
a = 10 
print(myfun(1))
# what is printed
# (<-) 11
# (->) 2
# ( ) error
11
def myfun(n):
    if n == 1:
        return 1
    else:
        return n*myfun(n-1)

print(myfun(3))

# what is printed
# (<-) 1
# (->) 6
# ( ) 3


# this is recursion: a function can call it self.
# the function compute n-factorial: myfun(n) = n! =  n * (n-1) * (n-2) ... * 2 * 1
6
# what happen
# myfun(3.5)


# this is dangerous, the function never return anything and similar to "infinite loop"
# myfun(3.5) calls myfun(2.5), which calls myfun(1.5), calls myfun(0.5), ....
# because the input is nevery 1, this goes on forever in theory
# in practice, python terminate the problem and give the following error:
# RecursionError: maximum recursion depth exceeded in comparison
type(myfun)
function
# lambda function is same as regular function defined using "def"
f = lambda x:x+2
type(f)
function
a = 1
fa = lambda x:x+a
fa(1)
2
a = 10
fa(1)
11
a = [5,2,3,1,4]
b = sorted(a)
# is "a" sorted in the end?
# (<-) Yes
# (->) no
# ( ) Don't know
print(a)

# this return a new list, that is sorted
[5, 2, 3, 1, 4]
a = [5,2,3,1,4]
c = a.sort()
# is "a" sorted in the end?
# (<-) Yes
# (->) no
# ( ) Don't know
print(a)

# this sorts the list "a" in-place, it returns None
[1, 2, 3, 4, 5]
print(c)
# what is c
# (<-) None
# (->) "a" but sorted
# ( ) don't know
None
c = [5, -2, 3, -1, 4]
# how to sort by decreasing order of the absolute value of the elements
sorted(c, reverse = True, key = lambda x:abs(x))
[5, 4, 3, -1, -2]